Skip to content

Commit 9027266

Browse files
committed
1 parent ee4b3e0 commit 9027266

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+2937
-30
lines changed

mock-registry/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
"json-stringify-safe": "^5.0.1",
5252
"nock": "^13.3.3",
5353
"npm-package-arg": "^11.0.2",
54-
"pacote": "^17.0.4",
54+
"pacote": "^18.0.0",
5555
"tap": "^16.3.8"
5656
}
5757
}

node_modules/.gitignore

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@
3131
!/@npmcli/installed-package-contents
3232
!/@npmcli/map-workspaces
3333
!/@npmcli/metavuln-calculator
34+
!/@npmcli/metavuln-calculator/node_modules/
35+
/@npmcli/metavuln-calculator/node_modules/*
36+
!/@npmcli/metavuln-calculator/node_modules/@npmcli/
37+
/@npmcli/metavuln-calculator/node_modules/@npmcli/*
38+
!/@npmcli/metavuln-calculator/node_modules/@npmcli/run-script
39+
!/@npmcli/metavuln-calculator/node_modules/pacote
3440
!/@npmcli/name-from-folder
3541
!/@npmcli/node-gyp
3642
!/@npmcli/package-json
@@ -187,6 +193,12 @@
187193
!/npm-user-validate
188194
!/p-map
189195
!/pacote
196+
!/pacote/node_modules/
197+
/pacote/node_modules/*
198+
!/pacote/node_modules/@npmcli/
199+
/pacote/node_modules/@npmcli/*
200+
!/pacote/node_modules/@npmcli/run-script
201+
!/pacote/node_modules/proc-log
190202
!/parse-conflict-json
191203
!/path-key
192204
!/path-scurry
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
The ISC License
2+
3+
Copyright (c) npm, Inc.
4+
5+
Permission to use, copy, modify, and/or distribute this software for any
6+
purpose with or without fee is hereby granted, provided that the above
7+
copyright notice and this permission notice appear in all copies.
8+
9+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
15+
IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const { stat } = require('node:fs/promises')
2+
const { resolve } = require('node:path')
3+
4+
module.exports = async path => {
5+
try {
6+
const st = await stat(resolve(path, 'server.js'))
7+
return st.isFile()
8+
} catch (er) {
9+
return false
10+
}
11+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/* eslint camelcase: "off" */
2+
const setPATH = require('./set-path.js')
3+
const { resolve } = require('path')
4+
const npm_config_node_gyp = require.resolve('node-gyp/bin/node-gyp.js')
5+
6+
const makeSpawnArgs = options => {
7+
const {
8+
event,
9+
path,
10+
scriptShell = true,
11+
binPaths,
12+
env,
13+
stdio,
14+
cmd,
15+
args,
16+
stdioString,
17+
} = options
18+
19+
const spawnEnv = setPATH(path, binPaths, {
20+
// we need to at least save the PATH environment var
21+
...process.env,
22+
...env,
23+
npm_package_json: resolve(path, 'package.json'),
24+
npm_lifecycle_event: event,
25+
npm_lifecycle_script: cmd,
26+
npm_config_node_gyp,
27+
})
28+
29+
const spawnOpts = {
30+
env: spawnEnv,
31+
stdioString,
32+
stdio,
33+
cwd: path,
34+
shell: scriptShell,
35+
}
36+
37+
return [cmd, args, spawnOpts]
38+
}
39+
40+
module.exports = makeSpawnArgs
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/usr/bin/env sh
2+
node "$npm_config_node_gyp" "$@"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@node "%npm_config_node_gyp%" %*
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const packageEnvs = (vals, prefix, env = {}) => {
2+
for (const [key, val] of Object.entries(vals)) {
3+
if (val === undefined) {
4+
continue
5+
} else if (val === null || val === false) {
6+
env[`${prefix}${key}`] = ''
7+
} else if (Array.isArray(val)) {
8+
val.forEach((item, index) => {
9+
packageEnvs({ [`${key}_${index}`]: item }, `${prefix}`, env)
10+
})
11+
} else if (typeof val === 'object') {
12+
packageEnvs(val, `${prefix}${key}_`, env)
13+
} else {
14+
env[`${prefix}${key}`] = String(val)
15+
}
16+
}
17+
return env
18+
}
19+
20+
// https://github.com/npm/rfcs/pull/183 defines which fields we put into the environment
21+
module.exports = pkg => {
22+
return packageEnvs({
23+
name: pkg.name,
24+
version: pkg.version,
25+
config: pkg.config,
26+
engines: pkg.engines,
27+
bin: pkg.bin,
28+
}, 'npm_package_')
29+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
const makeSpawnArgs = require('./make-spawn-args.js')
2+
const promiseSpawn = require('@npmcli/promise-spawn')
3+
const packageEnvs = require('./package-envs.js')
4+
const { isNodeGypPackage, defaultGypInstallScript } = require('@npmcli/node-gyp')
5+
const signalManager = require('./signal-manager.js')
6+
const isServerPackage = require('./is-server-package.js')
7+
8+
// you wouldn't like me when I'm angry...
9+
const bruce = (id, event, cmd, args) => {
10+
let banner = id
11+
? `\n> ${id} ${event}\n`
12+
: `\n> ${event}\n`
13+
banner += `> ${cmd.trim().replace(/\n/g, '\n> ')}`
14+
if (args.length) {
15+
banner += ` ${args.join(' ')}`
16+
}
17+
banner += '\n'
18+
return banner
19+
}
20+
21+
const runScriptPkg = async options => {
22+
const {
23+
event,
24+
path,
25+
scriptShell,
26+
binPaths = false,
27+
env = {},
28+
stdio = 'pipe',
29+
pkg,
30+
args = [],
31+
stdioString,
32+
// note: only used when stdio:inherit
33+
banner = true,
34+
// how long to wait for a process.kill signal
35+
// only exposed here so that we can make the test go a bit faster.
36+
signalTimeout = 500,
37+
} = options
38+
39+
const { scripts = {}, gypfile } = pkg
40+
let cmd = null
41+
if (options.cmd) {
42+
cmd = options.cmd
43+
} else if (pkg.scripts && pkg.scripts[event]) {
44+
cmd = pkg.scripts[event]
45+
} else if (
46+
// If there is no preinstall or install script, default to rebuilding node-gyp packages.
47+
event === 'install' &&
48+
!scripts.install &&
49+
!scripts.preinstall &&
50+
gypfile !== false &&
51+
await isNodeGypPackage(path)
52+
) {
53+
cmd = defaultGypInstallScript
54+
} else if (event === 'start' && await isServerPackage(path)) {
55+
cmd = 'node server.js'
56+
}
57+
58+
if (!cmd) {
59+
return { code: 0, signal: null }
60+
}
61+
62+
if (stdio === 'inherit' && banner !== false) {
63+
// we're dumping to the parent's stdout, so print the banner
64+
console.log(bruce(pkg._id, event, cmd, args))
65+
}
66+
67+
const [spawnShell, spawnArgs, spawnOpts] = makeSpawnArgs({
68+
event,
69+
path,
70+
scriptShell,
71+
binPaths,
72+
env: { ...env, ...packageEnvs(pkg) },
73+
stdio,
74+
cmd,
75+
args,
76+
stdioString,
77+
})
78+
79+
const p = promiseSpawn(spawnShell, spawnArgs, spawnOpts, {
80+
event,
81+
script: cmd,
82+
pkgid: pkg._id,
83+
path,
84+
})
85+
86+
if (stdio === 'inherit') {
87+
signalManager.add(p.process)
88+
}
89+
90+
if (p.stdin) {
91+
p.stdin.end()
92+
}
93+
94+
return p.catch(er => {
95+
const { signal } = er
96+
// coverage disabled because win32 never emits signals
97+
/* istanbul ignore next */
98+
if (stdio === 'inherit' && signal) {
99+
// by the time we reach here, the child has already exited. we send the
100+
// signal back to ourselves again so that npm will exit with the same
101+
// status as the child
102+
process.kill(process.pid, signal)
103+
104+
// just in case we don't die, reject after 500ms
105+
// this also keeps the node process open long enough to actually
106+
// get the signal, rather than terminating gracefully.
107+
return new Promise((res, rej) => setTimeout(() => rej(er), signalTimeout))
108+
} else {
109+
throw er
110+
}
111+
})
112+
}
113+
114+
module.exports = runScriptPkg
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const PackageJson = require('@npmcli/package-json')
2+
const runScriptPkg = require('./run-script-pkg.js')
3+
const validateOptions = require('./validate-options.js')
4+
const isServerPackage = require('./is-server-package.js')
5+
6+
const runScript = async options => {
7+
validateOptions(options)
8+
if (options.pkg) {
9+
return runScriptPkg(options)
10+
}
11+
const { content: pkg } = await PackageJson.normalize(options.path)
12+
return runScriptPkg({ ...options, pkg })
13+
}
14+
15+
module.exports = Object.assign(runScript, { isServerPackage })

0 commit comments

Comments
 (0)