2020-11-11 16:08:11 +00:00
|
|
|
/**
|
|
|
|
* @author Martin Karkowski
|
|
|
|
* @email m.karkowski@zema.de
|
|
|
|
* @create date 2020-11-11 13:27:58
|
|
|
|
* @modify date 2020-11-11 16:59:09
|
|
|
|
* @desc [description]
|
|
|
|
*/
|
|
|
|
|
|
|
|
import { ArgumentParser } from 'argparse';
|
|
|
|
import { readFile } from 'fs/promises';
|
|
|
|
import "reflect-metadata";
|
|
|
|
import { promisify } from 'util';
|
|
|
|
import { AmqpLayer } from '../communication/amqpLayer';
|
|
|
|
import { EventLayer } from '../communication/eventLayer';
|
|
|
|
import { IoSocketClient } from '../communication/IoSocketClient';
|
|
|
|
import { IoSocketServer } from '../communication/IoSocketServer';
|
|
|
|
import { getLinkedDispatcher } from '../dispatcher/getLinkedDispatcher';
|
|
|
|
import { getPackageLoader } from '../loader/getPackageLoader';
|
|
|
|
import { loadPackages } from '../loader/loadPackages';
|
|
|
|
import { getNopeLogger } from '../logger/getLogger';
|
|
|
|
import { INopeDispatcher } from '../types/nope/nopeDispatcher.interface';
|
|
|
|
|
|
|
|
|
|
|
|
// 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, determines the available Packages.'
|
|
|
|
});
|
|
|
|
|
|
|
|
const validLayers= {
|
|
|
|
'event': EventLayer,
|
|
|
|
'amqp': AmqpLayer,
|
|
|
|
'io-server': IoSocketServer,
|
|
|
|
'io-client': IoSocketClient
|
|
|
|
}
|
|
|
|
|
|
|
|
const layerDefaults= {
|
|
|
|
'event': [],
|
|
|
|
'amqp': ['localhost'],
|
|
|
|
'io-server': [7000],
|
2020-11-11 17:24:21 +00:00
|
|
|
'io-client': ['http://localhost:7000']
|
2020-11-11 16:08:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let opts: {
|
|
|
|
params: any[],
|
|
|
|
}
|
|
|
|
|
|
|
|
try{
|
|
|
|
// Try to read in the default config file.
|
|
|
|
opts = JSON.parse(await readFile('./nopeconfig.json', {
|
|
|
|
encoding: 'utf-8'
|
|
|
|
}));
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
opts = {} as any
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
parser.addArgument(
|
|
|
|
['-f', '--file'],
|
|
|
|
{
|
|
|
|
help: 'File containing containing the package definitions.',
|
|
|
|
defaultValue: './config/packages.json',
|
|
|
|
type: 'string',
|
|
|
|
dest: 'file'
|
|
|
|
}
|
|
|
|
);
|
|
|
|
parser.addArgument(
|
|
|
|
['-c', '--channel'],
|
|
|
|
{
|
|
|
|
help: 'The Communication Channel, which should be used. Possible Values are: ' +
|
|
|
|
// Display all Options:
|
|
|
|
Object.getOwnPropertyNames(validLayers).map(item => '"' + item + '"').join(", "),
|
|
|
|
defaultValue: 'event',
|
|
|
|
type: 'string',
|
|
|
|
dest: 'channel'
|
|
|
|
}
|
|
|
|
);
|
|
|
|
parser.addArgument(
|
|
|
|
['-params', '--params'],
|
|
|
|
{
|
|
|
|
help: 'Paramas for the Channel, to connect to. The Following Defaults are used: \n' + JSON.stringify(layerDefaults, undefined, 4),
|
|
|
|
defaultValue: 'not-provided',
|
|
|
|
type: 'string',
|
|
|
|
dest: 'params'
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
const args = parser.parseArgs();
|
|
|
|
// Define a Logger
|
|
|
|
const logger = getNopeLogger('starter');
|
|
|
|
|
|
|
|
if ( !Object.getOwnPropertyNames(validLayers).includes(args.channel)) {
|
|
|
|
logger.error('Invalid Channel. Please use the following values. ' + Object.getOwnPropertyNames(validLayers).map(item => '"' + item + '"').join(", "))
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assign the Default Setting for the Channel.
|
|
|
|
opts.params = layerDefaults[args.channel];
|
|
|
|
|
|
|
|
if (args.params != 'not-provided'){
|
|
|
|
try {
|
|
|
|
opts.params = JSON.parse(args.params)
|
|
|
|
} catch (e) {
|
|
|
|
logger.error('Unable to parse the Parameters for the channel. Please use valid JSON!');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to create an dispatcher:
|
|
|
|
let dispatcher: INopeDispatcher;
|
|
|
|
try {
|
|
|
|
dispatcher = getLinkedDispatcher({
|
2020-11-11 16:39:42 +00:00
|
|
|
communicator: new validLayers[args.channel](... opts.params)
|
2020-11-11 16:08:11 +00:00
|
|
|
})
|
|
|
|
} catch (e) {
|
|
|
|
logger.error('Unable to create the Dispatcher. Please check the Provided Parameters.');
|
|
|
|
console.error(e)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-11-11 17:11:37 +00:00
|
|
|
await dispatcher.ready.waitFor(value => value);
|
|
|
|
|
2020-11-11 16:08:11 +00:00
|
|
|
// Try to load the Modules.
|
|
|
|
try {
|
|
|
|
await loadPackages(getPackageLoader(dispatcher), args.file)
|
|
|
|
} catch (e) {
|
|
|
|
logger.error('Unable to load the Packages defined in ' + args.file + ' See Output for detailled information');
|
|
|
|
console.error(e)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
main().catch(e => console.error(e));
|