nope/lib/cli/generateDefaultPackageConfig.ts

90 lines
2.1 KiB
TypeScript

/**
* @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]
*/
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",
addHelp: true,
description: "Command Line interface, determines the available Packages."
});
let opts: {
modules: string;
};
for (const arg of additionalArguments) {
parser.addArgument(arg.name, {
help: arg.help,
defaultValue: arg.defaultValue,
type: arg.type
});
}
try {
// Try to read in the default config file.
opts = JSON.parse(
await readFile("./nopeconfig.json", {
encoding: "utf-8"
})
);
} catch (error) {
opts = {} as any;
}
parser.addArgument(["-f", "--file"], {
help: "File containing containing the package definitions.",
defaultValue: "./config/settings.json",
type: "string",
dest: "file"
});
parser.addArgument(["-s", "--search"], {
help: "Directory containing the Modules.",
defaultValue: "not-provided",
type: "string",
dest: "modules"
});
const args = parser.parseArgs();
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);
}
};
if (require.main === module) {
generateDefaultPackageConfig().catch((e) => console.error(e));
}