2020-11-11 16:08:11 +00:00
|
|
|
/**
|
|
|
|
* @author Martin Karkowski
|
|
|
|
* @email m.karkowski@zema.de
|
|
|
|
* @create date 2020-11-11 13:27:58
|
2021-01-21 07:38:44 +00:00
|
|
|
* @modify date 2021-01-18 18:48:59
|
2020-11-11 16:08:11 +00:00
|
|
|
* @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";
|
2020-11-11 16:08:11 +00:00
|
|
|
|
|
|
|
// Define the Main Function.
|
|
|
|
// This function is used as cli tool.
|
2021-01-21 07:38:44 +00:00
|
|
|
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."
|
|
|
|
});
|
2020-11-11 16:08:11 +00:00
|
|
|
|
2021-01-21 07:38:44 +00:00
|
|
|
let opts: {
|
|
|
|
modules: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
for (const arg of additionalArguments) {
|
|
|
|
parser.addArgument(arg.name, {
|
|
|
|
help: arg.help,
|
|
|
|
defaultValue: arg.defaultValue,
|
|
|
|
type: arg.type
|
2020-11-11 16:08:11 +00:00
|
|
|
});
|
2021-01-21 07:38:44 +00:00
|
|
|
}
|
2020-11-11 16:08:11 +00:00
|
|
|
|
2021-01-21 07:38:44 +00:00
|
|
|
try {
|
|
|
|
// Try to read in the default config file.
|
|
|
|
opts = JSON.parse(
|
|
|
|
await readFile("./nopeconfig.json", {
|
|
|
|
encoding: "utf-8"
|
|
|
|
})
|
|
|
|
);
|
|
|
|
} catch (error) {
|
|
|
|
opts = {} as any;
|
|
|
|
}
|
2020-11-11 16:08:11 +00:00
|
|
|
|
2021-01-21 07:38:44 +00:00
|
|
|
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"
|
|
|
|
});
|
2020-11-11 16:08:11 +00:00
|
|
|
|
2021-01-21 07:38:44 +00:00
|
|
|
const args = parser.parseArgs();
|
2020-11-11 16:08:11 +00:00
|
|
|
|
2021-01-21 07:38:44 +00:00
|
|
|
if (args.modules != "not-provided") {
|
|
|
|
opts.modules = args.modules;
|
|
|
|
}
|
2020-11-11 16:08:11 +00:00
|
|
|
|
2021-01-21 07:38:44 +00:00
|
|
|
// Define a Logger
|
|
|
|
const logger = getNopeLogger("Setup-CLI");
|
2020-11-11 16:08:11 +00:00
|
|
|
|
2021-01-21 07:38:44 +00:00
|
|
|
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
|
|
|
};
|
2020-11-11 16:08:11 +00:00
|
|
|
|
2021-01-21 07:38:44 +00:00
|
|
|
if (require.main === module) {
|
|
|
|
generateDefaultPackageConfig().catch((e) => console.error(e));
|
|
|
|
}
|