nope/lib/cli/grabTypescriptDefintion.ts
2020-11-23 07:09:31 +01:00

125 lines
3.4 KiB
TypeScript

/**
* @author Martin Karkowski
* @email m.karkowski@zema.de
* @create date 2020-11-09 13:27:58
* @modify date 2020-11-10 16:20:05
* @desc [description]
*/
import { ArgumentParser } from "argparse";
import { readFile, writeFile } from "fs/promises";
import { Logger } from "winston";
import { extractDefinitions } from "../analyzers/typescript/analyzeTypescriptFiles";
import { getNopeLogger } from "../logger/getLogger";
import { ISystemElements } from "../types/ISystemElements";
// Define the Main Function.
// This function is used as cli tool.
const main = async function () {
let opts: {
inputDir: string,
tsConfig: string,
tempDir: string,
// A Logger.
logger?: Logger
} = null;
try{
const settings = JSON.parse(await readFile("./nopeconfig.json", {
encoding: "utf-8"
}));
// Try to read in the default config file.
opts = settings.analyzers.typescript;
opts.tempDir = settings.tempDir;
} catch (error) {
opts = {} as any;
}
const parser = new ArgumentParser({
version: "1.0.0",
addHelp: true,
description: "Command Line interface, generate the interface based on the project folder."
});
parser.addArgument(
[ "-i", "--inputDir" ],
{
help: "Input Folder to Scan",
defaultValue: "not-provided",
type: "string",
dest: "inputDir"
}
);
parser.addArgument(
[ "-t", "--tsConfig" ],
{
help: "Typescript-Settings File to use.",
defaultValue: "not-provided",
type: "string",
dest: "tsConfig"
}
);
parser.addArgument(
["-f", "--file"],
{
help: "File containing all Defintions, which are currently available. If it doenst extists it will be generated",
defaultValue: "./config/description.json",
type: "string",
dest: "file"
}
);
parser.addArgument(
["-o", "--overwrite"],
{
help: "You can extend the existing description file or overwrite it.",
defaultValue: true,
dest: "overwrite"
}
);
const args = parser.parseArgs();
const logger = getNopeLogger("TS-Analyzer");
if (args.inputDir != "not-provided"){
opts.inputDir = args.inputDir;
}
if (args.inputDir != "not-provided"){
opts.tsConfig = args.tsConfig;
}
logger.debug("using the following settings: \n" + JSON.stringify(opts, undefined,4));
logger.info("scanning: " + opts.inputDir);
opts.logger = logger;
let definition: ISystemElements = {
services: [],
modules: [],
generalInformationModel: {}
};
try {
if (!args.overwrite) {
// Try to read in the default config file.
definition = JSON.parse(await readFile(args.file, {
encoding: "utf-8"
}));
}
} catch (error) {
definition = {
services: [],
modules: [],
generalInformationModel: {}
};
}
// Extract the Definitions.
const res = await extractDefinitions(opts, definition);
await writeFile(args.file, JSON.stringify(res, undefined, 4));
logger.info("Defined config in " + args.file);
};
main().catch(e => console.error(e));