Remove the usage of exec for variable cases#33544
Conversation
There was a problem hiding this comment.
Pull request overview
This PR aims to address ESLint security/detect-child-process warnings by replacing child_process.exec usage (especially with variable input) with child_process.spawn in demo utility scripts.
Changes:
- Switched TypeScript compilation in the TS→JS converter from
exec("tsc ...")tospawn("tsc", [...]). - Switched Angular demo build invocation from
exec("npm run ...")tospawn("npm", [...]).
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| apps/demos/utils/ts-to-js-converter/converter.ts | Replaces exec-based tsc invocation with spawn and updates call site to pass arguments as an array. |
| apps/demos/utils/create-bundles/Angular/bundler.ts | Replaces exec-based Angular build command with spawn('npm', [...]). |
Comments suppressed due to low confidence (1)
apps/demos/utils/create-bundles/Angular/bundler.ts:55
spawn('npm', ...)should handle the child processerrorevent. If spawning fails, the unhandlederrorevent can crash the process andres()will never be called (hanging the batch). Add anngBuildProcess.on('error', ...)handler (and consider treating non-zero exit codes as failures rather than always callingres()as success).
const ngBuildProcess = spawn('npm', ['run', 'build-angular', '--', getProjectNameByDemo(demo)]);
ngBuildProcess.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
});
ngBuildProcess.stderr.on('data', (data) => {
console.error(`stderr: ${data}`);
});
ngBuildProcess.on('close', (code) => {
console.log(`child process exited with code ${code}`);
res();
});
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
Comments suppressed due to low confidence (1)
apps/demos/utils/create-bundles/Angular/bundler.ts:55
spawn('npm', ...)can emit anerrorevent (e.g., ifnpmis not found). Right now there is noerrorhandler, which can crash the process and also leaveres()never called. Add anerrorlistener that logs/reports the failure and completes the callback (and consider failing the build when the exit code is non-zero).
const ngBuildProcess = spawn('npm', ['run', 'build-angular', '--', getProjectNameByDemo(demo)]);
ngBuildProcess.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
});
ngBuildProcess.stderr.on('data', (data) => {
console.error(`stderr: ${data}`);
});
ngBuildProcess.on('close', (code) => {
console.log(`child process exited with code ${code}`);
res();
});
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
Comments suppressed due to low confidence (1)
apps/demos/utils/create-bundles/Angular/bundler.ts:52
- There is no
'error'handler for the spawned process. If the process fails to start (e.g., missingnpm/PATH issues), neither'close'nor stdout/stderr handlers will guaranteeres()is called, which can leave the caller hanging. Add an'error'listener that logs and callsres()(or rejects, depending on expected control flow).
const ngBuildProcess = spawn('npm', ['run', 'build-angular', '--', getProjectNameByDemo(demo)]);
ngBuildProcess.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
});
ngBuildProcess.stderr.on('data', (data) => {
console.error(`stderr: ${data}`);
});
ngBuildProcess.on('close', (code) => {
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
Comments suppressed due to low confidence (1)
apps/demos/utils/create-bundles/Angular/bundler.ts:55
- The spawned process has no
'error'handler. Ifnpmcannot be spawned (e.g., PATH issues), Node will emit anerrorevent that can crash the script or leave the demo build promise unresolved. Add anngBuildProcess.on('error', ...)handler and ensureres()(or an error propagation path) is triggered.
const ngBuildProcess = spawn('npm', ['run', 'build-angular', '--', getProjectNameByDemo(demo)], { shell: process.platform === 'win32' });
ngBuildProcess.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
});
ngBuildProcess.stderr.on('data', (data) => {
console.error(`stderr: ${data}`);
});
ngBuildProcess.on('close', (code) => {
console.log(`child process exited with code ${code}`);
res();
});
This reverts commit 4184da5.
| cps.exec(`tsc ${args}`, (error, stdout, stderr) => { | ||
| if (error != null) { | ||
| const execTsc = (directory: string, args: string[]): Promise<string> => new Promise((resolve, reject) => { | ||
| const proc = cps.spawn('tsc', args, { cwd: directory, shell: isWindows() }); |
|
|
||
| const ngBuildCommand = `npm run build-angular -- ${getProjectNameByDemo(demo)}`; | ||
| const ngBuildProcess = exec(ngBuildCommand); | ||
| const ngBuildProcess = spawn('npm', ['run', 'build-angular', '--', getProjectNameByDemo(demo)], { shell: process.platform === 'win32' }); |
| if (signal !== null) { | ||
| // eslint-disable-next-line prefer-promise-reject-errors | ||
| return reject(`tsc killed by signal ${signal}\n${stderr}\n${stdout}`); | ||
| } |
| if (code !== 0) { | ||
| // eslint-disable-next-line prefer-promise-reject-errors | ||
| return reject(`${error}\n${stderr}\n${stdout}`); | ||
| return reject(`tsc exited with code ${code}\n${stderr}\n${stdout}`); | ||
| } |
Fix the eslint rule - security/detect-child-process