Skip to content

Commit

Permalink
fix: more lightweight npm bin discovery in windows
Browse files Browse the repository at this point in the history
  • Loading branch information
wraithgar committed Mar 6, 2024
1 parent af3c48e commit e59db0d
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 9 deletions.
3 changes: 2 additions & 1 deletion bin/npm
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ if [ $? -ne 0 ]; then
no_node_dir
fi
NPM_CLI_JS="$CLI_BASEDIR/node_modules/npm/bin/npm-cli.js"
NPM_PREFIX=`"$NODE_EXE" "$NPM_CLI_JS" prefix -g`
NPM_PREFIX_JS="$CLI_BASEDIR/node_modules/npm/bin/npm-prefix.js"
NPM_PREFIX=`"$NODE_EXE" "$NPM_PREFIX_JS"`
if [ $? -ne 0 ]; then
no_node_dir
fi
Expand Down
23 changes: 23 additions & 0 deletions bin/npm-prefix.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env node
// This is a single-use bin to help windows discover the proper prefix for npm without having to load all of npm first

Check failure on line 2 in bin/npm-prefix.js

View workflow job for this annotation

GitHub Actions / Lint

This line has a length of 118. Maximum allowed is 100
// It does not accept argv params

const path = require('path')
const Config = require('@npmcli/config')
const { definitions, flatten, shorthands } = require('@npmcli/config/lib/definitions')
const config = new Config({
npmPath: path.dirname(__dirname),
// argv is explicitly not looked at since prefix is not something that can be changed via argv
argv: [],
definitions,
flatten,
shorthands,
excludeNpmCwd: false,
})

config.load().then(() => {

Check failure on line 18 in bin/npm-prefix.js

View workflow job for this annotation

GitHub Actions / Lint

Each then() should return a value or throw
console.log(config.globalPrefix)

Check failure on line 19 in bin/npm-prefix.js

View workflow job for this annotation

GitHub Actions / Lint

Unexpected console statement
}).catch(err => {
console.trace(err)

Check failure on line 21 in bin/npm-prefix.js

View workflow job for this annotation

GitHub Actions / Lint

Unexpected console statement
process.exit(1)
})
3 changes: 2 additions & 1 deletion bin/npm.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ IF NOT EXIST "%NODE_EXE%" (
)

SET "NPM_CLI_JS=%~dp0\node_modules\npm\bin\npm-cli.js"
FOR /F "delims=" %%F IN ('CALL "%NODE_EXE%" "%NPM_CLI_JS%" prefix -g') DO (
SET "NPM_PREFIX_JS=%~dp0\node_modules\npm\bin\npm-prefix.js"
FOR /F "delims=" %%F IN ('CALL "%NODE_EXE%" "%NPM_PREFIX_JS%"') DO (
SET "NPM_PREFIX_NPM_CLI_JS=%%F\node_modules\npm\bin\npm-cli.js"
)
IF EXIST "%NPM_PREFIX_NPM_CLI_JS%" (
Expand Down
3 changes: 2 additions & 1 deletion bin/npm.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ if ($nodebin -eq $null) {
$nodedir = $(New-Object -ComObject Scripting.FileSystemObject).GetFile("$nodebin").ParentFolder.Path

$npmclijs="$nodedir/node_modules/npm/bin/npm-cli.js"
$npmprefix=(& $nodeexe $npmclijs prefix -g)
$npmprefixjs="$nodedir/node_modules/npm/bin/npm-prefix.js"
$npmprefix=(& $nodeexe $npmprefixjs)
if ($LASTEXITCODE -ne 0) {
Write-Host "Could not determine Node.js install directory"
exit 1
Expand Down
4 changes: 2 additions & 2 deletions bin/npx
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ if [ $? -ne 0 ]; then
fi
no_node_dir
fi
NPM_CLI_JS="$CLI_BASEDIR/node_modules/npm/bin/npm-cli.js"
NPM_PREFIX_JS="$CLI_BASEDIR/node_modules/npm/bin/npm-prefix.js"
NPX_CLI_JS="$CLI_BASEDIR/node_modules/npm/bin/npx-cli.js"
NPM_PREFIX=`"$NODE_EXE" "$NPM_CLI_JS" prefix -g`
NPM_PREFIX=`"$NODE_EXE" "$NPM_PREFIX_JS"`
if [ $? -ne 0 ]; then
no_node_dir
fi
Expand Down
4 changes: 2 additions & 2 deletions bin/npx.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ IF NOT EXIST "%NODE_EXE%" (
SET "NODE_EXE=node"
)

SET "NPM_CLI_JS=%~dp0\node_modules\npm\bin\npm-cli.js"
SET "NPM_PREFIX_JS=%~dp0\node_modules\npm\bin\npm-prefix.js"
SET "NPX_CLI_JS=%~dp0\node_modules\npm\bin\npx-cli.js"
FOR /F "delims=" %%F IN ('CALL "%NODE_EXE%" "%NPM_CLI_JS%" prefix -g') DO (
FOR /F "delims=" %%F IN ('CALL "%NODE_EXE%" "%NPM_PREFIX_JS%"') DO (
SET "NPM_PREFIX_NPX_CLI_JS=%%F\node_modules\npm\bin\npx-cli.js"
)
IF EXIST "%NPM_PREFIX_NPX_CLI_JS%" (
Expand Down
4 changes: 2 additions & 2 deletions bin/npx.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ if ($nodebin -eq $null) {
}
$nodedir = $(New-Object -ComObject Scripting.FileSystemObject).GetFile("$nodebin").ParentFolder.Path

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

0 comments on commit e59db0d

Please sign in to comment.