nope/lib/cli/generateOpenApi.ts

126 lines
3.4 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-12 11:31:20
2020-11-09 14:28:09 +00:00
* @desc [description]
*/
import { ArgumentParser } from 'argparse';
import { readFile, writeFile } from 'fs/promises';
import { join } from 'path';
2020-11-09 14:28:09 +00:00
import { Logger } from 'winston';
import { getNopeLogger } from '../logger/getLogger';
import { parseFunctionToOpenAPI, parseModuleToOpenAPI } from '../parsers/open-api/OpenApiParser';
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 () {
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,
2020-11-09 14:28:09 +00:00
logger: Logger;
}
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'
});
2020-11-09 14:28:09 +00:00
const args = parser.parseArgs();
const logger = getNopeLogger('TS-Analyzer');
if (args.outputDir != 'not-provided'){
opts.outputDir = args.outputDir;
2020-11-09 14:28:09 +00:00
}
opts.sharedDefinitions = Array.isArray(args.sharedDefinitions);
let data: ISystemElements;
2020-11-09 14:28:09 +00:00
try {
// Try to read in the default config file.
data = JSON.parse(await readFile(args.file, {
2020-11-09 14:28:09 +00:00
encoding: 'utf-8'
}));
} catch (error) {
logger.error('Unable to open File: ' + args.file)
}
2020-11-09 14:28:09 +00:00
if (data != null){
2020-11-09 14:28:09 +00:00
// 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);
}
2020-11-09 14:28:09 +00:00
}
// 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);
}
2020-11-09 14:28:09 +00:00
}
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);
}
2020-11-09 14:28:09 +00:00
}
2020-11-09 14:28:09 +00:00
}
main().catch(e => console.error(e));