nope/lib/cli/generateOpenApi.ts
2020-11-09 15:28:09 +01:00

87 lines
2.2 KiB
TypeScript

/**
* @author Martin Karkowski
* @email m.karkowski@zema.de
* @create date 2020-11-09 13:27:58
* @modify date 2020-11-09 15:05:43
* @desc [description]
*/
import { ArgumentParser } from 'argparse';
import { readFile } from 'fs/promises';
import { Logger } from 'winston';
import { getNopeLogger } from '../logger/getLogger';
import { parseToOpenAPI } from '../parsers/open-api/OpenApiParser';
// 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,
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'
}
);
const args = parser.parseArgs();
const logger = getNopeLogger('TS-Analyzer');
if (args.outputDir != 'not-provided'){
opts.outputDir = args.inputDir;
}
try {
// Try to read in the default config file.
const modules = JSON.parse(await readFile(args.file, {
encoding: 'utf-8'
}));
// Save the Logger
opts.logger = logger;
if (args.outputDir != 'not-provided') {
opts.outputDir = args.outputDir;
}
for (const m of modules) {
await parseToOpenAPI(m,opts);
}
} catch (error) {
logger.error('Unable to open File: ' + args.file)
}
}
main().catch(e => console.error(e));