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-11 16:08:11 +00:00
|
|
|
* @modify date 2020-11-10 16:20:05
|
2020-11-09 14:28:09 +00:00
|
|
|
* @desc [description]
|
|
|
|
*/
|
|
|
|
|
|
|
|
import { ArgumentParser } from 'argparse';
|
|
|
|
import { readFile, writeFile } from 'fs/promises';
|
|
|
|
import { Logger } from 'winston';
|
|
|
|
import { extractDefinitions } from '../analyzers/typescript/analyzeTypescriptFiles';
|
|
|
|
import { getNopeLogger } from '../logger/getLogger';
|
2020-11-11 16:08:11 +00:00
|
|
|
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 () {
|
|
|
|
let opts: {
|
|
|
|
inputDir: string,
|
|
|
|
tsConfig: string,
|
|
|
|
tempDir: string,
|
|
|
|
// A Logger.
|
|
|
|
logger?: Logger
|
|
|
|
} = null;
|
|
|
|
|
|
|
|
try{
|
|
|
|
const settings = JSON.parse(await readFile('./nopeconfig.json', {
|
|
|
|
encoding: 'utf-8'
|
|
|
|
}));
|
|
|
|
|
|
|
|
// Try to read in the default config file.
|
|
|
|
opts = settings.analyzers.typescript;
|
|
|
|
|
|
|
|
opts.tempDir = settings.tempDir;
|
|
|
|
} catch (error) {
|
|
|
|
opts = {} as any
|
|
|
|
}
|
|
|
|
|
|
|
|
const parser = new ArgumentParser({
|
|
|
|
version: '1.0.0',
|
|
|
|
addHelp: true,
|
|
|
|
description: 'Command Line interface, generate the interface based on the project folder.'
|
|
|
|
});
|
|
|
|
parser.addArgument(
|
|
|
|
[ '-i', '--inputDir' ],
|
|
|
|
{
|
|
|
|
help: 'Input Folder to Scan',
|
|
|
|
defaultValue: 'not-provided',
|
|
|
|
type: 'string',
|
|
|
|
dest: 'inputDir'
|
|
|
|
}
|
|
|
|
);
|
|
|
|
parser.addArgument(
|
|
|
|
[ '-t', '--tsConfig' ],
|
|
|
|
{
|
|
|
|
help: 'Typescript-Settings File to use.',
|
|
|
|
defaultValue: 'not-provided',
|
|
|
|
type: 'string',
|
|
|
|
dest: 'tsConfig'
|
|
|
|
}
|
|
|
|
);
|
|
|
|
parser.addArgument(
|
|
|
|
['-f', '--file'],
|
|
|
|
{
|
|
|
|
help: 'File containing all Defintions, which are currently available. If it doenst extists it will be generated',
|
|
|
|
defaultValue: './config/description.json',
|
|
|
|
type: 'string',
|
|
|
|
dest: 'file'
|
|
|
|
}
|
2020-11-11 16:08:11 +00:00
|
|
|
);
|
|
|
|
parser.addArgument(
|
|
|
|
['-o', '--overwrite'],
|
|
|
|
{
|
|
|
|
help: 'You can extend the existing description file or overwrite it.',
|
|
|
|
defaultValue: true,
|
|
|
|
dest: 'overwrite'
|
|
|
|
}
|
2020-11-09 14:28:09 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
const args = parser.parseArgs();
|
|
|
|
const logger = getNopeLogger('TS-Analyzer');
|
|
|
|
|
|
|
|
if (args.inputDir != 'not-provided'){
|
|
|
|
opts.inputDir = args.inputDir;
|
|
|
|
}
|
|
|
|
if (args.inputDir != 'not-provided'){
|
|
|
|
opts.tsConfig = args.tsConfig;
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.debug('using the following settings: \n' + JSON.stringify(opts, undefined,4));
|
|
|
|
|
|
|
|
logger.info('scanning: ' + opts.inputDir);
|
2020-11-11 16:08:11 +00:00
|
|
|
opts.logger = logger;
|
|
|
|
|
|
|
|
let definition: ISystemElements = {
|
|
|
|
services: [],
|
|
|
|
modules: [],
|
|
|
|
generalInformationModel: {}
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (!args.overwrite) {
|
|
|
|
// Try to read in the default config file.
|
|
|
|
definition = JSON.parse(await readFile(args.file, {
|
|
|
|
encoding: 'utf-8'
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
definition = {
|
|
|
|
services: [],
|
|
|
|
modules: [],
|
|
|
|
generalInformationModel: {}
|
|
|
|
};
|
|
|
|
}
|
2020-11-09 14:28:09 +00:00
|
|
|
|
|
|
|
// Extract the Definitions.
|
2020-11-11 16:08:11 +00:00
|
|
|
const res = await extractDefinitions(opts, definition);
|
2020-11-09 14:28:09 +00:00
|
|
|
|
|
|
|
await writeFile(args.file, JSON.stringify(res, undefined, 4));
|
|
|
|
|
2020-11-11 16:08:11 +00:00
|
|
|
logger.info('Defined config in ' + args.file)
|
2020-11-09 14:28:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
main().catch(e => console.error(e));
|