Skip to content

Commit 21694f2

Browse files
wraithgarlegobeat
andauthored
feat: use nodeGyp option (#230)
This allows for a `nodeGyp` option to be passed in to define where the node-gyp bin is to run. It also allows for the environment variable `npm_config_node_gyp` to already be set, and not override it if it is. This is an extension of #222 Closes: npm/cli#2839 Co-author: @legobeat --------- Co-authored-by: legobt <[email protected]>
1 parent fea1ba3 commit 21694f2

File tree

3 files changed

+50
-16
lines changed

3 files changed

+50
-16
lines changed

lib/make-spawn-args.js

+18-5
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,34 @@
11
/* eslint camelcase: "off" */
22
const setPATH = require('./set-path.js')
33
const { resolve } = require('path')
4-
const npm_config_node_gyp = require.resolve('node-gyp/bin/node-gyp.js')
4+
5+
let npm_config_node_gyp
56

67
const makeSpawnArgs = options => {
78
const {
9+
args,
10+
binPaths,
11+
cmd,
12+
env,
813
event,
14+
nodeGyp,
915
path,
1016
scriptShell = true,
11-
binPaths,
12-
env,
1317
stdio,
14-
cmd,
15-
args,
1618
stdioString,
1719
} = options
1820

21+
if (nodeGyp) {
22+
// npm already pulled this from env and passes it in to options
23+
npm_config_node_gyp = nodeGyp
24+
} else if (env.npm_config_node_gyp) {
25+
// legacy mode for standalone user
26+
npm_config_node_gyp = env.npm_config_node_gyp
27+
} else {
28+
// default
29+
npm_config_node_gyp = require.resolve('node-gyp/bin/node-gyp.js')
30+
}
31+
1932
const spawnEnv = setPATH(path, binPaths, {
2033
// we need to at least save the PATH environment var
2134
...process.env,

lib/run-script-pkg.js

+12-10
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,19 @@ const isServerPackage = require('./is-server-package.js')
77

88
const runScriptPkg = async options => {
99
const {
10-
event,
11-
path,
12-
scriptShell,
10+
args = [],
1311
binPaths = false,
1412
env = {},
15-
stdio = 'pipe',
13+
event,
14+
nodeGyp,
15+
path,
1616
pkg,
17-
args = [],
18-
stdioString,
17+
scriptShell,
1918
// how long to wait for a process.kill signal
2019
// only exposed here so that we can make the test go a bit faster.
2120
signalTimeout = 500,
21+
stdio = 'pipe',
22+
stdioString,
2223
} = options
2324

2425
const { scripts = {}, gypfile } = pkg
@@ -63,14 +64,15 @@ const runScriptPkg = async options => {
6364
}
6465

6566
const [spawnShell, spawnArgs, spawnOpts] = makeSpawnArgs({
67+
args,
68+
binPaths,
69+
cmd,
70+
env: { ...env, ...packageEnvs(pkg) },
6671
event,
72+
nodeGyp,
6773
path,
6874
scriptShell,
69-
binPaths,
70-
env: { ...env, ...packageEnvs(pkg) },
7175
stdio,
72-
cmd,
73-
args,
7476
stdioString,
7577
})
7678

test/make-spawn-args.js

+20-1
Original file line numberDiff line numberDiff line change
@@ -62,20 +62,39 @@ t.test('spawn args', async t => {
6262
/.*/,
6363
a => a.includes('echo test'),
6464
e => {
65-
return e.env.test_fixture === 'a string'
65+
return e.env.test_fixture === 'a string' &&
66+
e.env.npm_config_node_gyp === '/test/path.js'
6667
}
6768
)
6869
await t.resolves(() => runScript({
6970
pkg,
7071
path: testdir,
7172
env: {
73+
npm_config_node_gyp: '/test/path.js',
7274
test_fixture: 'a string',
7375
},
7476
event: 'test',
7577
}))
7678
t.ok(spawk.done())
7779
})
7880

81+
await t.test('provided options.nodeGyp', async t => {
82+
spawk.spawn(
83+
/.*/,
84+
a => a.includes('echo test'),
85+
e => {
86+
return e.env.npm_config_node_gyp === '/test/path.js'
87+
}
88+
)
89+
await t.resolves(() => runScript({
90+
pkg,
91+
path: testdir,
92+
nodeGyp: '/test/path.js',
93+
event: 'test',
94+
}))
95+
t.ok(spawk.done())
96+
})
97+
7998
await t.test('provided args', async t => {
8099
spawk.spawn(
81100
/.*/,

0 commit comments

Comments
 (0)