nope/lib/cli/generateOpenApi.ts
2021-08-04 18:17:23 +02:00

146 lines
3.6 KiB
TypeScript

/**
* @author Martin Karkowski
* @email m.karkowski@zema.de
* @create date 2020-11-09 13:27:58
* @modify date 2020-12-03 08:29:27
* @desc [description]
*/
import { ArgumentParser } from "argparse";
import { readFile, writeFile } from "fs/promises";
import { ILogger } from "js-logger";
import { join } from "path";
import { getNopeLogger } from "../logger/getLogger";
import {
parseFunctionToOpenAPI,
parseModuleToOpenAPI
} from "../parsers/open-api/OpenApiParser";
import { ISystemElements } from "../types/ISystemElements";
import { NOPELOGO } from "./renderNope";
// Define the Main Function.
// This function is used as cli tool.
const main = async function () {
console.log(NOPELOGO);
console.log("\n\n");
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: ILogger;
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));