From 075864932984d5a92b83e582d9b81136f5798506 Mon Sep 17 00:00:00 2001 From: Narek Hovhannisyan Date: Sat, 4 May 2024 17:14:41 +0400 Subject: [PATCH 01/10] feat(config): introduce CLI aliases --- src/config/config.ts | 31 ++++++++----------------------- 1 file changed, 8 insertions(+), 23 deletions(-) diff --git a/src/config/config.ts b/src/config/config.ts index dfb2dc1a7..c27faf2ca 100644 --- a/src/config/config.ts +++ b/src/config/config.ts @@ -8,36 +8,25 @@ export const CONFIG = { manifest: { type: String, optional: true, - alias: 'i', + alias: 'm', description: 'Path to an input manifest file.', }, output: { type: String, optional: true, - description: - 'Path to the output file where the results as saved, if none is provided it prints to stdout.', + alias: 'o', + description: 'Path to the output file where the results as saved.', }, 'override-params': { type: String, optional: true, + alias: 'op', description: 'Path to a parameter file that overrides our defaults.', }, - format: { - type: String, - optional: true, - description: - 'The output file format. default to yaml but if csv is specified then it formats the outputs as a csv file for loading into another program.', - defaultValue: 'yaml', - }, - verbose: { - type: Boolean, - optional: true, - description: - 'How much information to output about the calculation to aid investigation and debugging.', - }, stdout: { type: Boolean, optional: true, + alias: 's', description: 'Prints output to the console.', }, help: { @@ -48,16 +37,12 @@ export const CONFIG = { }, } as ArgumentConfig, HELP: `impact - -manifest [path to the input file] - -output [path to the output file] - -format [yaml|csv] - -verbose + --manifest [path to the input file] + --output [path to the output file] --stdout - -help + --help manifest: path to an input manifest output: path to the output file where the results as saved, if none is provided it prints to stdout. - format: the output file format. default to yaml but if csv is specified then it formats the outputs as a csv file for loading into another program. - verbose: how much information to output about the calculation to aid investigation and debugging. help: prints out the above help instruction. stdout: Prints output to the console. `, From 50b587c4ecc31641d7ec1edc8ba7351397c7db18 Mon Sep 17 00:00:00 2001 From: Narek Hovhannisyan Date: Tue, 7 May 2024 17:33:21 +0400 Subject: [PATCH 02/10] fix(util): drop help flag checker --- src/util/args.ts | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/src/util/args.ts b/src/util/args.ts index 15375d686..3f9a1c4a5 100644 --- a/src/util/args.ts +++ b/src/util/args.ts @@ -11,17 +11,17 @@ import {ManifestProcessArgs, ProcessArgsOutputs} from '../types/process-args'; const {CliInputError} = ERRORS; -const {impact} = CONFIG; -const {ARGS, HELP, NO_OUTPUT} = impact; +const {IE} = CONFIG; +const {ARGS, HELP} = IE; -const {FILE_IS_NOT_YAML, MANIFEST_IS_MISSING} = STRINGS; +const {FILE_IS_NOT_YAML, MANIFEST_IS_MISSING, NO_OUTPUT} = STRINGS; /** * Validates process arguments */ const validateAndParseProcessArgs = () => { try { - return parse(ARGS); + return parse(ARGS, HELP); } catch (error) { if (error instanceof Error) { throw new CliInputError(error.message); @@ -57,15 +57,9 @@ export const parseArgs = (): ProcessArgsOutputs | undefined => { manifest, output, 'override-params': overrideParams, - help, stdout, } = validateAndParseProcessArgs(); - if (help) { - console.info(HELP); - return; - } - if (!output && !stdout) { logger.warn(NO_OUTPUT); } From be4ac52383d0db1ef7d21c63fe75d27c33e3d66d Mon Sep 17 00:00:00 2001 From: Narek Hovhannisyan Date: Tue, 7 May 2024 17:36:56 +0400 Subject: [PATCH 03/10] fix(types): drop help property from manifest args --- src/types/process-args.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/types/process-args.ts b/src/types/process-args.ts index 5dbbbb837..80a5163aa 100644 --- a/src/types/process-args.ts +++ b/src/types/process-args.ts @@ -2,7 +2,6 @@ export interface ManifestProcessArgs { manifest?: string; output?: string; 'override-params'?: string; - help?: boolean; stdout?: boolean; } From b4bf85cd0c2ce56eba78be8ddc5a5d439b187af6 Mon Sep 17 00:00:00 2001 From: Narek Hovhannisyan Date: Tue, 7 May 2024 17:38:57 +0400 Subject: [PATCH 04/10] fix(config): remove redundant [!important], add no output message --- src/config/strings.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/config/strings.ts b/src/config/strings.ts index 5781bc5e2..5bfb7d134 100644 --- a/src/config/strings.ts +++ b/src/config/strings.ts @@ -10,13 +10,13 @@ export const STRINGS = { OVERRIDE_WARNING: '\n**WARNING**: You are overriding the IF default parameters file. Please be extremely careful of unintended side-effects in your plugin pipeline!\n', DISCLAIMER_MESSAGE: ` -[!important] Incubation Project +Incubation Project This project is an incubation project being run inside the Green Software Foundation; as such, we DON’T recommend using it in any critical use case. Incubation projects are experimental, offer no support guarantee, have minimal governance and process, and may be retired at any moment. This project may one day graduate, in which case this disclaimer will be removed.`, NOT_NATIVE_PLUGIN: (path: string) => ` -[!important] You are using plugin ${path} which is not part of the Impact Framework standard library. You should do your own research to ensure the plugins are up to date and accurate. They may not be actively maintained.`, +You are using plugin ${path} which is not part of the Impact Framework standard library. You should do your own research to ensure the plugins are up to date and accurate. They may not be actively maintained.`, SOMETHING_WRONG: 'Something wrong with cli arguments. Please check docs.', ISSUE_TEMPLATE: ` Impact Framework is an alpha release from the Green Software Foundation and is released to capture early feedback. If you'd like to offer some feedback, please use this issue template: @@ -47,4 +47,9 @@ https://github.com/Green-Software-Foundation/if/issues/new?assignees=&labels=fee `Unknown parameter: ${name}. Using 'sum' aggregation method.`, NOT_INITALIZED_PLUGIN: (name: string) => `Not initalized plugin: ${name}. Check if ${name} is in 'manifest.initalize.plugins'.`, + NO_OUTPUT: ` +You have not selected an output method. To see your output data, you can choose from: +--stdout: this will print your output data to the console +--output : this will save your output data to the given filepath (do not provide file extension) +Note that for the '--output' option you also need to define the output type in your manifest file. See https://if.greensoftware.foundation/major-concepts/manifest-file#initialize`, }; From 0ecbf0f458f055e9518ee49a7a4b82c698d2b0b8 Mon Sep 17 00:00:00 2001 From: Narek Hovhannisyan Date: Tue, 7 May 2024 17:41:54 +0400 Subject: [PATCH 05/10] feat(config): optimize CLI args --- src/config/config.ts | 44 +++++++++++++++++++++----------------------- 1 file changed, 21 insertions(+), 23 deletions(-) diff --git a/src/config/config.ts b/src/config/config.ts index c27faf2ca..9a3f8d57e 100644 --- a/src/config/config.ts +++ b/src/config/config.ts @@ -1,56 +1,54 @@ -import {ArgumentConfig} from 'ts-command-line-args'; +import {ArgumentConfig, ParseOptions} from 'ts-command-line-args'; + +import {STRINGS} from './strings'; import {ManifestProcessArgs} from '../types/process-args'; +const {DISCLAIMER_MESSAGE} = STRINGS; + export const CONFIG = { - impact: { + IE: { ARGS: { manifest: { type: String, optional: true, alias: 'm', - description: 'Path to an input manifest file.', + description: '[path to the input file]', }, output: { type: String, optional: true, alias: 'o', - description: 'Path to the output file where the results as saved.', + description: '[path to the output file]', }, 'override-params': { type: String, optional: true, - alias: 'op', - description: 'Path to a parameter file that overrides our defaults.', + alias: 'p', + description: '[path to a parameter file that overrides our defaults]', }, stdout: { type: Boolean, optional: true, alias: 's', - description: 'Prints output to the console.', + description: '[prints out to the console]', }, help: { type: Boolean, optional: true, alias: 'h', - description: 'Prints this usage guide.', + description: '[prints out the above help instruction]', }, } as ArgumentConfig, - HELP: `impact - --manifest [path to the input file] - --output [path to the output file] - --stdout - --help - manifest: path to an input manifest - output: path to the output file where the results as saved, if none is provided it prints to stdout. - help: prints out the above help instruction. - stdout: Prints output to the console. - `, - NO_OUTPUT: ` -You have not selected an output method. To see your output data, you can choose from: ---stdout: this will print your output data to the console ---output : this will save your output data to the given filepath (do not provide file extension) -Note that for the '--output' option you also need to define the output type in your manifest file. See https://if.greensoftware.foundation/major-concepts/manifest-file#initialize`, + HELP: { + helpArg: 'help', + headerContentSections: [ + {header: 'Impact Framework', content: 'Helpful keywords:'}, + ], + footerContentSections: [ + {header: 'Green Software Foundation', content: DISCLAIMER_MESSAGE}, + ], + } as ParseOptions, }, GITHUB_PATH: 'https://github.com', NATIVE_PLUGIN: 'if-plugins', From ce9b4d7e45bad50c9f9b52ac06f29b2b583d30ac Mon Sep 17 00:00:00 2001 From: Narek Hovhannisyan Date: Tue, 7 May 2024 17:42:22 +0400 Subject: [PATCH 06/10] test(util): fix strings and args --- src/__tests__/unit/config/strings.test.ts | 2 +- src/__tests__/unit/util/args.test.ts | 17 +---------------- 2 files changed, 2 insertions(+), 17 deletions(-) diff --git a/src/__tests__/unit/config/strings.test.ts b/src/__tests__/unit/config/strings.test.ts index 6315edeb6..bdc1f871f 100644 --- a/src/__tests__/unit/config/strings.test.ts +++ b/src/__tests__/unit/config/strings.test.ts @@ -19,7 +19,7 @@ describe('config/strings: ', () => { it('successfully injects path into message.', () => { const path = 'mock/path'; const expectedMessage = ` -[!important] You are using plugin ${path} which is not part of the Impact Framework standard library. You should do your own research to ensure the plugins are up to date and accurate. They may not be actively maintained.`; +You are using plugin ${path} which is not part of the Impact Framework standard library. You should do your own research to ensure the plugins are up to date and accurate. They may not be actively maintained.`; expect(NOT_NATIVE_PLUGIN(path)).toEqual(expectedMessage); }); diff --git a/src/__tests__/unit/util/args.test.ts b/src/__tests__/unit/util/args.test.ts index bbbc0e0f3..d3c5d7364 100644 --- a/src/__tests__/unit/util/args.test.ts +++ b/src/__tests__/unit/util/args.test.ts @@ -51,16 +51,12 @@ import path = require('path'); import {parseArgs} from '../../../util/args'; import {ERRORS} from '../../../util/errors'; -import {STRINGS, CONFIG} from '../../../config'; -const {impact} = CONFIG; -const {HELP} = impact; +import {STRINGS} from '../../../config'; const {CliInputError} = ERRORS; const {MANIFEST_IS_MISSING, FILE_IS_NOT_YAML} = STRINGS; -const info = jest.spyOn(console, 'info').mockImplementation(() => {}); - describe('util/args: ', () => { const originalEnv = process.env; @@ -143,17 +139,6 @@ describe('util/args: ', () => { expect(result).toEqual(expectedResult); }); - it('returns manifest with help.', () => { - expect.assertions(2); - - process.env.result = 'help'; - - const result = parseArgs(); - - expect(info).toHaveBeenCalledWith(HELP); - expect(result).toBeUndefined(); - }); - it('returns manifest and output path.', () => { expect.assertions(1); From f0a6f557aa8a022d4b9962481f2bc99edba46d47 Mon Sep 17 00:00:00 2001 From: Narek Hovhannisyan Date: Tue, 7 May 2024 20:13:11 +0400 Subject: [PATCH 07/10] fix(util): args return type --- src/util/args.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/util/args.ts b/src/util/args.ts index 3f9a1c4a5..6bffad5ed 100644 --- a/src/util/args.ts +++ b/src/util/args.ts @@ -52,7 +52,7 @@ const prependFullFilePath = (filePath: string) => { * If it is, then returns object containing full path. * 4. If params are missing or invalid, then rejects with `CliInputError`. */ -export const parseArgs = (): ProcessArgsOutputs | undefined => { +export const parseArgs = (): ProcessArgsOutputs => { const { manifest, output, From 0b5f29d1703986bf77d3b4c9a11e884718d6c6cc Mon Sep 17 00:00:00 2001 From: Narek Hovhannisyan Date: Tue, 7 May 2024 20:13:43 +0400 Subject: [PATCH 08/10] fix(types): make input path required --- src/types/process-args.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/types/process-args.ts b/src/types/process-args.ts index 80a5163aa..3ba4eab31 100644 --- a/src/types/process-args.ts +++ b/src/types/process-args.ts @@ -11,7 +11,7 @@ export interface Options { } export interface ProcessArgsOutputs { - inputPath?: string; + inputPath: string; outputOptions: { outputPath?: string; stdout?: boolean; From a6f044c804eb4b51607d6b27d136d792b4e6715d Mon Sep 17 00:00:00 2001 From: Narek Hovhannisyan Date: Tue, 7 May 2024 20:14:51 +0400 Subject: [PATCH 09/10] fix(src): remove redundant check --- src/index.ts | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/index.ts b/src/index.ts index 0191f24f3..2fff764f9 100644 --- a/src/index.ts +++ b/src/index.ts @@ -18,14 +18,10 @@ const {DISCLAIMER_MESSAGE} = STRINGS; const impactEngine = async () => { const options = parseArgs(); - if (!options) { - return; - } - logger.info(DISCLAIMER_MESSAGE); const {inputPath, paramPath, outputOptions} = options; - const {tree, rawContext, parameters} = await load(inputPath!, paramPath); + const {tree, rawContext, parameters} = await load(inputPath, paramPath); const context = await injectEnvironment(rawContext); parameterize.combine(context.params, parameters); const pluginStorage = await initalize(context.initialize.plugins); From 59d8d0c69a6d84593c97200f83e244135f633ffd Mon Sep 17 00:00:00 2001 From: Narek Hovhannisyan Date: Tue, 7 May 2024 20:19:38 +0400 Subject: [PATCH 10/10] fix(src): remove return at the end --- src/index.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index 2fff764f9..11fa4e928 100644 --- a/src/index.ts +++ b/src/index.ts @@ -28,8 +28,6 @@ const impactEngine = async () => { const computedTree = await compute(tree, {context, pluginStorage}); const aggregatedTree = aggregate(computedTree, context.aggregation); exhaust(aggregatedTree, context, outputOptions); - - return; }; impactEngine().catch(andHandle);