From 03c2b8aeab935a1eb7a84235fb980051e717ee7d Mon Sep 17 00:00:00 2001 From: Rainer Killinger Date: Thu, 30 Sep 2021 17:39:07 +0200 Subject: [PATCH] refactor: read env variables via commander --- src/cli.ts | 25 ++++++++----------------- 1 file changed, 8 insertions(+), 17 deletions(-) diff --git a/src/cli.ts b/src/cli.ts index a19ec916..db4d02c7 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -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 ', '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 + // tslint:disable: newline-per-chained-call + .addOption(new Option('-b, --backend-url ', 'URL of the StApps backend deployment').default('http://localhost:3000').env('STAPPS_BACKEND')) + .addOption(new Option('-n, --plugin-name ', 'The name of the plugin').default('minimal-plugin')) // TODO: adjust default + .addOption(new Option('-r, --route-name ', 'The name of the route').default('minimalplugin')) // TODO: adjust default + .addOption(new Option('-u, --url ', 'The url of the plugin').default('http://localhost').env('PLUGIN_URL')) // TODO: adjust default + .addOption(new Option('-p, --port ', '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);