Skip to content

Commit 3a028f3

Browse files
chore(src): fetch changes from main
2 parents e5f7642 + 7e3859f commit 3a028f3

File tree

2 files changed

+52
-86
lines changed

2 files changed

+52
-86
lines changed

src/__tests__/if-check/util/npm.test.ts

Lines changed: 16 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -11,37 +11,29 @@ jest.mock('../../../common/util/logger', () => ({
1111
},
1212
}));
1313

14-
jest.mock('../../../common/util/helpers', () => {
15-
const originalModule = jest.requireActual('../../../common/util/helpers');
14+
jest.mock('child_process', () => {
15+
const originalModule = jest.requireActual('child_process');
1616
return {
1717
...originalModule,
18-
execPromise: async (param: any) => {
18+
execFileSync: (file: any, args: any) => {
1919
switch (process.env.NPM_INSTALL) {
2020
case 'true':
21-
expect(param).toEqual('npm install @grnsft/if@0.3.3-beta.0');
21+
expect(file).toEqual('npm install @grnsft/if@0.3.3-beta.0');
2222
break;
2323
case 'npm init -y':
24-
expect(param).toEqual('npm init -y');
24+
expect(file).toEqual('npm init -y');
2525
break;
2626
case 'if-check':
27-
expect(param).toEqual(
28-
"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"
29-
);
30-
break;
31-
case 'if-check-cwd':
32-
expect(param).toEqual(
33-
"npm run if-env -- -m ./src/__mocks__/mock-manifest.yaml -c && 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"
34-
);
35-
break;
36-
case 'if-check-global':
37-
expect(param).toEqual(
38-
"if-env -m ./src/__mocks__/mock-manifest.yaml && if-run -m ./src/__mocks__/mock-manifest.yaml -o src/__mocks__/re-mock-manifest && node -p 'Boolean(process.stdout.isTTY)' | if-diff -s src/__mocks__/re-mock-manifest.yaml -t ./src/__mocks__/mock-manifest.yaml"
39-
);
40-
break;
41-
case 'if-check-prefix':
42-
expect(param).toEqual(
43-
"if-env --prefix=.. -m ./src/__mocks__/mock-manifest.yaml && if-run --prefix=.. -m ./src/__mocks__/mock-manifest.yaml -o src/__mocks__/re-mock-manifest && node -p 'Boolean(process.stdout.isTTY)' | if-diff --prefix=.. -s src/__mocks__/re-mock-manifest.yaml -t ./src/__mocks__/mock-manifest.yaml"
44-
);
27+
expect(
28+
[
29+
'npm run if-env -- -m ./src/__mocks__/mock-manifest.yaml',
30+
'npm run if-run -- -m ./src/__mocks__/mock-manifest.yaml -o src/__mocks__/re-mock-manifest',
31+
'node -p Boolean(process.stdout.isTTY)',
32+
'npm run if-diff -- -s src/__mocks__/re-mock-manifest.yaml -t ./src/__mocks__/mock-manifest.yaml',
33+
].includes(
34+
Array.isArray(args) ? `${file} ${args.join(' ')}` : file.trim()
35+
)
36+
).toBeTruthy();
4537
break;
4638
}
4739
return;
@@ -60,49 +52,7 @@ describe('if-check/util/npm: ', () => {
6052

6153
await executeCommands(manifest, false);
6254

63-
expect.assertions(2);
64-
expect(logSpy).toHaveBeenCalledWith(
65-
'✔ if-check successfully verified mock-manifest.yaml\n'
66-
);
67-
});
68-
69-
it('successfully executes with cwd command.', async () => {
70-
process.env.NPM_INSTALL = 'if-check-cwd';
71-
const manifest = './src/__mocks__/mock-manifest.yaml';
72-
const logSpy = jest.spyOn(global.console, 'log');
73-
74-
await executeCommands(manifest, true);
75-
76-
expect.assertions(2);
77-
expect(logSpy).toHaveBeenCalledWith(
78-
'✔ if-check successfully verified mock-manifest.yaml\n'
79-
);
80-
});
81-
82-
it('successfully executes with correct commands when is running from global.', async () => {
83-
process.env.npm_config_global = 'true';
84-
process.env.NPM_INSTALL = 'if-check-global';
85-
const manifest = './src/__mocks__/mock-manifest.yaml';
86-
const logSpy = jest.spyOn(global.console, 'log');
87-
88-
await executeCommands(manifest, false);
89-
90-
expect.assertions(2);
91-
expect(logSpy).toHaveBeenCalledWith(
92-
'✔ if-check successfully verified mock-manifest.yaml\n'
93-
);
94-
});
95-
96-
it('successfully executes with correct commands when CURRENT_DIR is provided.', async () => {
97-
process.env.CURRENT_DIR = './mock-path';
98-
process.env.npm_config_global = 'true';
99-
process.env.NPM_INSTALL = 'if-check-prefix';
100-
const manifest = './src/__mocks__/mock-manifest.yaml';
101-
const logSpy = jest.spyOn(global.console, 'log');
102-
103-
await executeCommands(manifest, false);
104-
105-
expect.assertions(2);
55+
expect.assertions(6);
10656
expect(logSpy).toHaveBeenCalledWith(
10757
'✔ if-check successfully verified mock-manifest.yaml\n'
10858
);

src/if-check/util/npm.ts

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as path from 'node:path';
2-
import {execPromise} from '../../common/util/helpers';
2+
import {execFileSync} from 'child_process';
33
import {getFileName, removeFileIfExists} from '../../common/util/fs';
44
import {STRINGS} from '../config';
55

@@ -28,47 +28,63 @@ export const executeCommands = async (manifest: string, cwd: boolean) => {
2828
const sanitizedExecutedManifest = escapeShellArg(executedManifest);
2929

3030
const ifEnvCommand = [
31-
isGlobal ? 'if-env' : 'npm run if-env',
31+
isGlobal ? 'if-env' : 'npm',
32+
...(isGlobal ? [] : ['run', 'if-env']),
3233
'--',
33-
...(prefixFlag === '' ? [] : prefixFlag),
34+
...(prefixFlag === '' ? [] : [prefixFlag]),
3435
'-m',
3536
sanitizedManifest,
3637
];
3738

3839
const ifRunCommand = [
39-
isGlobal ? 'if-run' : 'npm run if-run',
40+
isGlobal ? 'if-run' : 'npm',
41+
...(isGlobal ? [] : ['run', 'if-run']),
4042
'--',
41-
...(prefixFlag === '' ? [] : prefixFlag),
43+
...(prefixFlag === '' ? [] : [prefixFlag]),
4244
'-m',
4345
sanitizedManifest,
4446
'-o',
4547
sanitizedExecutedManifest,
4648
];
4749

48-
const ttyCommand = ['node', '-p', "'Boolean(process.stdout.isTTY)'"];
50+
const ttyCommand = ['node', '-p', 'Boolean(process.stdout.isTTY)'];
4951
const ifDiffCommand = [
50-
isGlobal ? 'if-diff' : 'npm run if-diff',
52+
isGlobal ? 'if-diff' : 'npm',
53+
...(isGlobal ? [] : ['run', 'if-diff']),
5154
'--',
52-
...(prefixFlag === '' ? [] : prefixFlag),
55+
...(prefixFlag === '' ? [] : [prefixFlag]),
5356
'-s',
5457
`${sanitizedExecutedManifest}.yaml`,
5558
'-t',
5659
sanitizedManifest,
5760
];
5861

59-
const fullCommand = [
60-
...ifEnvCommand,
61-
'&&',
62-
...ifRunCommand,
63-
'&&',
64-
...ttyCommand,
65-
'|',
66-
...ifDiffCommand,
67-
].join(' ');
68-
69-
// Execute the full command
70-
await execPromise(fullCommand, {
62+
execFileSync(ifEnvCommand[0], ifEnvCommand.slice(1), {
7163
cwd: process.env.CURRENT_DIR || process.cwd(),
64+
shell: true,
65+
});
66+
67+
execFileSync(ifRunCommand[0], ifRunCommand.slice(1), {
68+
cwd: process.env.CURRENT_DIR || process.cwd(),
69+
});
70+
71+
execFileSync(ttyCommand[0], ttyCommand.slice(1), {
72+
cwd: process.env.CURRENT_DIR || process.cwd(),
73+
});
74+
75+
const ttyResult = execFileSync(ttyCommand[0], ttyCommand.slice(1), {
76+
cwd: process.env.CURRENT_DIR || process.cwd(),
77+
});
78+
79+
const tty = ttyResult && ttyResult.toString().trim();
80+
const fullCommand = `${tty === 'true' ? 'tty |' : ''} ${ifDiffCommand.join(
81+
' '
82+
)}`;
83+
84+
execFileSync(fullCommand, {
85+
cwd: process.env.CURRENT_DIR || process.cwd(),
86+
stdio: 'inherit',
87+
shell: true,
7288
});
7389

7490
if (!cwd) {

0 commit comments

Comments
 (0)