refactor: build system

This commit is contained in:
2023-03-22 11:45:30 +01:00
parent 4df19e8c20
commit 8cb9285462
427 changed files with 3978 additions and 9810 deletions

View File

@@ -1,3 +1,4 @@
/* eslint-disable unicorn/prefer-module */
/*
* Copyright (C) 2019-2021 StApps
* This program is free software: you can redistribute it and/or modify it
@@ -13,35 +14,36 @@
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {HttpClient} from '@openstapps/api/lib/http-client.js';
import {PluginClient} from '@openstapps/api/lib/plugin-client.js';
import {Converter} from '@openstapps/core-tools/lib/schema.js';
import {HttpClient, PluginClient} from '@openstapps/api';
import {Converter} from '@openstapps/core-tools';
import {Logger} from '@openstapps/logger';
import {Command, Option} from 'commander';
import {readFileSync} from 'fs';
import {join, resolve} from 'path';
import path from 'path';
import {MinimalPlugin} from './plugin/minimal-plugin.js';
process.on('unhandledRejection', (error) => {
process.on('unhandledRejection', error => {
throw error;
});
const pluginVersion = JSON.parse(
readFileSync(join(__dirname, '..', 'package.json'))
.toString(),
).version;
const pluginVersion = JSON.parse(readFileSync(path.join(__dirname, '..', 'package.json')).toString()).version;
const program = new Command()
.version(pluginVersion)
// tslint:disable: newline-per-chained-call
.addOption(new Option('-b, --backend-url <string>', 'URL of the StApps backend deployment').default('http://localhost:3000').env('STAPPS_BACKEND'))
.addOption(
new Option('-b, --backend-url <string>', 'URL of the StApps backend deployment')
.default('http://localhost:3000')
.env('STAPPS_BACKEND'),
)
.addOption(new Option('-n, --plugin-name <string>', 'The name of the plugin').default('minimal-plugin')) // TODO: adjust default
.addOption(new Option('-r, --route-name <string>', 'The name of the route').default('minimalplugin')) // TODO: adjust default
.addOption(new Option('-u, --url <string>', 'The url of the plugin').default('http://localhost').env('PLUGIN_URL')) // TODO: adjust default
.addOption(
new Option('-u, --url <string>', 'The url of the plugin').default('http://localhost').env('PLUGIN_URL'),
) // TODO: adjust default
.addOption(new Option('-p, --port <number>', 'The port of the plugin').default('4000').env('PLUGIN_PORT')) // TODO: adjust default
.parse(process.argv);
// tslint:enable: newline-per-chained-call
// tslint:enable: newline-per-chained-call
const options = program.opts();
@@ -51,32 +53,38 @@ 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
Number.parseInt(options.port, 10),
options.pluginName,
options.url,
`/${options.routeName}`,
options.backendUrl,
new Converter(path.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
JSON.parse(readFileSync(path.resolve(__dirname, '..', 'package.json')).toString()).version, // get the version of the plugin from the package.json
);
pluginClient.registerPlugin(plugin)
pluginClient
.registerPlugin(plugin)
.then(() => {
Logger.ok(`Successfully registered plugin '${options.pluginName}' on /${options.routeName} .`);
})
.catch((err: Error) => {
throw err;
// eslint-disable-next-line unicorn/prefer-top-level-await
.catch((error: Error) => {
throw error;
});
[`exit`, `SIGINT`, `SIGUSR1`, `SIGUSR2`, `SIGTERM`].forEach((signal) => {
for (const signal of [`exit`, `SIGINT`, `SIGUSR1`, `SIGUSR2`, `SIGTERM`]) {
process.once(signal as NodeJS.Signals, () => {
pluginClient.unregisterPlugin(plugin)
.then(() => {
Logger.ok(`Successfully unregistered plugin '${options.pluginName}' from /${options.routeName} .`);
})
.catch((err: Error) => {
throw err;
});
}) ;
});
pluginClient
.unregisterPlugin(plugin)
.then(() => {
Logger.ok(`Successfully unregistered plugin '${options.pluginName}' from /${options.routeName} .`);
})
.catch((error: Error) => {
throw error;
});
});
}

View File

@@ -12,7 +12,7 @@
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {Plugin} from '@openstapps/api/lib/plugin.js';
import {Plugin} from '@openstapps/api';
import * as express from 'express';
import {SCMinimalRequest} from './protocol/request.js';
import {SCMinimalResponse} from './protocol/response.js';
@@ -24,7 +24,6 @@ import {SCMinimalResponse} from './protocol/response.js';
* TODO: rename the class
*/
export class MinimalPlugin extends Plugin {
/**
* Calculates the sum of a list of numbers
*

View File

@@ -1,3 +1,3 @@
{
"extends": "../../../node_modules/@openstapps/configuration/tsconfig.json"
"extends": "@openstapps/tsconfig"
}