/*
* Copyright (C) 2019-2021 StApps
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see .
*/
import {HttpClient} from '@openstapps/api/lib/http-client';
import {PluginClient} from '@openstapps/api/lib/plugin-client';
import {Converter} from '@openstapps/core-tools/lib/schema';
import {Logger} from '@openstapps/logger';
import {Command} from 'commander';
import {readFileSync} from 'fs';
import {join, resolve} from 'path';
import {MinimalPlugin} from './plugin/minimal-plugin';
process.on('unhandledRejection', (error) => {
throw error;
});
const pluginVersion = JSON.parse(
readFileSync(join(__dirname, '..', 'package.json'))
.toString(),
).version;
const program = new Command()
.version(pluginVersion)
.option('-b, --backend-url ', 'URL of the StApps backend deployment', 'http://localhost:3000')
.option('-n, --plugin-name ', 'The name of the plugin', 'minimal-plugin') // TODO: adjust default
.option('-r, --route-name ', 'The name of the route', 'minimalplugin') // TODO: adjust default
.option('-u, --url ', 'The url of the plugin', 'http://localhost') // TODO: adjust default
.option('-p, --port ', 'The port of the plugin', '4000') // TODO: adjust default
.parse(process.argv);
const options = program.opts();
// read essencial options from ENV variables if available
if ('STAPPS_BACKEND' in process.env && typeof process.env.STAPPS_BACKEND === 'string') {
options.backendUrl = process.env.STAPPS_BACKEND;
}
if ('PLUGIN_URL' in process.env && typeof process.env.PLUGIN_URL === 'string') {
options.url = process.env.PLUGIN_URL;
}
if ('PLUGIN_PORT' in process.env && typeof process.env.PLUGIN_PORT === 'string') {
options.port = process.env.PLUGIN_PORT;
}
// create an instance of the PluginClient
const pluginClient = new PluginClient(new HttpClient(), options.backendUrl);
// create an instance of your plugin
const plugin = new MinimalPlugin(
// tslint:disable-next-line:no-magic-numbers
Number.parseInt(options.port, 10), options.pluginName, options.url, `/${options.routeName}`, options.backendUrl,
new Converter(resolve(__dirname, '..', 'src', 'plugin', 'protocol')), // an instance of the converter. Required
// because your requests and response schemas are defined in the plugin. The path should lead to your request and
// response interfaces
'SCMinimalRequest', // TODO: adjust name of the request interface
'SCMinimalResponse', // TODO: adjust name of the response interface
JSON.parse(readFileSync(resolve(__dirname, '..', 'package.json'))
.toString()).version, // get the version of the plugin from the package.json
);
pluginClient.registerPlugin(plugin)
.then(() => {
Logger.ok(`Successfully registered plugin '${options.pluginName}' on /${options.routeName} .`);
})
.catch((err: Error) => {
throw err;
});