Skip to content

Commit 49d1a67

Browse files
committed
refactor(scripts): eliminate run-command wrapper, use socket-lib spawn directly
Remove 276 lines of duplicate command execution code by migrating all scripts from custom run-command.mjs wrapper to @socketsecurity/lib/spawn. Changes: - Remove duplicate run-command.mjs files (138 lines each = 276 total) - Update 10 scripts to use spawn directly with WIN32 shell constant - Update monorepo-helper.mjs to use spawn with stdioString option - Fix packages/cli/scripts/update.mjs to use build-infra runParallel Updated scripts: - scripts/check.mjs, scripts/fix.mjs, scripts/type.mjs - scripts/update.mjs, scripts/test-monorepo.mjs - scripts/utils/monorepo-helper.mjs - packages/cli/scripts/{build,check,cover,fix,lint,update}.mjs Benefits: - Eliminates 276 lines of duplicated code - Consistent cross-platform spawn usage with WIN32 constant - Direct usage of socket-lib utilities per CLAUDE.md guidelines - Better error handling with result.code instead of exitCode - Uses stdioString option for cleaner output capture DRY wins: ~276 lines saved
1 parent 536e8b6 commit 49d1a67

File tree

12 files changed

+65
-319
lines changed

12 files changed

+65
-319
lines changed

packages/cli/scripts/build.mjs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414

1515
import { spawn } from 'node:child_process'
1616

17-
import { runCommandQuiet } from './utils/run-command.mjs'
17+
import { WIN32 } from '@socketsecurity/lib/constants/platform'
18+
import { spawn } from '@socketsecurity/lib/spawn'
1819
import { logger } from '@socketsecurity/lib/logger'
1920

2021
// Simple CLI helpers without registry dependencies.
@@ -115,9 +116,9 @@ async function main() {
115116
// Always use runCommandQuiet to capture output for error reporting.
116117
const result = await runCommandQuiet(command, args)
117118

118-
if (result.exitCode !== 0) {
119+
if (result.code !== 0) {
119120
if (!quiet) {
120-
log.error(`${name} failed (exit code: ${result.exitCode})`)
121+
log.error(`${name} failed (exit code: ${result.code})`)
121122
}
122123
// Always show output on failure.
123124
if (result.stdout) {

packages/cli/scripts/check.mjs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import { parseArgs } from '@socketsecurity/lib/argv/parse'
77
import { logger } from '@socketsecurity/lib/logger'
88
import { printFooter, printHeader } from '@socketsecurity/lib/stdio/header'
99

10-
import { runCommandQuiet } from './utils/run-command.mjs'
10+
import { WIN32 } from '@socketsecurity/lib/constants/platform'
11+
import { spawn } from '@socketsecurity/lib/spawn'
1112

1213
/**
1314
* Run ESLint check via lint script.
@@ -35,7 +36,7 @@ async function runEslintCheck(options = {}) {
3536

3637
const result = await runCommandQuiet('pnpm', args)
3738

38-
if (result.exitCode !== 0) {
39+
if (result.code !== 0) {
3940
if (!quiet) {
4041
logger.error('ESLint check failed')
4142
}
@@ -45,7 +46,7 @@ async function runEslintCheck(options = {}) {
4546
if (result.stderr) {
4647
logger.error(result.stderr)
4748
}
48-
return result.exitCode
49+
return result.code
4950
}
5051

5152
if (!quiet) {
@@ -69,7 +70,7 @@ async function runTypeCheck(options = {}) {
6970

7071
const result = await runCommandQuiet('pnpm', ['run', 'type'])
7172

72-
if (result.exitCode !== 0) {
73+
if (result.code !== 0) {
7374
if (!quiet) {
7475
logger.error('TypeScript check failed')
7576
}
@@ -79,7 +80,7 @@ async function runTypeCheck(options = {}) {
7980
if (result.stderr) {
8081
logger.error(result.stderr)
8182
}
82-
return result.exitCode
83+
return result.code
8384
}
8485

8586
if (!quiet) {
@@ -173,7 +174,7 @@ async function main() {
173174
quiet,
174175
staged: values.staged,
175176
})
176-
if (exitCode !== 0) {
177+
if (code !== 0) {
177178
if (!quiet) {
178179
logger.error('Checks failed')
179180
}
@@ -185,7 +186,7 @@ async function main() {
185186
// Run TypeScript check if requested or running all
186187
if (runAll || values.types) {
187188
exitCode = await runTypeCheck({ quiet })
188-
if (exitCode !== 0) {
189+
if (code !== 0) {
189190
if (!quiet) {
190191
logger.error('Checks failed')
191192
}

packages/cli/scripts/cover.mjs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ import { isQuiet, isVerbose } from '@socketsecurity/lib/argv/flags'
1818
import { parseArgs } from '@socketsecurity/lib/argv/parse'
1919
import { logger } from '@socketsecurity/lib/logger'
2020

21-
import { runCommandQuiet } from './utils/run-command.mjs'
21+
import { WIN32 } from '@socketsecurity/lib/constants/platform'
22+
import { spawn } from '@socketsecurity/lib/spawn'
2223

2324
/**
2425
* Print a header message.
@@ -168,7 +169,7 @@ async function main() {
168169
logger.log(' ───────────────────────────────')
169170
logger.log(` Code Coverage: ${codeCoveragePercent.toFixed(2)}%`)
170171
logger.log()
171-
} else if (exitCode !== 0) {
172+
} else if (code !== 0) {
172173
logger.log('\n--- Output ---')
173174
logger.log(output)
174175
}
@@ -260,7 +261,7 @@ async function main() {
260261
}
261262
}
262263

263-
if (exitCode !== 0) {
264+
if (code !== 0) {
264265
if (!quiet) {
265266
printError('Coverage failed')
266267
// Show relevant output on failure for debugging
@@ -278,7 +279,8 @@ async function main() {
278279
if (open) {
279280
const { runCommand } = await import('./utils/run-command.mjs')
280281
logger.info('Opening coverage report...')
281-
await runCommand('open', ['coverage/index.html'], {
282+
await spawn('open', ['coverage/index.html'], {
283+
shell: WIN32,
282284
stdio: 'ignore',
283285
})
284286
}

packages/cli/scripts/fix.mjs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ import { parseArgs } from '@socketsecurity/lib/argv/parse'
1818
import { logger } from '@socketsecurity/lib/logger'
1919
import { printHeader } from '@socketsecurity/lib/stdio/header'
2020

21-
import { runCommand } from './utils/run-command.mjs'
21+
import { WIN32 } from '@socketsecurity/lib/constants/platform'
22+
import { spawn } from '@socketsecurity/lib/spawn'
2223

2324
async function main() {
2425
const { values } = parseArgs({
@@ -55,11 +56,12 @@ async function main() {
5556
}
5657

5758
// Run lint with --fix flag.
58-
const exitCode = await runCommand('pnpm', lintArgs, {
59+
const exitCode = await spawn('pnpm', lintArgs, {
60+
shell: WIN32,
5961
stdio: quiet ? 'pipe' : 'inherit',
6062
})
6163

62-
if (exitCode !== 0) {
64+
if (code !== 0) {
6365
if (!quiet) {
6466
logger.error('Some fixes could not be applied')
6567
}

packages/cli/scripts/lint.mjs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import { getChangedFiles, getStagedFiles } from '@socketsecurity/lib/git'
1212
import { logger } from '@socketsecurity/lib/logger'
1313
import { printHeader } from '@socketsecurity/lib/stdio/header'
1414

15-
import { runCommandQuiet } from './utils/run-command.mjs'
15+
import { WIN32 } from '@socketsecurity/lib/constants/platform'
16+
import { spawn } from '@socketsecurity/lib/spawn'
1617

1718
// Files that trigger a full lint when changed
1819
const CORE_FILES = new Set([
@@ -194,7 +195,7 @@ async function runLintOnFiles(files, options = {}) {
194195

195196
const result = await runCommandQuiet('pnpm', args)
196197

197-
if (result.exitCode !== 0) {
198+
if (result.code !== 0) {
198199
// When fixing, non-zero exit codes are normal if fixes were applied.
199200
if (!fix || (result.stderr && result.stderr.trim().length > 0)) {
200201
if (!quiet) {
@@ -206,7 +207,7 @@ async function runLintOnFiles(files, options = {}) {
206207
if (result.stdout && !fix) {
207208
logger.log(result.stdout)
208209
}
209-
return result.exitCode
210+
return result.code
210211
}
211212
}
212213
}
@@ -267,7 +268,7 @@ async function runLintOnAll(options = {}) {
267268
for (const { args } of linters) {
268269
const result = await runCommandQuiet('pnpm', args)
269270

270-
if (result.exitCode !== 0) {
271+
if (result.code !== 0) {
271272
// When fixing, non-zero exit codes are normal if fixes were applied.
272273
if (!fix || (result.stderr && result.stderr.trim().length > 0)) {
273274
if (!quiet) {
@@ -279,7 +280,7 @@ async function runLintOnAll(options = {}) {
279280
if (result.stdout && !fix) {
280281
logger.log(result.stdout)
281282
}
282-
return result.exitCode
283+
return result.code
283284
}
284285
}
285286
}
@@ -453,7 +454,7 @@ async function main() {
453454
}
454455
}
455456

456-
if (exitCode !== 0) {
457+
if (code !== 0) {
457458
if (!quiet) {
458459
logger.error('')
459460
logger.log('Lint failed')

packages/cli/scripts/update.mjs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import {
2020
printSuccess,
2121
} from '@socketsecurity/lib/stdio/header'
2222

23-
import { runParallel } from './utils/run-command.mjs'
23+
import { runParallel } from '@socketsecurity/build-infra/lib/script-runner'
2424

2525
async function main() {
2626
const quiet = isQuiet()
@@ -70,7 +70,8 @@ async function main() {
7070
})
7171
}
7272

73-
const exitCode = await runParallel(commands)
73+
const results = await runParallel(commands)
74+
const exitCode = results.some(r => r.code !== 0) ? 1 : 0
7475

7576
// Clear progress line.
7677
if (!quiet) {

packages/cli/scripts/utils/run-command.mjs

Lines changed: 0 additions & 138 deletions
This file was deleted.

scripts/test-monorepo.mjs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ import {
1414
getAffectedPackages,
1515
getPackagesWithScript,
1616
} from './utils/monorepo-helper.mjs'
17-
import { runCommandQuiet } from './utils/run-command.mjs'
17+
import { WIN32 } from '@socketsecurity/lib/constants/platform'
18+
import { spawn } from '@socketsecurity/lib/spawn'
1819

1920
/**
2021
* Get packages to test and determine affected packages.
@@ -82,7 +83,7 @@ async function runPackageTest(pkg, testArgs = [], quiet = false) {
8283
{ cwd: process.cwd() },
8384
)
8485

85-
if (result.exitCode !== 0) {
86+
if (result.code !== 0) {
8687
if (!quiet) {
8788
logger.clearLine()
8889
logger.log(`${colors.red('✗')} ${displayName}`)
@@ -93,7 +94,7 @@ async function runPackageTest(pkg, testArgs = [], quiet = false) {
9394
if (result.stderr) {
9495
logger.error(result.stderr)
9596
}
96-
return result.exitCode
97+
return result.code
9798
}
9899

99100
if (!quiet) {
@@ -177,7 +178,7 @@ async function main() {
177178
}
178179
}
179180

180-
if (exitCode !== 0) {
181+
if (code !== 0) {
181182
if (!quiet) {
182183
logger.error('')
183184
logger.log('Tests failed')

0 commit comments

Comments
 (0)