nope/lib/cli/grabTypescriptDefintion.ts

121 lines
3.0 KiB
TypeScript
Raw Normal View History

2020-11-09 14:28:09 +00:00
/**
* @author Martin Karkowski
* @email m.karkowski@zema.de
* @create date 2020-11-09 13:27:58
* @modify date 2020-11-10 16:20:05
2020-11-09 14:28:09 +00:00
* @desc [description]
*/
2020-11-23 06:09:31 +00:00
import { ArgumentParser } from "argparse";
import { readFile, writeFile } from "fs/promises";
import { extractDefinitions } from "../analyzers/typescript/analyzeTypescriptFiles";
import { getNopeLogger } from "../logger/getLogger";
import { ISystemElements } from "../types/ISystemElements";
2020-11-09 14:28:09 +00:00
// 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;
2020-11-09 14:28:09 +00:00
try {
const settings = JSON.parse(
await readFile("./nopeconfig.json", {
encoding: "utf-8"
})
2020-11-09 14:28:09 +00:00
);
// Try to read in the default config file.
opts = settings.analyzers.typescript;
2020-11-09 14:28:09 +00:00
opts.tempDir = settings.tempDir;
} catch (error) {
opts = {} as any;
}
2020-11-09 14:28:09 +00:00
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);
2020-11-09 14:28:09 +00:00
await writeFile(args.file, JSON.stringify(res, undefined, 4));
2020-11-09 14:28:09 +00:00
logger.info("Defined config in " + args.file);
2020-11-23 06:09:31 +00:00
};
2020-11-09 14:28:09 +00:00
main().catch((e) => console.error(e));