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

140 lines
3.9 KiB
TypeScript

/**
* @author Martin Karkowski
* @email m.karkowski@zema.de
* @create date 2020-11-09 13:27:58
* @modify date 2020-11-13 12:06:54
* @desc [description]
*/
import { ArgumentParser } from "argparse";
import { readFile, writeFile } from "fs/promises";
import { join } from "path";
import { Logger } from "winston";
import { getNopeLogger } from "../logger/getLogger";
import { parseFunctionToOpenAPI, parseModuleToOpenAPI } from "../parsers/open-api/OpenApiParser";
import { ISystemElements } from "../types/ISystemElements";
// Define the Main Function.
// This function is used as cli tool.
const main = async function () {
const parser = new ArgumentParser({
version: "1.0.0",
addHelp: true,
description: "Command Line interface, generate the interface based on the project folder."
});
let opts: {
outputDir: string,
sharedDefinitions: boolean,
logger: Logger;
mode: "js" | "ts";
};
try{
// Try to read in the default config file.
opts = JSON.parse(await readFile("./nopeconfig.json", {
encoding: "utf-8"
})).parsers.openApi;
} catch (error) {
opts = {} as any;
}
parser.addArgument(
["-f", "--file"],
{
help: "File containing all Defintions, which are currently available.",
defaultValue: "./config/description.json",
type: "string",
dest: "file"
}
);
parser.addArgument(
["-o", "--outputDir"],
{
help: "Directory, in which the settings should be provided",
defaultValue: "not-provided",
type: "string",
dest: "outputDir"
}
);
parser.addArgument(["-s", "--sharedDefinitions"], {
help: "Directory, in which the settings should be provided",
action: "append", nargs: "?",
dest: "sharedDefinitions"
});
parser.addArgument(["--ts"], {
help: "Enable Typescript Rendering",
action: "append", nargs: "?",
dest: "ts"
});
const args = parser.parseArgs();
const logger = getNopeLogger("TS-Analyzer");
if (args.outputDir != "not-provided"){
opts.outputDir = args.outputDir;
}
opts.sharedDefinitions = Array.isArray(args.sharedDefinitions);
opts.mode = Array.isArray(args.ts) ? "ts" : "js";
// Adapt the default output folder if Javascript file generators are used.
if (opts.mode === "js" && !opts.outputDir.startsWith("./dist")){
if (opts.outputDir.startsWith("./")){
opts.outputDir = opts.outputDir.replace("./","./dist/");
}
}
let data: ISystemElements;
try {
// Try to read in the default config file.
data = JSON.parse(await readFile(args.file, {
encoding: "utf-8"
}));
} catch (error) {
logger.error("Unable to open File: " + args.file);
}
if (data != null){
// Save the Logger
opts.logger = logger;
// Parse all modules.
for (const mod of data.modules) {
try {
await parseModuleToOpenAPI(mod, opts);
} catch(e) {
opts.logger.error("Failed to parse Module " + mod.name);
console.error(e);
}
}
// Parse all services
for (const func of data.services) {
try {
await parseFunctionToOpenAPI(func, opts);
} catch(e) {
opts.logger.error("Failed to parse Service " + func.id);
console.error(e);
}
}
try {
await writeFile(join("dist", "lib", "open-api", "apidoc.json"), JSON.stringify(data.generalInformationModel, undefined, 4));
} catch(e){
opts.logger.error("Failed to generate " + join("dist", "src", "apidoc.json"));
console.error(e);
}
}
};
main().catch(e => console.error(e));