From edc4520c5efbfeefaf8eba83be116d3cf6028011 Mon Sep 17 00:00:00 2001 From: Abhinav Bansal Date: Thu, 4 Apr 2024 01:26:12 +0530 Subject: [PATCH] feat(src): add --stdout flag Signed-off-by: Abhinav Bansal --- README.md | 5 ++++ src/__tests__/integration/scenarios/sci-e.ts | 30 +++++++++++-------- .../unit/builtins/export-log.test.ts | 6 ++-- src/builtins/export-log.ts | 5 ++-- src/config/config.ts | 7 +++++ src/index.ts | 4 +-- src/lib/exhaust.ts | 15 ++++++---- src/types/process-args.ts | 1 + src/util/args.ts | 2 ++ 9 files changed, 49 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 0b6b81699..a300a2603 100644 --- a/README.md +++ b/README.md @@ -71,6 +71,11 @@ Then, run `if` using the following command: ie --manifest ``` +Note that above command will not print the final output. In order to print the final output to the console, run `if` using the optional stdout argument: +```sh +ie --manifest --stdout +``` + You can also add an optional savepath for your output yaml (if you do not provide one, the output will be printed to the console): ```sh diff --git a/src/__tests__/integration/scenarios/sci-e.ts b/src/__tests__/integration/scenarios/sci-e.ts index 067bf19ee..30bdff898 100644 --- a/src/__tests__/integration/scenarios/sci-e.ts +++ b/src/__tests__/integration/scenarios/sci-e.ts @@ -4,9 +4,9 @@ import { npmInstallPackage, npmUninstallPackage, } from '../helpers/module-installer'; -import {execPromise, getJSONFromText} from '../helpers/common'; +import {execPromise} from '../helpers/common'; import {sciEInputData} from '../test-data/sci-e'; - +import * as YAML from 'js-yaml'; describe('integration/sci-e', () => { const modelName = 'sci-e'; const absoluteManifestPath = `${__dirname}/../manifest/sci-e.yaml`; @@ -24,7 +24,7 @@ describe('integration/sci-e', () => { method: 'SciE', path: '@grnsft/if-plugins', }; - file.initialize.outputs = ['log']; + file.initialize.outputs = []; file.tree.children.child.pipeline = [modelName]; file.tree.children.child.config = {}; @@ -33,23 +33,29 @@ describe('integration/sci-e', () => { await saveYamlFileAs(file, absoluteManifestPath); // save yaml uses absolute path const response = ( - await execPromise(`npm run ie -- --manifest ${relativeManifestPath}`) + await execPromise( + `npm run ie -- --manifest ${relativeManifestPath} --stdout` + ) ).stdout; // exec promise uses relative path - const finalOutputParsed = getJSONFromText(response); + const yamlPart = response.substring( + response.indexOf('name:'), + response.length + ); + const finalOutputParsed: any = YAML.load(yamlPart); // assertions - const path = finalOutputParsed.tree.children['child'].outputs![0]; - const manifestPath = file.tree.children['child'].inputs[0]; + const path = finalOutputParsed.tree.children.child.outputs[0]; + const manifestPath = file.tree.children.child.inputs[0]; // assert timestamp - expect( - finalOutputParsed.tree.children['child'].inputs[0].timestamp - ).toEqual(file.tree.children['child'].inputs[0].timestamp); + expect(finalOutputParsed.tree.children.child.inputs[0].timestamp).toEqual( + file.tree.children.child.inputs[0].timestamp + ); // assert duration - expect(finalOutputParsed.tree.children['child'].inputs[0].duration).toEqual( - file.tree.children['child'].inputs[0].duration + expect(finalOutputParsed.tree.children.child.inputs[0].duration).toEqual( + file.tree.children.child.inputs[0].duration ); // assert total energy diff --git a/src/__tests__/unit/builtins/export-log.test.ts b/src/__tests__/unit/builtins/export-log.test.ts index 7ff7aae8d..ebe3bb733 100644 --- a/src/__tests__/unit/builtins/export-log.test.ts +++ b/src/__tests__/unit/builtins/export-log.test.ts @@ -1,5 +1,5 @@ import {ExportLog} from '../../../builtins/export-log'; - +import * as YAML from 'js-yaml'; import {tree, context} from '../../../__mocks__/builtins/export-csv'; describe('builtins/export-log:', () => { @@ -10,7 +10,7 @@ describe('builtins/export-log:', () => { expect(mockConsoleLog).toHaveBeenCalled(); expect(mockConsoleLog).toHaveBeenCalledWith( - JSON.stringify({...context, tree}, null, 2) + YAML.dump({...context, tree}, {noRefs: true}) ); mockConsoleLog.mockRestore(); @@ -22,7 +22,7 @@ describe('builtins/export-log:', () => { expect(mockConsoleLog).toHaveBeenCalled(); expect(mockConsoleLog).toHaveBeenCalledWith( - JSON.stringify({...context, tree: {}}, null, 2) + YAML.dump({...context, tree: {}}, {noRefs: true}) ); mockConsoleLog.mockRestore(); diff --git a/src/builtins/export-log.ts b/src/builtins/export-log.ts index e8c9d9d0d..05ecef98d 100644 --- a/src/builtins/export-log.ts +++ b/src/builtins/export-log.ts @@ -1,5 +1,5 @@ import {Context} from '../types/manifest'; - +import * as YAML from 'js-yaml'; export const ExportLog = () => { /** * Logs output manifest in console. @@ -9,8 +9,7 @@ export const ExportLog = () => { ...context, tree, }; - - console.log(JSON.stringify(outputFile, null, 2)); + console.log(YAML.dump(outputFile, {noRefs: true})); }; return {execute}; diff --git a/src/config/config.ts b/src/config/config.ts index c213b29e0..d5c57f94f 100644 --- a/src/config/config.ts +++ b/src/config/config.ts @@ -35,6 +35,11 @@ export const CONFIG = { description: 'How much information to output about the calculation to aid investigation and debugging.', }, + stdout: { + type: Boolean, + optional: true, + description: 'Prints output to the console.', + }, help: { type: Boolean, optional: true, @@ -47,12 +52,14 @@ export const CONFIG = { -output [path to the output file] -format [yaml|csv] -verbose + --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. 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. `, }, GITHUB_PATH: 'https://github.com', diff --git a/src/index.ts b/src/index.ts index 9a5eebe42..2b7ca9fd1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -24,7 +24,7 @@ const impactEngine = async () => { const options = parseArgs(); if (options) { - const {inputPath, outputPath, paramPath} = options; + const {inputPath, outputPath, paramPath, stdout} = options; const {tree, context, parameters} = await load(inputPath, paramPath); parameterize.combine(context.params, parameters); @@ -32,7 +32,7 @@ const impactEngine = async () => { const computedTree = await compute(tree, {context, pluginStorage}); const aggregatedTree = aggregate(computedTree, context.aggregation); context['if-version'] = packageJson.version; - exhaust(aggregatedTree, context, outputPath); + exhaust(aggregatedTree, context, outputPath, stdout); return; } diff --git a/src/lib/exhaust.ts b/src/lib/exhaust.ts index c0e02d1d3..1854d0de4 100644 --- a/src/lib/exhaust.ts +++ b/src/lib/exhaust.ts @@ -33,8 +33,6 @@ const initializeExhaustPlugin = (name: string): ExhaustPluginInterface => { return ExportCSV(); case 'csv-raw': return ExportCSVRaw(); - case 'log': - return ExportLog(); default: throw new ModuleInitializationError(INVALID_EXHAUST_PLUGIN(name)); } @@ -44,12 +42,17 @@ const initializeExhaustPlugin = (name: string): ExhaustPluginInterface => { * Output manager - Exhaust. * Grabs output plugins from context, executes every. */ -export const exhaust = (tree: any, context: Context, outputPath?: string) => { +export const exhaust = ( + tree: any, + context: Context, + outputPath?: string, + stdout?: boolean +) => { const outputPlugins = context.initialize.outputs; - - if (!outputPlugins) { + if (stdout) { ExportLog().execute(tree, context); - + } + if (!outputPlugins) { return; } diff --git a/src/types/process-args.ts b/src/types/process-args.ts index f40ec7bab..d5cd14891 100644 --- a/src/types/process-args.ts +++ b/src/types/process-args.ts @@ -3,4 +3,5 @@ export interface ManifestProcessArgs { output?: string; 'override-params'?: string; help?: boolean; + stdout?: boolean; } diff --git a/src/util/args.ts b/src/util/args.ts index b2fcb07ef..76a061ef1 100644 --- a/src/util/args.ts +++ b/src/util/args.ts @@ -56,6 +56,7 @@ export const parseArgs = () => { output, 'override-params': overrideParams, help, + stdout, } = validateAndParseProcessArgs(); if (help) { @@ -67,6 +68,7 @@ export const parseArgs = () => { if (checkIfFileIsYaml(manifest)) { return { inputPath: prependFullFilePath(manifest), + stdout: stdout, ...(output && {outputPath: prependFullFilePath(output)}), ...(overrideParams && {paramPath: overrideParams}), };