Skip to content

Commit 475412d

Browse files
committed
refactor(scripts): convert spawnSync to async spawn for consistency
Convert remaining spawnSync usage to async spawn from @socketsecurity/lib/spawn for consistency across the codebase. Using async spawn provides better performance and matches the pattern used throughout the project. Files converted: - scripts/build.mjs: Build router now uses async spawn with result.code - scripts/publish.mjs: Publish router now uses async spawn with result.code - packages/cli/scripts/utils/patches.mjs: Patch utilities now use async spawn Changes: - Import spawn from @socketsecurity/lib/spawn instead of spawnSync from node:child_process - Wrap top-level code in async main() function - Change result.status to result.code (spawn API returns {code, stdout?, stderr?}) - Add main().catch() error handler for async execution Benefits: - Consistent spawn usage across all scripts - Better cross-platform compatibility with WIN32 shell handling - Non-blocking execution where synchronous behavior isn't required
1 parent 8e45dee commit 475412d

File tree

3 files changed

+82
-65
lines changed

3 files changed

+82
-65
lines changed

packages/cli/scripts/utils/patches.mjs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
* Provides helpers for transforming node_modules files and generating patch files.
44
*/
55

6-
import { spawnSync } from 'node:child_process'
76
import { existsSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
87
import path from 'node:path'
98

109
import { parse } from '@babel/core'
1110
import MagicString from 'magic-string'
11+
import { WIN32 } from '@socketsecurity/lib/constants/platform'
1212
import { logger } from '@socketsecurity/lib/logger'
13+
import { spawn } from '@socketsecurity/lib/spawn'
1314

1415
/**
1516
* Parse JavaScript/TypeScript code into a Babel AST.
@@ -91,14 +92,15 @@ export async function startPatch(packageSpec) {
9192
logger.log(`Starting patch for ${packageSpec}...`)
9293

9394
// First, try to run pnpm patch to see if directory already exists.
94-
let result = spawnSync('pnpm', ['patch', packageSpec], {
95-
encoding: 'utf-8',
95+
let result = await spawn('pnpm', ['patch', packageSpec], {
96+
shell: WIN32,
9697
stdio: ['inherit', 'pipe', 'pipe'], // Capture stdout and stderr.
98+
stdioString: true,
9799
})
98100

99101
// Check if the error is about existing patch directory.
100102
// pnpm outputs errors to stdout, not stderr.
101-
if (result.status !== 0 && result.stdout.includes('is not empty')) {
103+
if (result.code !== 0 && result.stdout.includes('is not empty')) {
102104
const match = result.stdout.match(/directory (.+?) is not empty/)
103105
const existingPatchDir = match ? match[1] : null
104106

@@ -118,14 +120,15 @@ export async function startPatch(packageSpec) {
118120
rmSync(existingPatchDir, { force: true, recursive: true })
119121

120122
// Try pnpm patch again.
121-
result = spawnSync('pnpm', ['patch', packageSpec], {
122-
encoding: 'utf-8',
123+
result = await spawn('pnpm', ['patch', packageSpec], {
124+
shell: WIN32,
123125
stdio: ['inherit', 'pipe', 'inherit'],
126+
stdioString: true,
124127
})
125128
}
126129
}
127130

128-
if (result.status !== 0) {
131+
if (result.code !== 0) {
129132
throw new Error(`Failed to start patch for ${packageSpec}`)
130133
}
131134

@@ -153,14 +156,14 @@ export async function startPatch(packageSpec) {
153156
* @param {string} patchPath - Path to temporary patch directory.
154157
* @param {string} packageName - Package name for logging.
155158
*/
156-
export function commitPatch(patchPath, packageName) {
159+
export async function commitPatch(patchPath, packageName) {
157160
logger.log(`Committing patch for ${packageName}...`)
158-
const result = spawnSync('pnpm', ['patch-commit', patchPath], {
159-
encoding: 'utf-8',
161+
const result = await spawn('pnpm', ['patch-commit', patchPath], {
162+
shell: WIN32,
160163
stdio: 'inherit',
161164
})
162165

163-
if (result.status !== 0) {
166+
if (result.code !== 0) {
164167
throw new Error(`Failed to commit patch for ${packageName}`)
165168
}
166169

@@ -221,7 +224,7 @@ export async function createPatch(patchDef) {
221224
}
222225

223226
// Commit the patch.
224-
commitPatch(patchPath, packageName)
227+
await commitPatch(patchPath, packageName)
225228
} catch (error) {
226229
logger.error(`Error creating patch for ${packageName}:`, error.message)
227230
// Cleanup temp directory on error.

scripts/build.mjs

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
* Routes build commands to appropriate packages based on --target flag.
66
*/
77

8-
import { spawnSync } from 'node:child_process'
98
import process from 'node:process'
9+
10+
import { WIN32 } from '@socketsecurity/lib/constants/platform'
1011
import { logger } from '@socketsecurity/lib/logger'
11-
import colors from 'yoctocolors-cjs'
12+
import { spawn } from '@socketsecurity/lib/spawn'
1213

1314
const TARGET_PACKAGES = {
1415
__proto__: null,
@@ -41,25 +42,31 @@ for (let i = 0; i < args.length; i++) {
4142
}
4243
}
4344

44-
const packageFilter = TARGET_PACKAGES[target]
45-
if (!packageFilter) {
46-
logger.error(`Unknown build target: ${target}`)
47-
logger.error(`Available targets: ${Object.keys(TARGET_PACKAGES).join(', ')}`)
48-
process.exit(1)
49-
}
45+
async function main() {
46+
const packageFilter = TARGET_PACKAGES[target]
47+
if (!packageFilter) {
48+
logger.error(`Unknown build target: ${target}`)
49+
logger.error(`Available targets: ${Object.keys(TARGET_PACKAGES).join(', ')}`)
50+
process.exit(1)
51+
}
5052

51-
const pnpmArgs = [
52-
'--filter',
53-
packageFilter,
54-
'run',
55-
'build',
56-
...buildArgs
57-
]
53+
const pnpmArgs = [
54+
'--filter',
55+
packageFilter,
56+
'run',
57+
'build',
58+
...buildArgs
59+
]
5860

59-
const result = spawnSync('pnpm', pnpmArgs, {
60-
encoding: 'utf8',
61-
shell: false,
62-
stdio: 'inherit'
63-
})
61+
const result = await spawn('pnpm', pnpmArgs, {
62+
shell: WIN32,
63+
stdio: 'inherit',
64+
})
6465

65-
process.exit(result.status ?? 1)
66+
process.exit(result.code ?? 1)
67+
}
68+
69+
main().catch(e => {
70+
logger.error(e)
71+
process.exit(1)
72+
})

scripts/publish.mjs

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
* Routes publish commands to appropriate packages based on --target flag.
66
*/
77

8-
import { spawnSync } from 'node:child_process'
98
import process from 'node:process'
9+
10+
import { WIN32 } from '@socketsecurity/lib/constants/platform'
1011
import { logger } from '@socketsecurity/lib/logger'
11-
import colors from 'yoctocolors-cjs'
12+
import { spawn } from '@socketsecurity/lib/spawn'
1213

1314
const TARGET_PACKAGES = {
1415
__proto__: null,
@@ -41,40 +42,46 @@ for (let i = 0; i < args.length; i++) {
4142
}
4243
}
4344

44-
const packageFilter = TARGET_PACKAGES[target]
45-
if (!packageFilter) {
46-
logger.error(`Unknown publish target: ${target}`)
47-
logger.error(`Available targets: ${Object.keys(TARGET_PACKAGES).join(', ')}`)
48-
process.exit(1)
49-
}
45+
async function main() {
46+
const packageFilter = TARGET_PACKAGES[target]
47+
if (!packageFilter) {
48+
logger.error(`Unknown publish target: ${target}`)
49+
logger.error(`Available targets: ${Object.keys(TARGET_PACKAGES).join(', ')}`)
50+
process.exit(1)
51+
}
5052

51-
// Special handling for 'all' target.
52-
if (target === 'all') {
53-
logger.log('Publishing all packages...')
54-
logger.log('Note: Packages are published in dependency order by pnpm')
55-
}
53+
// Special handling for 'all' target.
54+
if (target === 'all') {
55+
logger.log('Publishing all packages...')
56+
logger.log('Note: Packages are published in dependency order by pnpm')
57+
}
5658

57-
const pnpmArgs = [
58-
'--filter',
59-
packageFilter,
60-
'publish',
61-
...publishArgs
62-
]
59+
const pnpmArgs = [
60+
'--filter',
61+
packageFilter,
62+
'publish',
63+
...publishArgs
64+
]
6365

64-
logger.log(`Publishing ${target}...`)
65-
logger.log(`Command: pnpm ${pnpmArgs.join(' ')}`)
66-
logger.log('')
66+
logger.log(`Publishing ${target}...`)
67+
logger.log(`Command: pnpm ${pnpmArgs.join(' ')}`)
68+
logger.log('')
6769

68-
const result = spawnSync('pnpm', pnpmArgs, {
69-
encoding: 'utf8',
70-
shell: false,
71-
stdio: 'inherit'
72-
})
70+
const result = await spawn('pnpm', pnpmArgs, {
71+
shell: WIN32,
72+
stdio: 'inherit',
73+
})
74+
75+
if (result.code === 0) {
76+
logger.log(`\n✓ Successfully published ${target}`)
77+
} else {
78+
logger.error(`\n✗ Failed to publish ${target}`)
79+
}
7380

74-
if (result.status === 0) {
75-
logger.log(`\n✓ Successfully published ${target}`)
76-
} else {
77-
logger.error(`\n✗ Failed to publish ${target}`)
81+
process.exit(result.code ?? 1)
7882
}
7983

80-
process.exit(result.status ?? 1)
84+
main().catch(e => {
85+
logger.error(e)
86+
process.exit(1)
87+
})

0 commit comments

Comments
 (0)