refactor: read env variables via commander

This commit is contained in:
Rainer Killinger
2021-09-30 17:39:07 +02:00
parent 88909a5650
commit 03c2b8aeab

View File

@@ -17,7 +17,7 @@ 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 {Command, Option} from 'commander';
import {readFileSync} from 'fs';
import {join, resolve} from 'path';
import {MinimalPlugin} from './plugin/minimal-plugin';
@@ -34,26 +34,17 @@ const pluginVersion = JSON.parse(
const program = new Command()
.version(pluginVersion)
.option('-b, --backend-url <string>', 'URL of the StApps backend deployment', 'http://localhost:3000')
.option('-n, --plugin-name <string>', 'The name of the plugin', 'minimal-plugin') // TODO: adjust default
.option('-r, --route-name <string>', 'The name of the route', 'minimalplugin') // TODO: adjust default
.option('-u, --url <string>', 'The url of the plugin', 'http://localhost') // TODO: adjust default
.option('-p, --port <number>', 'The port of the plugin', '4000') // TODO: adjust default
// 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('-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('-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
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);