diff --git a/src/__tests__/if-check/util/npm.test.ts b/src/__tests__/if-check/util/npm.test.ts index 816dcc71..966717c7 100644 --- a/src/__tests__/if-check/util/npm.test.ts +++ b/src/__tests__/if-check/util/npm.test.ts @@ -11,22 +11,29 @@ jest.mock('../../../common/util/logger', () => ({ }, })); -jest.mock('../../../common/util/helpers', () => { - const originalModule = jest.requireActual('../../../common/util/helpers'); +jest.mock('child_process', () => { + const originalModule = jest.requireActual('child_process'); return { ...originalModule, - execPromise: async (param: any) => { + execFileSync: (file: any, args: any) => { switch (process.env.NPM_INSTALL) { case 'true': - expect(param).toEqual('npm install @grnsft/if@0.3.3-beta.0'); + expect(file).toEqual('npm install @grnsft/if@0.3.3-beta.0'); break; case 'npm init -y': - expect(param).toEqual('npm init -y'); + expect(file).toEqual('npm init -y'); break; case 'if-check': - expect(param).toEqual( - "npm run if-env -- -m ./src/__mocks__/mock-manifest.yaml && npm run if-run -- -m ./src/__mocks__/mock-manifest.yaml -o src/__mocks__/re-mock-manifest && node -p 'Boolean(process.stdout.isTTY)' | npm run if-diff -- -s src/__mocks__/re-mock-manifest.yaml -t ./src/__mocks__/mock-manifest.yaml" - ); + expect( + [ + 'npm run if-env -- -m ./src/__mocks__/mock-manifest.yaml', + 'npm run if-run -- -m ./src/__mocks__/mock-manifest.yaml -o src/__mocks__/re-mock-manifest', + 'node -p Boolean(process.stdout.isTTY)', + 'npm run if-diff -- -s src/__mocks__/re-mock-manifest.yaml -t ./src/__mocks__/mock-manifest.yaml', + ].includes( + Array.isArray(args) ? `${file} ${args.join(' ')}` : file.trim() + ) + ).toBeTruthy(); break; case 'if-check-cwd': expect(param).toEqual( @@ -60,7 +67,7 @@ describe('if-check/util/npm: ', () => { await executeCommands(manifest, false); - expect.assertions(2); + expect.assertions(6); expect(logSpy).toHaveBeenCalledWith( '✔ if-check successfully verified mock-manifest.yaml\n' ); diff --git a/src/if-check/util/npm.ts b/src/if-check/util/npm.ts index 9717819f..7688e839 100644 --- a/src/if-check/util/npm.ts +++ b/src/if-check/util/npm.ts @@ -1,5 +1,5 @@ import * as path from 'node:path'; -import {execPromise} from '../../common/util/helpers'; +import {execFileSync} from 'child_process'; import {getFileName, removeFileIfExists} from '../../common/util/fs'; import {STRINGS} from '../config'; @@ -28,47 +28,63 @@ export const executeCommands = async (manifest: string, cwd: boolean) => { const sanitizedExecutedManifest = escapeShellArg(executedManifest); const ifEnvCommand = [ - isGlobal ? 'if-env' : 'npm run if-env', + isGlobal ? 'if-env' : 'npm', + ...(isGlobal ? [] : ['run', 'if-env']), '--', - ...(prefixFlag === '' ? [] : prefixFlag), + ...(prefixFlag === '' ? [] : [prefixFlag]), '-m', sanitizedManifest, ]; const ifRunCommand = [ - isGlobal ? 'if-run' : 'npm run if-run', + isGlobal ? 'if-run' : 'npm', + ...(isGlobal ? [] : ['run', 'if-run']), '--', - ...(prefixFlag === '' ? [] : prefixFlag), + ...(prefixFlag === '' ? [] : [prefixFlag]), '-m', sanitizedManifest, '-o', sanitizedExecutedManifest, ]; - const ttyCommand = ['node', '-p', "'Boolean(process.stdout.isTTY)'"]; + const ttyCommand = ['node', '-p', 'Boolean(process.stdout.isTTY)']; const ifDiffCommand = [ - isGlobal ? 'if-diff' : 'npm run if-diff', + isGlobal ? 'if-diff' : 'npm', + ...(isGlobal ? [] : ['run', 'if-diff']), '--', - ...(prefixFlag === '' ? [] : prefixFlag), + ...(prefixFlag === '' ? [] : [prefixFlag]), '-s', `${sanitizedExecutedManifest}.yaml`, '-t', sanitizedManifest, ]; - const fullCommand = [ - ...ifEnvCommand, - '&&', - ...ifRunCommand, - '&&', - ...ttyCommand, - '|', - ...ifDiffCommand, - ].join(' '); - - // Execute the full command - await execPromise(fullCommand, { + execFileSync(ifEnvCommand[0], ifEnvCommand.slice(1), { cwd: process.env.CURRENT_DIR || process.cwd(), + shell: true, + }); + + execFileSync(ifRunCommand[0], ifRunCommand.slice(1), { + cwd: process.env.CURRENT_DIR || process.cwd(), + }); + + execFileSync(ttyCommand[0], ttyCommand.slice(1), { + cwd: process.env.CURRENT_DIR || process.cwd(), + }); + + const ttyResult = execFileSync(ttyCommand[0], ttyCommand.slice(1), { + cwd: process.env.CURRENT_DIR || process.cwd(), + }); + + const tty = ttyResult && ttyResult.toString().trim(); + const fullCommand = `${tty === 'true' ? 'tty |' : ''} ${ifDiffCommand.join( + ' ' + )}`; + + execFileSync(fullCommand, { + cwd: process.env.CURRENT_DIR || process.cwd(), + stdio: 'inherit', + shell: true, }); if (!cwd) {