Skip to content

Commit 89b2741

Browse files
mribbonslukekarrys
andauthoredJun 26, 2023
feat: add ps1 scripts (#6548)
* add ps1 scripts Resolves UNC path issues because powershell.exe supports UNC, while cmd.exe doesn't. (Which is why npm.cmd doesn't work within UNC paths, even under powershell) Resolves #6280 * fixup: add tests and prefix for ps1 scripts --------- Co-authored-by: Luke Karrys <luke@lukekarrys.com>
1 parent 332dec3 commit 89b2741

File tree

3 files changed

+90
-1
lines changed

3 files changed

+90
-1
lines changed
 

‎bin/npm.ps1

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/usr/bin/env pwsh
2+
$basedir=Split-Path $MyInvocation.MyCommand.Definition -Parent
3+
4+
$exe=""
5+
if ($PSVersionTable.PSVersion -lt "6.0" -or $IsWindows) {
6+
# Fix case when both the Windows and Linux builds of Node
7+
# are installed in the same directory
8+
$exe=".exe"
9+
}
10+
$ret=0
11+
12+
$nodeexe = "node$exe"
13+
$nodebin = $(Get-Command $nodeexe -ErrorAction SilentlyContinue -ErrorVariable F).Source
14+
if ($nodebin -eq $null) {
15+
Write-Host "$nodeexe not found."
16+
exit 1
17+
}
18+
$nodedir = $(New-Object -ComObject Scripting.FileSystemObject).GetFile("$nodebin").ParentFolder.Path
19+
20+
$npmclijs="$nodedir/node_modules/npm/bin/npm-cli.js"
21+
$npmprefix=(& $nodeexe $npmclijs prefix -g)
22+
if ($LASTEXITCODE -ne 0) {
23+
Write-Host "Could not determine Node.js install directory"
24+
exit 1
25+
}
26+
$npmprefixclijs="$npmprefix/node_modules/npm/bin/npm-cli.js"
27+
28+
# Support pipeline input
29+
if ($MyInvocation.ExpectingInput) {
30+
$input | & $nodeexe $npmprefixclijs $args
31+
} else {
32+
& $nodeexe $npmprefixclijs $args
33+
}
34+
$ret=$LASTEXITCODE
35+
exit $ret

‎bin/npx.ps1

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/usr/bin/env pwsh
2+
$basedir=Split-Path $MyInvocation.MyCommand.Definition -Parent
3+
4+
$exe=""
5+
if ($PSVersionTable.PSVersion -lt "6.0" -or $IsWindows) {
6+
# Fix case when both the Windows and Linux builds of Node
7+
# are installed in the same directory
8+
$exe=".exe"
9+
}
10+
$ret=0
11+
12+
$nodeexe = "node$exe"
13+
$nodebin = $(Get-Command $nodeexe -ErrorAction SilentlyContinue -ErrorVariable F).Source
14+
if ($nodebin -eq $null) {
15+
Write-Host "$nodeexe not found."
16+
exit 1
17+
}
18+
$nodedir = $(New-Object -ComObject Scripting.FileSystemObject).GetFile("$nodebin").ParentFolder.Path
19+
20+
$npmclijs="$nodedir/node_modules/npm/bin/npm-cli.js"
21+
$npmprefix=(& $nodeexe $npmclijs prefix -g)
22+
if ($LASTEXITCODE -ne 0) {
23+
Write-Host "Could not determine Node.js install directory"
24+
exit 1
25+
}
26+
$npmprefixclijs="$npmprefix/node_modules/npm/bin/npx-cli.js"
27+
28+
# Support pipeline input
29+
if ($MyInvocation.ExpectingInput) {
30+
$input | & $nodeexe $npmprefixclijs $args
31+
} else {
32+
& $nodeexe $npmprefixclijs $args
33+
}
34+
$ret=$LASTEXITCODE
35+
exit $ret

‎test/bin/windows-shims.js

+20-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const t = require('tap')
22
const { spawnSync } = require('child_process')
3-
const { resolve, join, extname, basename } = require('path')
3+
const { resolve, join, extname, basename, sep } = require('path')
44
const { readFileSync, chmodSync, readdirSync } = require('fs')
55
const Diff = require('diff')
66
const { sync: which } = require('which')
@@ -18,6 +18,12 @@ const SHIMS = readdirSync(BIN).reduce((acc, shim) => {
1818

1919
const SHIM_EXTS = [...new Set(Object.keys(SHIMS).map(p => extname(p)))]
2020

21+
// windows requires each segment of a command path to be quoted when using shell: true
22+
const quotePath = (cmd) => cmd
23+
.split(sep)
24+
.map(p => p.includes(' ') ? `"${p}"` : p)
25+
.join(sep)
26+
2127
t.test('shim contents', t => {
2228
// these scripts should be kept in sync so this tests the contents of each
2329
// and does a diff to ensure the only differences between them are necessary
@@ -49,6 +55,13 @@ t.test('shim contents', t => {
4955
t.strictSame([...letters], ['M', 'X'], 'all other changes are m->x')
5056
t.end()
5157
})
58+
59+
t.test('pwsh', t => {
60+
const { diff, letters } = diffFiles(SHIMS['npm.ps1'], SHIMS['npx.ps1'])
61+
t.equal(diff.length, 0)
62+
t.strictSame([...letters], ['M', 'X'], 'all other changes are m->x')
63+
t.end()
64+
})
5265
})
5366

5467
t.test('run shims', t => {
@@ -133,6 +146,7 @@ t.test('run shims', t => {
133146

134147
const shells = Object.entries({
135148
cmd: 'cmd',
149+
pwsh: 'pwsh',
136150
git: join(ProgramFiles, 'Git', 'bin', 'bash.exe'),
137151
'user git': join(ProgramFiles, 'Git', 'usr', 'bin', 'bash.exe'),
138152
wsl: join(SystemRoot, 'System32', 'bash.exe'),
@@ -197,6 +211,11 @@ t.test('run shims', t => {
197211
case 'bash.exe':
198212
args.push(bin)
199213
break
214+
case 'pwsh.exe':
215+
cmd = quotePath(cmd)
216+
args.push(`${bin}.ps1`)
217+
opts.shell = true
218+
break
200219
default:
201220
throw new Error('unknown shell')
202221
}

0 commit comments

Comments
 (0)
Please sign in to comment.