Skip to content

Commit

Permalink
multiple confs
Browse files Browse the repository at this point in the history
  • Loading branch information
timbrinded committed Nov 2, 2023
1 parent 7492e9c commit d902230
Show file tree
Hide file tree
Showing 5 changed files with 215 additions and 19 deletions.
1 change: 1 addition & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
"execa": "^8.0.1",
"inquirer": "^9.2.11",
"inquirer-press-to-continue": "^1.2.0",
"jsonc-parser": "^3.2.0",
"minimatch": "^9.0.3",
"node-fetch": "^3.3.2",
"semver": "^7.5.4",
Expand Down
15 changes: 14 additions & 1 deletion packages/cli/src/cmds/entrypoint.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import "../internal/logging";
import "@moonbeam-network/api-augment";
import yargs from "yargs";
import fs from "fs";
import { hideBin } from "yargs/helpers";
import { testCmd } from "./runTests";
import { runNetworkCmd } from "./runNetwork";
Expand All @@ -10,14 +11,26 @@ import { fetchArtifact } from "../internal/cmdFunctions/fetchArtifact";
import dotenv from "dotenv";
dotenv.config();

const defaultConfigFiles = ["./moonwall.config", "./moonwall.config.json"];

function findExistingConfig(files: string[]): string | undefined {
for (const file of files) {
if (fs.existsSync(file)) {
return file;
}
}
}

const defaultConfigFile = findExistingConfig(defaultConfigFiles) || "./moonwall.config.json";

// Hack to expose config-path to all commands and fallback
const parsed = yargs(hideBin(process.argv))
.options({
configFile: {
type: "string",
alias: "c",
description: "path to MoonwallConfig file",
default: "./moonwall.config.json",
default: defaultConfigFile,
},
})
.parseSync();
Expand Down
33 changes: 28 additions & 5 deletions packages/cli/src/lib/configReader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,31 @@ import "@moonbeam-network/api-augment";
import { MoonwallConfig } from "@moonwall/types";
import fs, { readFile } from "fs/promises";
import { readFileSync } from "fs";
import path from "path";
import JSONC from "jsonc-parser";
import path, { extname } from "path";

let cachedConfig: MoonwallConfig | undefined;

async function parseConfig(filePath: string) {
let result: any;

const file = await readFile(filePath, "utf8");

switch (extname(filePath)) {
case ".json":
result = JSON.parse(file);
break;
case ".config":
result = JSONC.parse(file);
break;
default:
result = undefined;
break;
}

return result;
}

export async function loadConfig(path: string): Promise<MoonwallConfig> {
if (
!(await fs
Expand Down Expand Up @@ -89,11 +110,13 @@ export async function importAsyncConfig(): Promise<MoonwallConfig> {
const filePath = path.isAbsolute(configPath) ? configPath : path.join(process.cwd(), configPath);

try {
const file = await readFile(filePath, "utf8");
const json = JSON.parse(file);
const replacedJson = replaceEnvVars(json);
// const file = await readFile(filePath, "utf8");
// const json = JSON.parse(file);

cachedConfig = replacedJson as MoonwallConfig;
const config = await parseConfig(filePath);
const replacedConfig = replaceEnvVars(config);

cachedConfig = replacedConfig as MoonwallConfig;
return cachedConfig;
} catch (e) {
console.error(e);
Expand Down
Loading

0 comments on commit d902230

Please sign in to comment.