Skip to content

Commit 109c23d

Browse files
committed
fix(scripts): use .process property for spawn child process
The spawn function from @socketsecurity/lib/spawn returns a Promise with the child process attached as .process property. Updated all script files to access spawn(...).process instead of calling .on() directly on the Promise. Fixes CI test failures where child.on is not a function.
1 parent c2dd732 commit 109c23d

File tree

5 files changed

+73
-29
lines changed

5 files changed

+73
-29
lines changed

scripts/bump.mjs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
* Includes interactive mode for reviewing and refining AI-generated changelogs.
55
*/
66

7-
import { spawn } from '@socketsecurity/lib/spawn'
87
import { existsSync, promises as fs } from 'node:fs'
98
import path from 'node:path'
109
import readline from 'node:readline'
@@ -15,6 +14,7 @@ import colors from 'yoctocolors-cjs'
1514

1615
import { parseArgs } from '@socketsecurity/lib/argv/parse'
1716
import { getDefaultLogger } from '@socketsecurity/lib/logger'
17+
import { spawn } from '@socketsecurity/lib/spawn'
1818
import { printFooter, printHeader } from '@socketsecurity/lib/stdio/header'
1919

2020
const logger = getDefaultLogger()
@@ -103,13 +103,15 @@ async function confirm(question, defaultYes = true) {
103103

104104
async function runCommand(command, args = [], options = {}) {
105105
return new Promise((resolve, reject) => {
106-
const child = spawn(command, args, {
106+
const spawnPromise = spawn(command, args, {
107107
stdio: 'inherit',
108108
cwd: rootPath,
109109
...(WIN32 && { shell: true }),
110110
...options,
111111
})
112112

113+
const child = spawnPromise.process
114+
113115
child.on('exit', code => {
114116
resolve(code || 0)
115117
})
@@ -125,12 +127,14 @@ async function runCommandWithOutput(command, args = [], options = {}) {
125127
let stdout = ''
126128
let stderr = ''
127129

128-
const child = spawn(command, args, {
130+
const spawnPromise = spawn(command, args, {
129131
cwd: rootPath,
130132
...(WIN32 && { shell: true }),
131133
...options,
132134
})
133135

136+
const child = spawnPromise.process
137+
134138
if (child.stdout) {
135139
child.stdout.on('data', data => {
136140
stdout += data

scripts/claude.mjs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
* Requires Claude Code (claude) CLI to be installed.
55
*/
66

7-
import { spawn } from '@socketsecurity/lib/spawn'
87
import crypto from 'node:crypto'
98
import {
109
existsSync,
@@ -20,6 +19,7 @@ import { deleteAsync as del } from 'del'
2019
import colors from 'yoctocolors-cjs'
2120

2221
import { parseArgs } from '@socketsecurity/lib/argv/parse'
22+
import { spawn } from '@socketsecurity/lib/spawn'
2323

2424
const __dirname = path.dirname(fileURLToPath(import.meta.url))
2525
const rootPath = path.join(__dirname, '..')
@@ -892,13 +892,15 @@ function displayAnalysis(analysis) {
892892
async function runCommand(command, args = [], options = {}) {
893893
const opts = { __proto__: null, ...options }
894894
return new Promise((resolve, reject) => {
895-
const child = spawn(command, args, {
895+
const spawnPromise = spawn(command, args, {
896896
stdio: 'inherit',
897897
cwd: rootPath,
898898
...(WIN32 && { shell: true }),
899899
...opts,
900900
})
901901

902+
const child = spawnPromise.process
903+
902904
child.on('exit', code => {
903905
resolve(code || 0)
904906
})
@@ -917,12 +919,14 @@ async function runCommandWithOutput(command, args = [], options = {}) {
917919
let stdout = ''
918920
let stderr = ''
919921

920-
const child = spawn(command, args, {
922+
const spawnPromise = spawn(command, args, {
921923
cwd: rootPath,
922924
...(WIN32 && { shell: true }),
923925
...spawnOpts,
924926
})
925927

928+
const child = spawnPromise.process
929+
926930
// Write input to stdin if provided.
927931
if (input && child.stdin) {
928932
child.stdin.write(input)
@@ -1022,12 +1026,14 @@ async function runClaude(claudeCmd, prompt, options = {}) {
10221026
if (opts.interactive !== false) {
10231027
// Interactive mode - spawn with inherited stdio and pipe prompt
10241028
result = await new Promise((resolve, _reject) => {
1025-
const child = spawn(claudeCmd, args, {
1029+
const spawnPromise = spawn(claudeCmd, args, {
10261030
stdio: ['pipe', 'inherit', 'inherit'],
10271031
cwd: opts.cwd || rootPath,
10281032
...(WIN32 && { shell: true }),
10291033
})
10301034

1035+
const child = spawnPromise.process
1036+
10311037
// Set up timeout for interactive mode
10321038
const timeoutId = setTimeout(() => {
10331039
timedOut = true
@@ -4716,12 +4722,14 @@ Fix all issues by making necessary file changes. Be direct, don't ask questions.
47164722
}
47174723

47184724
const exitCode = await new Promise((resolve, _reject) => {
4719-
const child = spawn(scriptCmd, [], {
4725+
const spawnPromise = spawn(scriptCmd, [], {
47204726
stdio: 'inherit',
47214727
cwd: rootPath,
47224728
shell: true,
47234729
})
47244730

4731+
const child = spawnPromise.process
4732+
47254733
// Handle Ctrl+C gracefully
47264734
const sigintHandler = () => {
47274735
child.kill('SIGINT')
@@ -5036,12 +5044,14 @@ Fix the issue by making necessary file changes. Be direct, don't ask questions.`
50365044
}
50375045

50385046
const exitCode = await new Promise((resolve, _reject) => {
5039-
const child = spawn(scriptCmd, [], {
5047+
const spawnPromise = spawn(scriptCmd, [], {
50405048
stdio: 'inherit',
50415049
cwd: rootPath,
50425050
shell: true,
50435051
})
50445052

5053+
const child = spawnPromise.process
5054+
50455055
// Handle Ctrl+C gracefully
50465056
const sigintHandler = () => {
50475057
child.kill('SIGINT')

scripts/fix.mjs

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,51 @@
22
* @fileoverview Fix script that runs lint with auto-fix enabled.
33
*/
44

5-
import { spawn } from '@socketsecurity/lib/spawn'
65
import path from 'node:path'
76
import { fileURLToPath } from 'node:url'
87

8+
import { spawn } from '@socketsecurity/lib/spawn'
9+
910
const rootPath = path.resolve(
1011
path.dirname(fileURLToPath(import.meta.url)),
1112
'..',
1213
)
14+
const WIN32 = process.platform === 'win32'
1315

14-
// Pass through to lint.mjs with --fix flag
15-
const args = ['run', 'lint', '--fix', ...process.argv.slice(2)]
16+
async function runCommand(command, args = [], options = {}) {
17+
return new Promise((resolve, reject) => {
18+
const spawnPromise = spawn(command, args, {
19+
stdio: 'inherit',
20+
cwd: rootPath,
21+
...(WIN32 && { shell: true }),
22+
...options,
23+
})
1624

17-
const child = spawn('pnpm', args, {
18-
stdio: 'inherit',
19-
cwd: rootPath,
20-
...(process.platform === 'win32' && { shell: true }),
21-
})
25+
const child = spawnPromise.process
2226

23-
child.on('exit', code => {
24-
process.exitCode = code || 0
25-
})
27+
child.on('exit', code => {
28+
resolve(code || 0)
29+
})
30+
31+
child.on('error', error => {
32+
reject(error)
33+
})
34+
})
35+
}
36+
37+
async function main() {
38+
try {
39+
// Pass through to lint.mjs with --fix flag
40+
const args = ['run', 'lint', '--fix', ...process.argv.slice(2)]
41+
const exitCode = await runCommand('pnpm', args)
42+
process.exitCode = exitCode
43+
} catch (error) {
44+
console.error(`Fix script failed: ${error.message}`)
45+
process.exitCode = 1
46+
}
47+
}
2648

27-
child.on('error', error => {
28-
console.error(`Fix script failed: ${error.message}`)
29-
process.exitCode = 1
49+
main().catch(error => {
50+
console.error(error)
51+
process.exit(1)
3052
})

scripts/test.mjs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
* Combines check, build, and test steps with clean, consistent output.
44
*/
55

6-
import { spawn } from '@socketsecurity/lib/spawn'
76
import { existsSync } from 'node:fs'
87
import path from 'node:path'
98
import { fileURLToPath } from 'node:url'
@@ -13,6 +12,7 @@ import glob from 'fast-glob'
1312
import { parseArgs } from '@socketsecurity/lib/argv/parse'
1413
import { getDefaultLogger } from '@socketsecurity/lib/logger'
1514
import { onExit } from '@socketsecurity/lib/signal-exit'
15+
import { spawn } from '@socketsecurity/lib/spawn'
1616
import { getDefaultSpinner } from '@socketsecurity/lib/spinner'
1717
import { printHeader } from '@socketsecurity/lib/stdio/header'
1818

@@ -68,12 +68,14 @@ const removeExitHandler = onExit((_code, signal) => {
6868

6969
async function runCommand(command, args = [], options = {}) {
7070
return new Promise((resolve, reject) => {
71-
const child = spawn(command, args, {
71+
const spawnPromise = spawn(command, args, {
7272
stdio: 'inherit',
7373
...(process.platform === 'win32' && { shell: true }),
7474
...options,
7575
})
7676

77+
const child = spawnPromise.process
78+
7779
runningProcesses.add(child)
7880

7981
child.on('exit', code => {
@@ -93,11 +95,13 @@ async function runCommandWithOutput(command, args = [], options = {}) {
9395
let stdout = ''
9496
let stderr = ''
9597

96-
const child = spawn(command, args, {
98+
const spawnPromise = spawn(command, args, {
9799
...(process.platform === 'win32' && { shell: true }),
98100
...options,
99101
})
100102

103+
const child = spawnPromise.process
104+
101105
runningProcesses.add(child)
102106

103107
if (child.stdout) {

scripts/update.mjs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
* Handles taze updates, Socket package updates, and project-specific tasks.
44
*/
55

6-
import { spawn } from '@socketsecurity/lib/spawn'
76
import { existsSync } from 'node:fs'
87
import path from 'node:path'
98
import { fileURLToPath } from 'node:url'
109

1110
import { parseArgs } from '@socketsecurity/lib/argv/parse'
1211
import { getDefaultLogger } from '@socketsecurity/lib/logger'
12+
import { spawn } from '@socketsecurity/lib/spawn'
1313
import { printFooter, printHeader } from '@socketsecurity/lib/stdio/header'
1414

1515
const logger = getDefaultLogger()
@@ -30,13 +30,15 @@ function includesProvenanceDowngradeWarning(output) {
3030

3131
async function runCommand(command, args = [], options = {}) {
3232
return new Promise((resolve, reject) => {
33-
const child = spawn(command, args, {
33+
const spawnPromise = spawn(command, args, {
3434
stdio: 'inherit',
3535
cwd: rootPath,
3636
...(WIN32 && { shell: true }),
3737
...options,
3838
})
3939

40+
const child = spawnPromise.process
41+
4042
child.on('exit', code => {
4143
resolve(code || 0)
4244
})
@@ -53,12 +55,14 @@ async function runCommandWithOutput(command, args = [], options = {}) {
5355
let stderr = ''
5456
let hasProvenanceDowngrade = false
5557

56-
const child = spawn(command, args, {
58+
const spawnPromise = spawn(command, args, {
5759
cwd: rootPath,
5860
...(WIN32 && { shell: true }),
5961
...options,
6062
})
6163

64+
const child = spawnPromise.process
65+
6266
if (child.stdout) {
6367
child.stdout.on('data', chunk => {
6468
stdout += chunk

0 commit comments

Comments
 (0)