Skip to content

Commit

Permalink
feat(src): add --stdout flag
Browse files Browse the repository at this point in the history
Signed-off-by: Abhinav Bansal <[email protected]>
  • Loading branch information
abhinavbansal19961996 committed Apr 3, 2024
1 parent 1d6509f commit 27cc1fc
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 26 deletions.
30 changes: 18 additions & 12 deletions src/__tests__/integration/scenarios/sci-e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`;
Expand All @@ -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 = {};
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/__tests__/unit/builtins/export-log.test.ts
Original file line number Diff line number Diff line change
@@ -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:', () => {
Expand All @@ -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();
Expand All @@ -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();
Expand Down
5 changes: 2 additions & 3 deletions src/builtins/export-log.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Context} from '../types/manifest';

import * as YAML from 'js-yaml';
export const ExportLog = () => {
/**
* Logs output manifest in console.
Expand All @@ -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};
Expand Down
7 changes: 7 additions & 0 deletions src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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',
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ 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);
const pluginStorage = await initalize(context.initialize.plugins);
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;
}
Expand Down
15 changes: 9 additions & 6 deletions src/lib/exhaust.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand All @@ -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;
}

Expand Down
1 change: 1 addition & 0 deletions src/types/process-args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ export interface ManifestProcessArgs {
output?: string;
'override-params'?: string;
help?: boolean;
stdout?: boolean;
}
2 changes: 2 additions & 0 deletions src/util/args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export const parseArgs = () => {
output,
'override-params': overrideParams,
help,
stdout,
} = validateAndParseProcessArgs();

if (help) {
Expand All @@ -67,6 +68,7 @@ export const parseArgs = () => {
if (checkIfFileIsYaml(manifest)) {
return {
inputPath: prependFullFilePath(manifest),
stdout: stdout,
...(output && {outputPath: prependFullFilePath(output)}),
...(overrideParams && {paramPath: overrideParams}),
};
Expand Down

0 comments on commit 27cc1fc

Please sign in to comment.