nope/lib/cli/generateDefaultPackageConfig.ts

90 lines
2.1 KiB
TypeScript
Raw Normal View History

/**
* @author Martin Karkowski
* @email m.karkowski@zema.de
* @create date 2020-11-11 13:27:58
* @modify date 2021-01-18 18:48:59
* @desc [description]
*/
2020-11-23 06:09:31 +00:00
import { ArgumentParser } from "argparse";
import { readFile } from "fs/promises";
import { writeDefaultConfig } from "../loader/loadPackages";
import { getNopeLogger } from "../logger/getLogger";
// Define the Main Function.
// This function is used as cli tool.
export const generateDefaultPackageConfig = async function (
additionalArguments: {
help: string;
type: "string" | "number";
name: string | string;
defaultValue?: any;
}[] = []
) {
const parser = new ArgumentParser({
// version: "1.0.0",
add_help: true,
2021-12-04 07:25:26 +00:00
description: "Command Line interface, determines the available Packages.",
});
let opts: {
modules: string;
};
for (const arg of additionalArguments) {
parser.add_argument(arg.name, {
help: arg.help,
default: arg.defaultValue,
2021-12-04 07:25:26 +00:00
type: arg.type,
});
}
try {
// Try to read in the default config file.
opts = JSON.parse(
await readFile("./nopeconfig.json", {
2021-12-04 07:25:26 +00:00
encoding: "utf-8",
})
);
} catch (error) {
opts = {} as any;
}
parser.add_argument("-f", "--file", {
help: "File containing containing the package definitions.",
default: "./config/settings.json",
type: "str",
2021-12-04 07:25:26 +00:00
dest: "file",
});
2022-01-26 12:03:03 +00:00
parser.add_argument("-d", "--dir", {
help: "Directory containing the Modules.",
default: "not-provided",
type: "str",
2021-12-04 07:25:26 +00:00
dest: "modules",
});
const args = parser.parse_args();
if (args.modules != "not-provided") {
opts.modules = args.modules;
}
// Define a Logger
const logger = getNopeLogger("Setup-CLI");
try {
logger.info("Scanning " + opts.modules);
// Write the Config.
await writeDefaultConfig(opts.modules, args.file);
// Inform the user.
logger.info("Created Package-File at " + args.file);
} catch (error) {
logger.error("Failed generating the Config");
logger.error(error);
}
2020-11-23 06:09:31 +00:00
};
if (require.main === module) {
generateDefaultPackageConfig().catch((e) => console.error(e));
}