Skip to content

Commit 50d9f2c

Browse files
author
John Doe
committed
refactor: wip
1 parent 0a8af75 commit 50d9f2c

File tree

2 files changed

+91
-4
lines changed

2 files changed

+91
-4
lines changed

packages/nx-plugin/src/executors/cli/executor.ts

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,37 @@ export default async function runAutorunExecutor(
2222
...argsObj
2323
} = terminalAndExecutorOptions;
2424
const command = bin ? `node` : 'npx';
25+
26+
// Extract --import flags from NODE_OPTIONS if present (Node.js doesn't allow --import in NODE_OPTIONS)
27+
let importArgs: string[] = [];
28+
const envVariables = { ...env };
29+
30+
if (bin && env?.NODE_OPTIONS) {
31+
const nodeOptions = env.NODE_OPTIONS;
32+
// Match --import <value> or --import=<value>
33+
const importMatch = nodeOptions.match(/--import(?:\s+|=)(\S+)/);
34+
if (importMatch && importMatch[1]) {
35+
importArgs = ['--import', importMatch[1]];
36+
// Remove --import flag from NODE_OPTIONS
37+
const cleaned = nodeOptions.replace(/--import(?:\s+|=)\S+/g, '').trim();
38+
if (cleaned) {
39+
envVariables.NODE_OPTIONS = cleaned;
40+
} else {
41+
delete envVariables.NODE_OPTIONS;
42+
}
43+
}
44+
}
45+
2546
const positionals = [
47+
...importArgs, // Add --import flags before the script when using node
2648
bin ?? '@code-pushup/cli',
2749
...(cliCommand ? [cliCommand] : []),
2850
];
2951
const args = objectToCliArgs(argsObj);
30-
const envVariables = {
31-
...env,
32-
...(verbose && { CP_VERBOSE: 'true' }),
33-
};
52+
53+
if (verbose) {
54+
envVariables.CP_VERBOSE = 'true';
55+
}
3456

3557
const { logger, stringifyError, formatCommand } = await import(
3658
'@code-pushup/utils'

packages/nx-plugin/src/executors/cli/executor.unit.test.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,4 +178,69 @@ describe('runAutorunExecutor', () => {
178178
expect(warnMessage).toContain('DryRun execution of:');
179179
expect(warnMessage).toContain('npx @code-pushup/cli');
180180
});
181+
182+
it('should extract --import from NODE_OPTIONS and pass as direct argument when bin is set', async () => {
183+
const { command } = await runAutorunExecutor(
184+
{
185+
bin: 'packages/cli/src/index.ts',
186+
env: {
187+
NODE_OPTIONS: '--import tsx',
188+
TSX_TSCONFIG_PATH: 'tsconfig.base.json',
189+
},
190+
},
191+
executorContext('utils'),
192+
);
193+
194+
const commandWithoutAnsi = removeColorCodes(command || '');
195+
expect(commandWithoutAnsi).toMatch(
196+
'node --import tsx packages/cli/src/index.ts',
197+
);
198+
expect(executeProcessSpy).toHaveBeenCalledWith(
199+
expect.objectContaining({
200+
command: 'node',
201+
args: expect.arrayContaining([
202+
'--import',
203+
'tsx',
204+
'packages/cli/src/index.ts',
205+
]),
206+
env: expect.objectContaining({
207+
TSX_TSCONFIG_PATH: 'tsconfig.base.json',
208+
}),
209+
}),
210+
);
211+
// NODE_OPTIONS should be removed since it only contained --import
212+
expect(executeProcessSpy.mock.calls[0]?.[0]?.env).not.toHaveProperty(
213+
'NODE_OPTIONS',
214+
);
215+
});
216+
217+
it('should preserve other NODE_OPTIONS when extracting --import', async () => {
218+
const { command } = await runAutorunExecutor(
219+
{
220+
bin: 'packages/cli/src/index.ts',
221+
env: {
222+
NODE_OPTIONS: '--max-old-space-size=4096 --import tsx',
223+
},
224+
},
225+
executorContext('utils'),
226+
);
227+
228+
const commandWithoutAnsi = removeColorCodes(command || '');
229+
expect(commandWithoutAnsi).toMatch(
230+
'node --import tsx packages/cli/src/index.ts',
231+
);
232+
expect(executeProcessSpy).toHaveBeenCalledWith(
233+
expect.objectContaining({
234+
command: 'node',
235+
args: expect.arrayContaining([
236+
'--import',
237+
'tsx',
238+
'packages/cli/src/index.ts',
239+
]),
240+
env: expect.objectContaining({
241+
NODE_OPTIONS: '--max-old-space-size=4096',
242+
}),
243+
}),
244+
);
245+
});
181246
});

0 commit comments

Comments
 (0)