Skip to content

Commit 8c37e7f

Browse files
committed
Add signal exits
1 parent 0176664 commit 8c37e7f

File tree

5 files changed

+70
-43
lines changed

5 files changed

+70
-43
lines changed

bin/cli.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,13 @@ if (DIST_TYPE === 'require') {
2727
stdio: 'inherit'
2828
}
2929
)
30-
spawnPromise.process.on('exit', (code, signal) => {
31-
if (signal) {
32-
process.kill(process.pid, signal)
30+
// See https://nodejs.org/api/all.html#all_child_process_event-exit.
31+
spawnPromise.process.on('exit', (code, signalName) => {
32+
if (abortSignal.aborted) {
33+
return
34+
}
35+
if (signalName) {
36+
process.kill(process.pid, signalName)
3337
} else if (code !== null) {
3438
process.exit(code)
3539
}

src/commands/raw-npm.ts

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,19 @@ import meow from 'meow'
44
import constants from '../constants'
55
import { commonFlags, validationFlags } from '../flags'
66
import { printFlagList } from '../utils/formatting'
7+
import { findBinPathDetails } from '../utils/path-resolve'
78

89
import type { CliSubcommand } from '../utils/meow-with-subcommands'
910

1011
const { abortSignal } = constants
1112

13+
const binName = 'npm'
14+
1215
export const rawNpm: CliSubcommand = {
13-
description: 'Temporarily disable the Socket npm wrapper',
16+
description: `Temporarily disable the Socket ${binName} wrapper`,
1417
async run(argv, importMeta, { parentName }) {
1518
await setupCommand(
16-
`${parentName} raw-npm`,
19+
`${parentName} raw-${binName}`,
1720
rawNpm.description,
1821
argv,
1922
importMeta
@@ -34,7 +37,7 @@ async function setupCommand(
3437
const cli = meow(
3538
`
3639
Usage
37-
$ ${name} <npm command>
40+
$ ${name} <${binName} command>
3841
3942
Options
4043
${printFlagList(flags, 6)}
@@ -57,13 +60,26 @@ async function setupCommand(
5760
cli.showHelp()
5861
return
5962
}
60-
const spawnPromise = spawn('npm', <string[]>argv, {
63+
const { path: binPath } = await findBinPathDetails(binName)
64+
if (!binPath) {
65+
// The exit code 127 indicates that the command or binary being executed
66+
// could not be found.
67+
console.error(
68+
`Socket unable to locate ${binName}; ensure it is available in the PATH environment variable.`
69+
)
70+
process.exit(127)
71+
}
72+
const spawnPromise = spawn(binPath, <string[]>argv, {
6173
signal: abortSignal,
6274
stdio: 'inherit'
6375
})
64-
spawnPromise.process.on('exit', (code, signal) => {
65-
if (signal) {
66-
process.kill(process.pid, signal)
76+
// See https://nodejs.org/api/all.html#all_child_process_event-exit.
77+
spawnPromise.process.on('exit', (code, signalName) => {
78+
if (abortSignal.aborted) {
79+
return
80+
}
81+
if (signalName) {
82+
process.kill(process.pid, signalName)
6783
} else if (code !== null) {
6884
process.exit(code)
6985
}

src/commands/raw-npx.ts

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,19 @@ import meow from 'meow'
44
import constants from '../constants'
55
import { commonFlags, validationFlags } from '../flags'
66
import { printFlagList } from '../utils/formatting'
7+
import { findBinPathDetails } from '../utils/path-resolve'
78

89
import type { CliSubcommand } from '../utils/meow-with-subcommands'
910

1011
const { abortSignal } = constants
1112

13+
const binName = 'npx'
14+
1215
export const rawNpx: CliSubcommand = {
13-
description: 'Temporarily disable the Socket npm/npx wrapper',
16+
description: `Temporarily disable the Socket ${binName} wrapper`,
1417
async run(argv, importMeta, { parentName }) {
1518
await setupCommand(
16-
`${parentName} raw-npx`,
19+
`${parentName} raw-${binName}`,
1720
rawNpx.description,
1821
argv,
1922
importMeta
@@ -34,7 +37,7 @@ async function setupCommand(
3437
const cli = meow(
3538
`
3639
Usage
37-
$ ${name} <npx command>
40+
$ ${name} <${binName} command>
3841
3942
Options
4043
${printFlagList(flags, 6)}
@@ -57,13 +60,26 @@ async function setupCommand(
5760
cli.showHelp()
5861
return
5962
}
60-
const spawnPromise = spawn('npx', [argv.join(' ')], {
63+
const { path: binPath } = await findBinPathDetails(binName)
64+
if (!binPath) {
65+
// The exit code 127 indicates that the command or binary being executed
66+
// could not be found.
67+
console.error(
68+
`Socket unable to locate ${binName}; ensure it is available in the PATH environment variable.`
69+
)
70+
process.exit(127)
71+
}
72+
const spawnPromise = spawn(binPath, <string[]>argv, {
6173
signal: abortSignal,
6274
stdio: 'inherit'
6375
})
64-
spawnPromise.process.on('exit', (code, signal) => {
65-
if (signal) {
66-
process.kill(process.pid, signal)
76+
// See https://nodejs.org/api/all.html#all_child_process_event-exit.
77+
spawnPromise.process.on('exit', (code, signalName) => {
78+
if (abortSignal.aborted) {
79+
return
80+
}
81+
if (signalName) {
82+
process.kill(process.pid, signalName)
6783
} else if (code !== null) {
6884
process.exit(code)
6985
}

src/shadow/link.ts

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
import { realpathSync } from 'node:fs'
21
import path from 'node:path'
32

43
import cmdShim from 'cmd-shim'
5-
import which from 'which'
64

75
import constants from '../constants'
6+
import { findBinPathDetails } from '../utils/path-resolve'
87

98
const { WIN32, rootDistPath } = constants
109

@@ -13,34 +12,21 @@ export async function installLinks(
1312
binName: 'npm' | 'npx'
1413
): Promise<string> {
1514
// Find package manager being shadowed by this process.
16-
const bins =
17-
(await which(binName, {
18-
all: true,
19-
nothrow: true
20-
})) ?? []
21-
let shadowIndex = -1
22-
const binPath = bins.find((binPath, i) => {
23-
// Skip our bin directory if it's in the front.
24-
if (realpathSync(path.dirname(binPath)) === realBinPath) {
25-
shadowIndex = i
26-
return false
27-
}
28-
return true
29-
})
15+
const { path: binPath, shadowed } = await findBinPathDetails(binName)
3016
if (!binPath) {
31-
console.error(
32-
`Socket unable to locate ${binName}; ensure it is available in the PATH environment variable`
33-
)
3417
// The exit code 127 indicates that the command or binary being executed
3518
// could not be found.
19+
console.error(
20+
`Socket unable to locate ${binName}; ensure it is available in the PATH environment variable.`
21+
)
3622
process.exit(127)
3723
}
3824
// TODO: Is this early exit needed?
3925
if (WIN32 && binPath) {
4026
return binPath
4127
}
4228
// Move our bin directory to front of PATH so its found first.
43-
if (shadowIndex === -1) {
29+
if (!shadowed) {
4430
if (WIN32) {
4531
await cmdShim(
4632
path.join(rootDistPath, `${binName}-cli.js`),

src/shadow/shadow-bin.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,17 @@ const { abortSignal, distPath, execPath, shadowBinPath } = constants
1111

1212
const injectionPath = path.join(distPath, 'npm-injection.js')
1313

14-
export default async function shadow(binName: 'npm' | 'npx') {
14+
export default async function shadow(
15+
binName: 'npm' | 'npx',
16+
binArgs = process.argv.slice(2)
17+
) {
1518
const binPath = await installLinks(shadowBinPath, binName)
1619
if (abortSignal.aborted) {
1720
return
1821
}
1922
// Adding the `--quiet` and `--no-progress` flags when the `proc-log` module
2023
// is found to fix a UX issue when running the command with recent versions of
2124
// npm (input swallowed by the standard npm spinner)
22-
const binArgs: string[] = process.argv.slice(2)
23-
2425
if (
2526
binName === 'npm' &&
2627
binArgs.includes('install') &&
@@ -60,9 +61,13 @@ export default async function shadow(binName: 'npm' | 'npx') {
6061
stdio: 'inherit'
6162
}
6263
)
63-
spawnPromise.process.on('exit', (code, signal) => {
64-
if (signal) {
65-
process.kill(process.pid, signal)
64+
// See https://nodejs.org/api/all.html#all_child_process_event-exit.
65+
spawnPromise.process.on('exit', (code, signalName) => {
66+
if (abortSignal.aborted) {
67+
return
68+
}
69+
if (signalName) {
70+
process.kill(process.pid, signalName)
6671
} else if (code !== null) {
6772
process.exit(code)
6873
}

0 commit comments

Comments
 (0)