Skip to content

Commit ea36e9d

Browse files
committed
Use signal-exit
1 parent 07ae62f commit ea36e9d

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

bin/cli.js

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,42 @@
22
'use strict'
33

44
const constants = require('../dist/constants')
5-
require(`../dist/${constants.DIST_TYPE}/cli.js`)
5+
6+
const { DIST_TYPE, execPath } = constants
7+
8+
if (DIST_TYPE === 'require') {
9+
require(`../dist/require/cli.js`)
10+
} else {
11+
const path = require('node:path')
12+
const spawn = require('@npmcli/promise-spawn')
13+
const { onExit } = require('signal-exit')
14+
15+
const abortController = new AbortController()
16+
const { signal: abortSignal } = abortController
17+
18+
// Detect ^C, i.e. Ctrl + C.
19+
onExit(() => {
20+
abortController.abort()
21+
})
22+
23+
const spawnPromise = spawn(
24+
execPath,
25+
[
26+
// Lazily access constants.nodeNoWarningsFlags.
27+
...constants.nodeNoWarningsFlags,
28+
path.join(constants.rootDistPath, DIST_TYPE, 'cli.js'),
29+
...process.argv.slice(2)
30+
],
31+
{
32+
stdio: 'inherit',
33+
signal: abortSignal
34+
}
35+
)
36+
spawnPromise.process.on('exit', (code, signal) => {
37+
if (signal) {
38+
process.kill(process.pid, signal)
39+
} else if (code !== null) {
40+
process.exit(code)
41+
}
42+
})
43+
}

src/shadow/arborist.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import yoctoSpinner from '@socketregistry/yocto-spinner'
1010
import isInteractive from 'is-interactive'
1111
import npa from 'npm-package-arg'
1212
import semver from 'semver'
13+
import { onExit } from 'signal-exit'
1314

1415
import config from '@socketsecurity/config'
1516
import { isObject } from '@socketsecurity/registry/lib/objects'
@@ -259,6 +260,8 @@ if (npmRootPath === undefined) {
259260
console.error(
260261
`Unable to find npm CLI install directory.\nSearched parent directories of ${npmEntrypoint}.\n\n${POTENTIAL_BUG_ERROR_MESSAGE}`
261262
)
263+
// The exit code 127 indicates that the command or binary being executed
264+
// could not be found.
262265
process.exit(127)
263266
}
264267

@@ -288,6 +291,8 @@ if (log === undefined) {
288291
console.error(
289292
`Unable to integrate with npm CLI logging infrastructure.\n\n${POTENTIAL_BUG_ERROR_MESSAGE}.`
290293
)
294+
// The exit code 127 indicates that the command or binary being executed
295+
// could not be found.
291296
process.exit(127)
292297
}
293298

@@ -298,6 +303,11 @@ const translations = require(path.join(rootPath, 'translations.json'))
298303
const abortController = new AbortController()
299304
const { signal: abortSignal } = abortController
300305

306+
// Detect ^C, i.e. Ctrl + C.
307+
onExit(() => {
308+
abortController.abort()
309+
})
310+
301311
const Arborist: ArboristClass = require(arboristClassPath)
302312
const depValid: (
303313
child: NodeClass,

src/shadow/shadow-bin.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ export default async function shadow(binName: 'npm' | 'npx') {
2727
const npmEntrypoint = realpathSync(binPath)
2828
const npmRootPath = findRoot(path.dirname(npmEntrypoint))
2929
if (npmRootPath === undefined) {
30+
// The exit code 127 indicates that the command or binary being executed
31+
// could not be found.
3032
process.exit(127)
3133
}
3234
const npmDepPath = path.join(npmRootPath, 'node_modules')

0 commit comments

Comments
 (0)