Skip to content

Commit bebae7c

Browse files
committed
refactor: wip
1 parent 2041093 commit bebae7c

File tree

1 file changed

+73
-4
lines changed

1 file changed

+73
-4
lines changed

packages/nx-plugin/src/internal/execute-process.ts

Lines changed: 73 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,64 @@ export function calcDuration(start: number, stop?: number): number {
77
return Math.round((stop ?? performance.now()) - start);
88
}
99

10+
/**
11+
* Processes Node.js specific environment variables that can't be passed via NODE_OPTIONS.
12+
* Extracts flags like --import from NODE_OPTIONS and adds them directly to the command arguments.
13+
*/
14+
function processNodeOptions({
15+
command,
16+
args,
17+
env,
18+
}: {
19+
command: string;
20+
args: string[];
21+
env?: Record<string, string>;
22+
}): {
23+
processedCommand: string;
24+
processedArgs: string[];
25+
processedEnv?: Record<string, string>;
26+
} {
27+
if (!env || command !== 'node') {
28+
return {
29+
processedCommand: command,
30+
processedArgs: args,
31+
processedEnv: env,
32+
};
33+
}
34+
35+
const processedEnv = { ...env };
36+
const processedArgs = [...args];
37+
38+
// Handle NODE_OPTIONS that contain flags not allowed in environment variables
39+
if (processedEnv.NODE_OPTIONS) {
40+
const nodeOptions = processedEnv.NODE_OPTIONS;
41+
42+
// Extract --import flag which is not allowed in NODE_OPTIONS
43+
const importMatch = nodeOptions.match(/--import[=\s]+([^\s]+)/);
44+
if (importMatch) {
45+
// Add --import flag directly to node arguments
46+
processedArgs.unshift(`--import=${importMatch[1]}`);
47+
48+
// Remove --import from NODE_OPTIONS
49+
processedEnv.NODE_OPTIONS = nodeOptions
50+
.replace(/--import[=\s]+[^\s]+/, '')
51+
.trim();
52+
53+
// If NODE_OPTIONS is now empty, remove it entirely
54+
if (!processedEnv.NODE_OPTIONS) {
55+
delete processedEnv.NODE_OPTIONS;
56+
}
57+
}
58+
}
59+
60+
return {
61+
processedCommand: command,
62+
processedArgs: processedArgs,
63+
processedEnv:
64+
Object.keys(processedEnv).length > 0 ? processedEnv : undefined,
65+
};
66+
}
67+
1068
/**
1169
* Represents the process result.
1270
* @category Types
@@ -154,12 +212,19 @@ export function executeProcess(cfg: ProcessConfig): Promise<ProcessResult> {
154212
const date = new Date().toISOString();
155213
const start = performance.now();
156214

215+
// Handle Node.js specific environment variables that can't be passed via NODE_OPTIONS
216+
const { processedCommand, processedArgs, processedEnv } = processNodeOptions({
217+
command,
218+
args: args ?? [],
219+
env,
220+
});
221+
157222
ui().logger.log(
158223
gray(
159224
`Executing command:\n${formatCommandLog({
160-
command,
161-
args: args ?? [],
162-
env,
225+
command: processedCommand,
226+
args: processedArgs,
227+
env: processedEnv,
163228
})}\nIn working directory:\n${cfg.cwd ?? process.cwd()}`,
164229
),
165230
);
@@ -176,7 +241,11 @@ export function executeProcess(cfg: ProcessConfig): Promise<ProcessResult> {
176241

177242
return new Promise((resolve, reject) => {
178243
// shell:true tells Windows to use shell command for spawning a child process
179-
const process = spawn(command, args, { cwd, shell: true, env });
244+
const process = spawn(processedCommand, processedArgs, {
245+
cwd,
246+
shell: true,
247+
env: processedEnv,
248+
});
180249
// eslint-disable-next-line functional/no-let
181250
let stdout = '';
182251
// eslint-disable-next-line functional/no-let

0 commit comments

Comments
 (0)