/** * @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", add_help: true, 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, 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.add_argument("-f", "--file", { help: "File containing containing the package definitions.", default: "./config/settings.json", type: "str", dest: "file", }); parser.add_argument("-d", "--dir", { help: "Directory containing the Modules.", default: "not-provided", type: "str", 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); } }; if (require.main === module) { generateDefaultPackageConfig().catch((e) => console.error(e)); }