Skip to content

Commit 7f1ab88

Browse files
authored
fix: more lightweight npm bin discovery in windows (#7271)
This will stop npm having to load twice on every windows invocation
1 parent 0dab381 commit 7f1ab88

File tree

8 files changed

+48
-19
lines changed

8 files changed

+48
-19
lines changed

bin/npm

+2-1
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@ if [ $? -ne 0 ]; then
4141
fi
4242
no_node_dir
4343
fi
44+
NPM_PREFIX_JS="$CLI_BASEDIR/node_modules/npm/bin/npm-prefix.js"
4445
NPM_CLI_JS="$CLI_BASEDIR/node_modules/npm/bin/npm-cli.js"
45-
NPM_PREFIX=`"$NODE_EXE" "$NPM_CLI_JS" prefix -g`
46+
NPM_PREFIX=`"$NODE_EXE" "$NPM_PREFIX_JS"`
4647
if [ $? -ne 0 ]; then
4748
no_node_dir
4849
fi

bin/npm-prefix.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/env node
2+
// This is a single-use bin to help windows discover the proper prefix for npm
3+
// without having to load all of npm first
4+
// It does not accept argv params
5+
6+
const path = require('path')
7+
const Config = require('@npmcli/config')
8+
const { definitions, flatten, shorthands } = require('@npmcli/config/lib/definitions')
9+
const config = new Config({
10+
npmPath: path.dirname(__dirname),
11+
// argv is explicitly not looked at since prefix is not something that can be changed via argv
12+
argv: [],
13+
definitions,
14+
flatten,
15+
shorthands,
16+
excludeNpmCwd: false,
17+
})
18+
19+
async function main () {
20+
try {
21+
await config.load()
22+
// eslint-disable-next-line no-console
23+
console.log(config.globalPrefix)
24+
} catch (err) {
25+
// eslint-disable-next-line no-console
26+
console.error(err)
27+
process.exit(1)
28+
}
29+
}
30+
main()

bin/npm.cmd

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ IF NOT EXIST "%NODE_EXE%" (
88
SET "NODE_EXE=node"
99
)
1010

11+
SET "NPM_PREFIX_JS=%~dp0\node_modules\npm\bin\npm-prefix.js"
1112
SET "NPM_CLI_JS=%~dp0\node_modules\npm\bin\npm-cli.js"
12-
FOR /F "delims=" %%F IN ('CALL "%NODE_EXE%" "%NPM_CLI_JS%" prefix -g') DO (
13+
FOR /F "delims=" %%F IN ('CALL "%NODE_EXE%" "%NPM_PREFIX_JS%"') DO (
1314
SET "NPM_PREFIX_NPM_CLI_JS=%%F\node_modules\npm\bin\npm-cli.js"
1415
)
1516
IF EXIST "%NPM_PREFIX_NPM_CLI_JS%" (

bin/npm.ps1

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ if ($nodebin -eq $null) {
1717
}
1818
$nodedir = $(New-Object -ComObject Scripting.FileSystemObject).GetFile("$nodebin").ParentFolder.Path
1919

20-
$npmclijs="$nodedir/node_modules/npm/bin/npm-cli.js"
21-
$npmprefix=(& $nodeexe $npmclijs prefix -g)
20+
$npmprefixjs="$nodedir/node_modules/npm/bin/npm-prefix.js"
21+
$npmprefix=(& $nodeexe $npmprefixjs)
2222
if ($LASTEXITCODE -ne 0) {
2323
Write-Host "Could not determine Node.js install directory"
2424
exit 1

bin/npx

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ if [ $? -ne 0 ]; then
4141
fi
4242
no_node_dir
4343
fi
44-
NPM_CLI_JS="$CLI_BASEDIR/node_modules/npm/bin/npm-cli.js"
44+
NPM_PREFIX_JS="$CLI_BASEDIR/node_modules/npm/bin/npm-prefix.js"
4545
NPX_CLI_JS="$CLI_BASEDIR/node_modules/npm/bin/npx-cli.js"
46-
NPM_PREFIX=`"$NODE_EXE" "$NPM_CLI_JS" prefix -g`
46+
NPM_PREFIX=`"$NODE_EXE" "$NPM_PREFIX_JS"`
4747
if [ $? -ne 0 ]; then
4848
no_node_dir
4949
fi

bin/npx.cmd

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ IF NOT EXIST "%NODE_EXE%" (
88
SET "NODE_EXE=node"
99
)
1010

11-
SET "NPM_CLI_JS=%~dp0\node_modules\npm\bin\npm-cli.js"
11+
SET "NPM_PREFIX_JS=%~dp0\node_modules\npm\bin\npm-prefix.js"
1212
SET "NPX_CLI_JS=%~dp0\node_modules\npm\bin\npx-cli.js"
13-
FOR /F "delims=" %%F IN ('CALL "%NODE_EXE%" "%NPM_CLI_JS%" prefix -g') DO (
13+
FOR /F "delims=" %%F IN ('CALL "%NODE_EXE%" "%NPM_PREFIX_JS%"') DO (
1414
SET "NPM_PREFIX_NPX_CLI_JS=%%F\node_modules\npm\bin\npx-cli.js"
1515
)
1616
IF EXIST "%NPM_PREFIX_NPX_CLI_JS%" (

bin/npx.ps1

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ if ($nodebin -eq $null) {
1717
}
1818
$nodedir = $(New-Object -ComObject Scripting.FileSystemObject).GetFile("$nodebin").ParentFolder.Path
1919

20-
$npmclijs="$nodedir/node_modules/npm/bin/npm-cli.js"
21-
$npmprefix=(& $nodeexe $npmclijs prefix -g)
20+
$npmprefixjs="$nodedir/node_modules/npm/bin/npm-prefix.js"
21+
$npmprefix=(& $nodeexe $npmprefixjs)
2222
if ($LASTEXITCODE -ne 0) {
2323
Write-Host "Could not determine Node.js install directory"
2424
exit 1

test/bin/windows-shims.js

+6-9
Original file line numberDiff line numberDiff line change
@@ -44,23 +44,21 @@ t.test('shim contents', t => {
4444

4545
t.test('bash', t => {
4646
const { diff, letters } = diffFiles(SHIMS.npm, SHIMS.npx)
47-
t.match(diff[0].split('\n').reverse().join(''), /^NPX_CLI_JS=/, 'has NPX_CLI')
48-
t.equal(diff.length, 1)
47+
t.strictSame(diff, [])
4948
t.strictSame([...letters], ['M', 'X'], 'all other changes are m->x')
5049
t.end()
5150
})
5251

5352
t.test('cmd', t => {
5453
const { diff, letters } = diffFiles(SHIMS['npm.cmd'], SHIMS['npx.cmd'])
55-
t.match(diff[0], /^SET "NPX_CLI_JS=/, 'has NPX_CLI')
56-
t.equal(diff.length, 1)
54+
t.strictSame(diff, [])
5755
t.strictSame([...letters], ['M', 'X'], 'all other changes are m->x')
5856
t.end()
5957
})
6058

6159
t.test('pwsh', t => {
6260
const { diff, letters } = diffFiles(SHIMS['npm.ps1'], SHIMS['npx.ps1'])
63-
t.equal(diff.length, 0)
61+
t.strictSame(diff, [])
6462
t.strictSame([...letters], ['M', 'X'], 'all other changes are m->x')
6563
t.end()
6664
})
@@ -96,13 +94,12 @@ t.test('run shims', t => {
9694
node_modules: {
9795
npm: {
9896
bin: {
99-
'npx-cli.js': `throw new Error('this should not be called')`,
100-
'npm-cli.js': `
101-
const assert = require('assert')
97+
'npm-prefix.js': `
10298
const { resolve } = require('path')
103-
assert.equal(process.argv.slice(2).join(' '), 'prefix -g')
10499
console.log(resolve(__dirname, '../../../global-prefix'))
105100
`,
101+
'npx-cli.js': `throw new Error('local npx should not be called')`,
102+
'npm-cli.js': `throw new Error('local npm should not be called')`,
106103
},
107104
},
108105
},

0 commit comments

Comments
 (0)