Skip to content

Commit e7ba2f2

Browse files
committed
refactor: simplify stdout/stderr handling with optional chaining
Replace verbose ternary chains and type checks for stdout/stderr with cleaner optional chaining and nullish coalescing operators. Changes: - Use `stdout?.toString() ?? ''` instead of complex type checks - Consistent pattern across all spawn/subprocess utilities - Applies to utils (dlx, shadow, coana, python, process, ecosystem) - Updates console command and bootstrap tests - Updates lint script error handling No functional changes, just cleaner and more consistent code.
1 parent 10c5e2a commit e7ba2f2

File tree

10 files changed

+25
-48
lines changed

10 files changed

+25
-48
lines changed

packages/cli/scripts/lint.mjs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -204,15 +204,15 @@ async function runLintOnFiles(files, options = {}) {
204204
await spawn('pnpm', args, { shell: WIN32 })
205205
} catch (e) {
206206
// When fixing, non-zero exit codes are normal if fixes were applied.
207-
if (!fix || (e.stderr && e.stderr.toString().trim().length > 0)) {
207+
if (!fix || (e.stderr && e.stderr?.toString().trim().length > 0)) {
208208
if (!quiet) {
209209
logger.error('Linting failed')
210210
}
211211
if (e.stderr) {
212-
logger.error(e.stderr.toString())
212+
logger.error(e.stderr?.toString() ?? '')
213213
}
214214
if (e.stdout && !fix) {
215-
logger.log(e.stdout.toString())
215+
logger.log(e.stdout?.toString() ?? '')
216216
}
217217
return e.code || 1
218218
}
@@ -275,15 +275,15 @@ async function runLintOnAll(options = {}) {
275275
await spawn('pnpm', args, { shell: WIN32 })
276276
} catch (e) {
277277
// When fixing, non-zero exit codes are normal if fixes were applied.
278-
if (!fix || (e.stderr && e.stderr.toString().trim().length > 0)) {
278+
if (!fix || (e.stderr && e.stderr?.toString().trim().length > 0)) {
279279
if (!quiet) {
280280
logger.error('Linting failed')
281281
}
282282
if (e.stderr) {
283-
logger.error(e.stderr.toString())
283+
logger.error(e.stderr?.toString() ?? '')
284284
}
285285
if (e.stdout && !fix) {
286-
logger.log(e.stdout.toString())
286+
logger.log(e.stdout?.toString() ?? '')
287287
}
288288
return e.code || 1
289289
}

packages/cli/src/commands/console/handle-console.mts

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -237,13 +237,7 @@ export async function handleConsole(): Promise<void> {
237237

238238
// Add stdout output.
239239
if (result.stdout) {
240-
const lines = (
241-
typeof result.stdout === 'string'
242-
? result.stdout
243-
: result.stdout.toString()
244-
)
245-
.trim()
246-
.split('\n')
240+
const lines = result.stdout?.toString().trim().split('\n') ?? []
247241
for (const line of lines) {
248242
addMessage(line)
249243
}
@@ -281,13 +275,7 @@ export async function handleConsole(): Promise<void> {
281275

282276
// Add stdout output.
283277
if (result.stdout) {
284-
const lines = (
285-
typeof result.stdout === 'string'
286-
? result.stdout
287-
: result.stdout.toString()
288-
)
289-
.trim()
290-
.split('\n')
278+
const lines = result.stdout?.toString().trim().split('\n') ?? []
291279
for (const line of lines) {
292280
addMessage(line)
293281
}

packages/cli/src/shadow/npm-base.mts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,12 @@ export default async function shadowNpmBase(
9393
const prefixResult = spawnSync(npmBin, ['prefix', '-g'], {
9494
cwd: process.cwd(),
9595
})
96-
npmGlobalPrefix = prefixResult.stdout.toString().trim()
96+
npmGlobalPrefix = prefixResult.stdout?.toString().trim() ?? ''
9797
// Get npm cache path.
9898
const cacheResult = spawnSync(npmBin, ['config', 'get', 'cache'], {
9999
cwd: process.cwd(),
100100
})
101-
npmCachePath = cacheResult.stdout.toString().trim()
101+
npmCachePath = cacheResult.stdout?.toString().trim() ?? ''
102102
} catch {
103103
// Fallback to defaults if npm commands fail.
104104
const home = homedir()

packages/cli/src/utils/coana/spawn.mts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export async function spawnCoana(
7878

7979
return {
8080
ok: true,
81-
data: spawnResult.stdout ? spawnResult.stdout.toString() : '',
81+
data: spawnResult.stdout?.toString() ?? '',
8282
}
8383
}
8484

packages/cli/src/utils/dlx/spawn.mts

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -141,12 +141,7 @@ export async function spawnCoanaDlx(
141141

142142
return {
143143
ok: true,
144-
data:
145-
typeof spawnResult.stdout === 'string'
146-
? spawnResult.stdout
147-
: spawnResult.stdout
148-
? spawnResult.stdout.toString()
149-
: '',
144+
data: spawnResult.stdout?.toString() ?? '',
150145
}
151146
}
152147

@@ -174,12 +169,7 @@ export async function spawnCoanaDlx(
174169
const output = await result.spawnPromise
175170
return {
176171
ok: true,
177-
data:
178-
typeof output.stdout === 'string'
179-
? output.stdout
180-
: output.stdout
181-
? output.stdout.toString()
182-
: '',
172+
data: output.stdout?.toString() ?? '',
183173
}
184174
} catch (e) {
185175
const stderr = (e as any)?.stderr

packages/cli/src/utils/ecosystem/environment.mts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,8 +306,7 @@ async function getAgentVersion(
306306
// when shell is true.
307307
shell: WIN32,
308308
})
309-
const stdout = spawnResult.stdout
310-
return typeof stdout === 'string' ? stdout : stdout.toString()
309+
return spawnResult.stdout?.toString() ?? ''
311310
})(),
312311
) ?? undefined
313312
} catch (e) {

packages/cli/src/utils/process/runner.mts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ export async function runExternalCommand(
9898
spinner = undefined
9999
}
100100

101-
const stdout = spawnResult.stdout ? spawnResult.stdout.toString() : ''
102-
const stderr = spawnResult.stderr ? spawnResult.stderr.toString() : ''
101+
const stdout = spawnResult.stdout?.toString() ?? ''
102+
const stderr = spawnResult.stderr?.toString() ?? ''
103103
const exitCode = spawnResult.code ?? 0
104104

105105
debugNs('stdio', `Command completed with exit code: ${exitCode}`)

packages/cli/src/utils/python/standalone.mts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ export async function spawnSocketPython(
342342

343343
return {
344344
ok: true,
345-
data: spawnResult.stdout ? spawnResult.stdout.toString() : '',
345+
data: spawnResult.stdout?.toString() ?? '',
346346
}
347347
}
348348

@@ -366,7 +366,7 @@ export async function spawnSocketPython(
366366

367367
return {
368368
ok: true,
369-
data: spawnResult.stdout ? spawnResult.stdout.toString() : '',
369+
data: spawnResult.stdout?.toString() ?? '',
370370
}
371371
} catch (e) {
372372
return {

packages/cli/src/utils/shadow/runner.mts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ export async function runShadowCommand(
147147
}
148148

149149
const output = await result.spawnPromise
150-
return { ok: true, data: output.stdout.toString() }
150+
return { ok: true, data: output.stdout?.toString() ?? '' }
151151
} catch (e) {
152152
if (spinner) {
153153
spinner.stop()
@@ -209,7 +209,7 @@ export async function runShadowNpm(
209209
}
210210

211211
const output = await result.spawnPromise
212-
return { ok: true, data: output.stdout.toString() }
212+
return { ok: true, data: output.stdout?.toString() ?? '' }
213213
} catch (e) {
214214
if (spinner) {
215215
spinner.stop()

packages/socket/test/bootstrap.test.mjs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ describe('socket package', () => {
138138
expect(result.status).toBe(0)
139139

140140
// Should output version.
141-
const stdout = result.stdout.toString()
141+
const stdout = result.stdout?.toString() ?? ''
142142
expect(stdout).toMatch(/\d+\.\d+\.\d+/)
143143

144144
// Should have cached CLI.
@@ -164,7 +164,7 @@ describe('socket package', () => {
164164
})
165165

166166
expect(result.status).toBe(0)
167-
expect(result.stdout.toString()).toContain('1.0.0-mock')
167+
expect(result.stdout?.toString()).toContain('1.0.0-mock')
168168
})
169169

170170
it('should use compressed CLI when available', async () => {
@@ -186,7 +186,7 @@ describe('socket package', () => {
186186
})
187187

188188
expect(result.status).toBe(0)
189-
expect(result.stdout.toString()).toContain('1.0.0-compressed')
189+
expect(result.stdout?.toString()).toContain('1.0.0-compressed')
190190
})
191191

192192
it('should pass arguments to delegated CLI', async () => {
@@ -210,7 +210,7 @@ describe('socket package', () => {
210210
)
211211

212212
expect(result.status).toBe(0)
213-
const args = JSON.parse(result.stdout.toString())
213+
const args = JSON.parse(result.stdout?.toString())
214214
expect(args).toEqual(['report', '--json', 'lodash'])
215215
})
216216

@@ -231,7 +231,7 @@ describe('socket package', () => {
231231
})
232232

233233
expect(result.status).toBe(0)
234-
expect(result.stdout.toString()).toContain('PKG_INVOKE_NODEJS')
234+
expect(result.stdout?.toString()).toContain('PKG_INVOKE_NODEJS')
235235
})
236236

237237
it('should exit with CLI exit code', async () => {

0 commit comments

Comments
 (0)