2020-11-09 14:28:09 +00:00
|
|
|
/**
|
|
|
|
* @author Martin Karkowski
|
|
|
|
* @email m.karkowski@zema.de
|
|
|
|
* @create date 2020-11-09 13:27:58
|
2020-11-12 16:07:05 +00:00
|
|
|
* @modify date 2020-11-12 11:31:20
|
2020-11-09 14:28:09 +00:00
|
|
|
* @desc [description]
|
|
|
|
*/
|
|
|
|
|
|
|
|
import { ArgumentParser } from 'argparse';
|
2020-11-11 16:08:11 +00:00
|
|
|
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';
|
2020-11-11 16:08:11 +00:00
|
|
|
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,
|
2020-11-11 16:08:11 +00:00
|
|
|
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'
|
|
|
|
}
|
|
|
|
);
|
2020-11-11 16:08:11 +00:00
|
|
|
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'){
|
2020-11-11 16:08:11 +00:00
|
|
|
opts.outputDir = args.outputDir;
|
2020-11-09 14:28:09 +00:00
|
|
|
}
|
|
|
|
|
2020-11-11 16:08:11 +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.
|
2020-11-11 16:08:11 +00:00
|
|
|
data = JSON.parse(await readFile(args.file, {
|
2020-11-09 14:28:09 +00:00
|
|
|
encoding: 'utf-8'
|
|
|
|
}));
|
2020-11-11 16:08:11 +00:00
|
|
|
} catch (error) {
|
|
|
|
logger.error('Unable to open File: ' + args.file)
|
|
|
|
}
|
2020-11-09 14:28:09 +00:00
|
|
|
|
2020-11-11 16:08:11 +00:00
|
|
|
if (data != null){
|
2020-11-09 14:28:09 +00:00
|
|
|
// Save the Logger
|
|
|
|
opts.logger = logger;
|
|
|
|
|
2020-11-11 16:08:11 +00:00
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2020-11-11 16:08:11 +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
|
|
|
}
|
2020-11-11 16:08:11 +00:00
|
|
|
|
|
|
|
try {
|
2020-11-12 16:07:05 +00:00
|
|
|
await writeFile(join('dist', 'lib', 'open-api', 'apidoc.json'), JSON.stringify(data.generalInformationModel, undefined, 4));
|
2020-11-11 16:08:11 +00:00
|
|
|
} 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-11 16:08:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2020-11-09 14:28:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
main().catch(e => console.error(e));
|