diff --git a/package-lock.json b/package-lock.json index a52e10aa3..1c2c47735 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@grnsft/if", - "version": "0.3.4", + "version": "0.4.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@grnsft/if", - "version": "0.3.4", + "version": "0.4.0", "license": "MIT", "dependencies": { "@commitlint/cli": "^18.6.0", diff --git a/package.json b/package.json index 60e33dc0f..c6db34155 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@grnsft/if", "description": "Impact Framework", - "version": "0.3.4", + "version": "0.4.0", "author": { "name": "Green Software Foundation", "email": "info@gsf.com" diff --git a/src/__tests__/unit/lib/load.test.ts b/src/__tests__/unit/lib/load.test.ts index f9ad3e73b..95ae6b26b 100644 --- a/src/__tests__/unit/lib/load.test.ts +++ b/src/__tests__/unit/lib/load.test.ts @@ -45,6 +45,8 @@ import {PARAMETERS} from '../../../config'; import {PluginParams} from '../../../types/interface'; import {STRINGS} from '../../../config'; +import {parseManifestFromStdin} from '../../../util/helpers'; +import {LoadDiffParams} from '../../../types/util/args'; const {INVALID_SOURCE} = STRINGS; @@ -109,8 +111,10 @@ describe('lib/load: ', () => { it('successfully loads target, and source from stdin.', async () => { process.env.readline = 'valid-source'; - const params = { + const piped = await parseManifestFromStdin(); + const params: LoadDiffParams = { targetPath: 'target-path.yml', + pipedSourceManifest: piped, }; const response = await loadIfDiffFiles(params); diff --git a/src/__tests__/unit/util/helpers.test.ts b/src/__tests__/unit/util/helpers.test.ts index d627790e4..58ed0525b 100644 --- a/src/__tests__/unit/util/helpers.test.ts +++ b/src/__tests__/unit/util/helpers.test.ts @@ -364,16 +364,11 @@ describe('util/helpers: ', () => { it('throws error if there is no manifest in stdin.', async () => { process.env.readline = 'no_manifest'; - const expectedMessage = 'Manifest not found in STDIN.'; expect.assertions(1); - try { - await parseManifestFromStdin(); - } catch (error) { - if (error instanceof Error) { - expect(error.message).toEqual(expectedMessage); - } - } + const response = await parseManifestFromStdin(); + + expect(response).toEqual(''); }); it('returns empty string if there is no data in stdin.', async () => { diff --git a/src/diff.ts b/src/diff.ts index 7deb09e35..71a57ed17 100644 --- a/src/diff.ts +++ b/src/diff.ts @@ -4,21 +4,27 @@ import {loadIfDiffFiles} from './lib/load'; import {compare} from './lib/compare'; import {parseIfDiffArgs} from './util/args'; -import {formatNotMatchingLog} from './util/helpers'; +import {formatNotMatchingLog, parseManifestFromStdin} from './util/helpers'; import {validateManifest} from './util/validations'; import {CONFIG} from './config'; import {logger} from './util/logger'; +import {debugLogger} from './util/debug-logger'; const {IF_DIFF} = CONFIG; const {SUCCESS_MESSAGE, FAILURE_MESSAGE} = IF_DIFF; const IfDiff = async () => { + const pipedSourceManifest = await parseManifestFromStdin(); const {sourcePath, targetPath} = parseIfDiffArgs(); + // Call this function with false parameter to prevent log debug messages. + debugLogger.overrideConsoleMethods(false); + const {rawSourceManifest, rawTargetManifest} = await loadIfDiffFiles({ targetPath, sourcePath, + pipedSourceManifest, }); const [sourceManifest, targetManifest] = [ rawSourceManifest, diff --git a/src/lib/load.ts b/src/lib/load.ts index 9b9508ded..2ed335b6f 100644 --- a/src/lib/load.ts +++ b/src/lib/load.ts @@ -3,7 +3,6 @@ import * as YAML from 'js-yaml'; import {ERRORS} from '../util/errors'; import {openYamlFileAsObject} from '../util/yaml'; import {readAndParseJson} from '../util/json'; -import {parseManifestFromStdin} from '../util/helpers'; import {PARAMETERS} from '../config'; import {STRINGS} from '../config'; @@ -41,8 +40,7 @@ export const load = async (inputPath: string, paramPath?: string) => { * Loads files to compare. As a source file checks if data is piped and then decides which one to take. */ export const loadIfDiffFiles = async (params: LoadDiffParams) => { - const {sourcePath, targetPath} = params; - const pipedSourceManifest = await parseManifestFromStdin(); + const {sourcePath, targetPath, pipedSourceManifest} = params; if (!sourcePath && !pipedSourceManifest) { throw new CliInputError(INVALID_SOURCE); diff --git a/src/types/util/args.ts b/src/types/util/args.ts index d9debf3cd..ae78fa4d0 100644 --- a/src/types/util/args.ts +++ b/src/types/util/args.ts @@ -1,4 +1,5 @@ export type LoadDiffParams = { sourcePath?: string; targetPath: string; + pipedSourceManifest?: string; }; diff --git a/src/util/helpers.ts b/src/util/helpers.ts index b15863cef..2b2dba87f 100644 --- a/src/util/helpers.ts +++ b/src/util/helpers.ts @@ -164,7 +164,7 @@ const collectPipedData = async () => { /** * Checks if there is piped data, tries to parse yaml from it. - * Throws error if there is piped info, but there is no valid manifest. + * Returns empty string if haven't found anything. */ export const parseManifestFromStdin = async () => { const pipedSourceManifest = await collectPipedData(); @@ -177,7 +177,7 @@ export const parseManifestFromStdin = async () => { const match = regex.exec(pipedSourceManifest); if (!match) { - throw new Error('Manifest not found in STDIN.'); + return ''; } return match![1]; diff --git a/src/util/validations.ts b/src/util/validations.ts index 3cd1dd6b9..21695f3ee 100644 --- a/src/util/validations.ts +++ b/src/util/validations.ts @@ -9,6 +9,7 @@ import {STRINGS} from '../config/strings'; const {ManifestValidationError, InputValidationError} = ERRORS; const {VALIDATING_MANIFEST} = STRINGS; + /** * At least one property defined handler. */ @@ -148,5 +149,6 @@ const flattenPath = (path: (string | number)[]): string => { const flattenPath = path.map(part => typeof part === 'number' ? `[${part}]` : part ); + return flattenPath.join('.'); };