Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Powershell - Windows - Fix resolving commands that do not have an exe file #52

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
88 changes: 29 additions & 59 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ const writeShim_ = (from, to, prog, args, variables) => {
let longProg
let shProg = prog && prog.split('\\').join('/')
let shLongProg
let pwshProg = shProg && `"${shProg}$exe"`
let pwshProg = shProg && `"${shProg}"`
let pwshLongProg
shTarget = shTarget.split('\\').join('/')
args = args || ''
Expand All @@ -76,7 +76,7 @@ const writeShim_ = (from, to, prog, args, variables) => {
} else {
longProg = `"%dp0%\\${prog}.exe"`
shLongProg = `"$basedir/${prog}"`
pwshLongProg = `"$basedir/${prog}$exe"`
pwshLongProg = `"$basedir/${prog}"`
target = `"%dp0%\\${target}"`
shTarget = `"$basedir/${shTarget}"`
}
Expand Down Expand Up @@ -153,71 +153,41 @@ const writeShim_ = (from, to, prog, args, variables) => {
// #!/usr/bin/env pwsh
// $basedir=Split-Path $MyInvocation.MyCommand.Definition -Parent
//
// $ret=0
// $exe = ""
// if ($PSVersionTable.PSVersion -lt "6.0" -or $IsWindows) {
// # Fix case when both the Windows and Linux builds of Node
// # are installed in the same directory
// $exe = ".exe"
// $nodepath="node"
// if ($IsWindows -and (Test-Path ("$basedir/node" + ".exe"))) {
// $nodepath="$basedir/node" + ".exe"
// } elseif (Test-Path "$basedir/node") {
// $nodepath="$basedir/node"
// }
// if (Test-Path "$basedir/node") {
// # Suport pipeline input
// if ($MyInvocation.ExpectingInput) {
// input | & "$basedir/node$exe" "$basedir/node_modules/npm/bin/npm-cli.js" $args
// } else {
// & "$basedir/node$exe" "$basedir/node_modules/npm/bin/npm-cli.js" $args
// }
// $ret=$LASTEXITCODE
//
// # Support pipeline input
// if ($MyInvocation.ExpectingInput) {
// $input | & $nodepath "$basedir/node_modules/npm/bin/npm-cli.js" $args
// } else {
// # Support pipeline input
// if ($MyInvocation.ExpectingInput) {
// $input | & "node$exe" "$basedir/node_modules/npm/bin/npm-cli.js" $args
// } else {
// & "node$exe" "$basedir/node_modules/npm/bin/npm-cli.js" $args
// }
// $ret=$LASTEXITCODE
// & $nodepath "$basedir/node_modules/npm/bin/npm-cli.js" $args
// }
// exit $ret
// exit $LASTEXITCODE
let pwsh = '#!/usr/bin/env pwsh\n'
+ '$basedir=Split-Path $MyInvocation.MyCommand.Definition -Parent\n'
+ '\n'
+ '$exe=""\n'
+ 'if ($PSVersionTable.PSVersion -lt \"6.0\" -or $IsWindows) {\n'
+ ' # Fix case when both the Windows and Linux builds of Node\n'
+ ' # are installed in the same directory\n'
+ ' $exe=".exe"\n'
+ '}\n'
if (shLongProg) {
pwsh = pwsh
+ '$ret=0\n'
+ `if (Test-Path ${pwshLongProg}) {\n`
+ ' # Support pipeline input\n'
+ ' if ($MyInvocation.ExpectingInput) {\n'
+ ` $input | & ${pwshLongProg} ${args} ${shTarget} $args\n`
+ ' } else {\n'
+ ` & ${pwshLongProg} ${args} ${shTarget} $args\n`
+ ' }\n'
+ ' $ret=$LASTEXITCODE\n'
+ '} else {\n'
+ ' # Support pipeline input\n'
+ ' if ($MyInvocation.ExpectingInput) {\n'
+ ` $input | & ${pwshProg} ${args} ${shTarget} $args\n`
+ ' } else {\n'
+ ` & ${pwshProg} ${args} ${shTarget} $args\n`
+ ' }\n'
+ ' $ret=$LASTEXITCODE\n'
+ '}\n'
+ 'exit $ret\n'
} else {
+ `$nodepath=${pwshProg}\n`
if (pwshLongProg) {
pwsh = pwsh
+ '# Support pipeline input\n'
+ 'if ($MyInvocation.ExpectingInput) {\n'
+ ` $input | & ${pwshProg} ${args} ${shTarget} $args\n`
+ '} else {\n'
+ ` & ${pwshProg} ${args} ${shTarget} $args\n`
+ '}\n'
+ 'exit $LASTEXITCODE\n'
+ `if ($IsWindows -and (Test-Path (${pwshLongProg} + ".exe"))) {\n`
+ ` $nodepath=${pwshLongProg} + ".exe"\n`
+ `} elseif (Test-Path ${pwshLongProg}) {\n`
+ ` $nodepath=${pwshLongProg}\n`
+ `}\n`;
}
pwsh = pwsh
+ '\n'
+ '# Support pipeline input\n'
+ 'if ($MyInvocation.ExpectingInput) {\n'
+ ` $input | & $nodepath ${args} ${shTarget} $args\n`
+ '} else {\n'
+ ` & $nodepath ${args} ${shTarget} $args\n`
+ '}\n'
+ 'exit $LASTEXITCODE\n'

return Promise.all([
writeFile(to + '.ps1', pwsh, 'utf8'),
Expand Down
201 changes: 67 additions & 134 deletions tap-snapshots/test-basic.js-TAP.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,31 +30,20 @@ exports[`test/basic.js TAP env shebang > ps1 1`] = `
#!/usr/bin/env pwsh
$basedir=Split-Path $MyInvocation.MyCommand.Definition -Parent

$exe=""
if ($PSVersionTable.PSVersion -lt "6.0" -or $IsWindows) {
# Fix case when both the Windows and Linux builds of Node
# are installed in the same directory
$exe=".exe"
$nodepath="node"
if ($IsWindows -and (Test-Path ("$basedir/node" + ".exe"))) {
$nodepath="$basedir/node" + ".exe"
} elseif (Test-Path "$basedir/node") {
$nodepath="$basedir/node"
}
$ret=0
if (Test-Path "$basedir/node$exe") {
# Support pipeline input
if ($MyInvocation.ExpectingInput) {
$input | & "$basedir/node$exe" "$basedir/from.env" $args
} else {
& "$basedir/node$exe" "$basedir/from.env" $args
}
$ret=$LASTEXITCODE

# Support pipeline input
if ($MyInvocation.ExpectingInput) {
$input | & $nodepath "$basedir/from.env" $args
} else {
# Support pipeline input
if ($MyInvocation.ExpectingInput) {
$input | & "node$exe" "$basedir/from.env" $args
} else {
& "node$exe" "$basedir/from.env" $args
}
$ret=$LASTEXITCODE
& $nodepath "$basedir/from.env" $args
}
exit $ret
exit $LASTEXITCODE

`

Expand Down Expand Up @@ -99,31 +88,20 @@ exports[`test/basic.js TAP env shebang with args > ps1 1`] = `
#!/usr/bin/env pwsh
$basedir=Split-Path $MyInvocation.MyCommand.Definition -Parent

$exe=""
if ($PSVersionTable.PSVersion -lt "6.0" -or $IsWindows) {
# Fix case when both the Windows and Linux builds of Node
# are installed in the same directory
$exe=".exe"
$nodepath="node"
if ($IsWindows -and (Test-Path ("$basedir/node" + ".exe"))) {
$nodepath="$basedir/node" + ".exe"
} elseif (Test-Path "$basedir/node") {
$nodepath="$basedir/node"
}
$ret=0
if (Test-Path "$basedir/node$exe") {
# Support pipeline input
if ($MyInvocation.ExpectingInput) {
$input | & "$basedir/node$exe" --expose_gc "$basedir/from.env.args" $args
} else {
& "$basedir/node$exe" --expose_gc "$basedir/from.env.args" $args
}
$ret=$LASTEXITCODE

# Support pipeline input
if ($MyInvocation.ExpectingInput) {
$input | & $nodepath --expose_gc "$basedir/from.env.args" $args
} else {
# Support pipeline input
if ($MyInvocation.ExpectingInput) {
$input | & "node$exe" --expose_gc "$basedir/from.env.args" $args
} else {
& "node$exe" --expose_gc "$basedir/from.env.args" $args
}
$ret=$LASTEXITCODE
& $nodepath --expose_gc "$basedir/from.env.args" $args
}
exit $ret
exit $LASTEXITCODE

`

Expand Down Expand Up @@ -169,31 +147,20 @@ exports[`test/basic.js TAP env shebang with variables > ps1 1`] = `
#!/usr/bin/env pwsh
$basedir=Split-Path $MyInvocation.MyCommand.Definition -Parent

$exe=""
if ($PSVersionTable.PSVersion -lt "6.0" -or $IsWindows) {
# Fix case when both the Windows and Linux builds of Node
# are installed in the same directory
$exe=".exe"
$nodepath="node"
if ($IsWindows -and (Test-Path ("$basedir/node" + ".exe"))) {
$nodepath="$basedir/node" + ".exe"
} elseif (Test-Path "$basedir/node") {
$nodepath="$basedir/node"
}
$ret=0
if (Test-Path "$basedir/node$exe") {
# Support pipeline input
if ($MyInvocation.ExpectingInput) {
$input | & "$basedir/node$exe" "$basedir/from.env.variables" $args
} else {
& "$basedir/node$exe" "$basedir/from.env.variables" $args
}
$ret=$LASTEXITCODE

# Support pipeline input
if ($MyInvocation.ExpectingInput) {
$input | & $nodepath "$basedir/from.env.variables" $args
} else {
# Support pipeline input
if ($MyInvocation.ExpectingInput) {
$input | & "node$exe" "$basedir/from.env.variables" $args
} else {
& "node$exe" "$basedir/from.env.variables" $args
}
$ret=$LASTEXITCODE
& $nodepath "$basedir/from.env.variables" $args
}
exit $ret
exit $LASTEXITCODE

`

Expand Down Expand Up @@ -238,31 +205,20 @@ exports[`test/basic.js TAP explicit shebang > ps1 1`] = `
#!/usr/bin/env pwsh
$basedir=Split-Path $MyInvocation.MyCommand.Definition -Parent

$exe=""
if ($PSVersionTable.PSVersion -lt "6.0" -or $IsWindows) {
# Fix case when both the Windows and Linux builds of Node
# are installed in the same directory
$exe=".exe"
$nodepath="/usr/bin/sh"
if ($IsWindows -and (Test-Path ("$basedir//usr/bin/sh" + ".exe"))) {
$nodepath="$basedir//usr/bin/sh" + ".exe"
} elseif (Test-Path "$basedir//usr/bin/sh") {
$nodepath="$basedir//usr/bin/sh"
}
$ret=0
if (Test-Path "$basedir//usr/bin/sh$exe") {
# Support pipeline input
if ($MyInvocation.ExpectingInput) {
$input | & "$basedir//usr/bin/sh$exe" "$basedir/from.sh" $args
} else {
& "$basedir//usr/bin/sh$exe" "$basedir/from.sh" $args
}
$ret=$LASTEXITCODE

# Support pipeline input
if ($MyInvocation.ExpectingInput) {
$input | & $nodepath "$basedir/from.sh" $args
} else {
# Support pipeline input
if ($MyInvocation.ExpectingInput) {
$input | & "/usr/bin/sh$exe" "$basedir/from.sh" $args
} else {
& "/usr/bin/sh$exe" "$basedir/from.sh" $args
}
$ret=$LASTEXITCODE
& $nodepath "$basedir/from.sh" $args
}
exit $ret
exit $LASTEXITCODE

`

Expand Down Expand Up @@ -307,31 +263,20 @@ exports[`test/basic.js TAP explicit shebang with args > ps1 1`] = `
#!/usr/bin/env pwsh
$basedir=Split-Path $MyInvocation.MyCommand.Definition -Parent

$exe=""
if ($PSVersionTable.PSVersion -lt "6.0" -or $IsWindows) {
# Fix case when both the Windows and Linux builds of Node
# are installed in the same directory
$exe=".exe"
$nodepath="/usr/bin/sh"
if ($IsWindows -and (Test-Path ("$basedir//usr/bin/sh" + ".exe"))) {
$nodepath="$basedir//usr/bin/sh" + ".exe"
} elseif (Test-Path "$basedir//usr/bin/sh") {
$nodepath="$basedir//usr/bin/sh"
}
$ret=0
if (Test-Path "$basedir//usr/bin/sh$exe") {
# Support pipeline input
if ($MyInvocation.ExpectingInput) {
$input | & "$basedir//usr/bin/sh$exe" -x "$basedir/from.sh.args" $args
} else {
& "$basedir//usr/bin/sh$exe" -x "$basedir/from.sh.args" $args
}
$ret=$LASTEXITCODE

# Support pipeline input
if ($MyInvocation.ExpectingInput) {
$input | & $nodepath -x "$basedir/from.sh.args" $args
} else {
# Support pipeline input
if ($MyInvocation.ExpectingInput) {
$input | & "/usr/bin/sh$exe" -x "$basedir/from.sh.args" $args
} else {
& "/usr/bin/sh$exe" -x "$basedir/from.sh.args" $args
}
$ret=$LASTEXITCODE
& $nodepath -x "$basedir/from.sh.args" $args
}
exit $ret
exit $LASTEXITCODE

`

Expand Down Expand Up @@ -368,17 +313,13 @@ exports[`test/basic.js TAP if exists (it does exist) > ps1 1`] = `
#!/usr/bin/env pwsh
$basedir=Split-Path $MyInvocation.MyCommand.Definition -Parent

$exe=""
if ($PSVersionTable.PSVersion -lt "6.0" -or $IsWindows) {
# Fix case when both the Windows and Linux builds of Node
# are installed in the same directory
$exe=".exe"
}
$nodepath="$basedir/from.exe"

# Support pipeline input
if ($MyInvocation.ExpectingInput) {
$input | & "$basedir/from.exe" $args
$input | & $nodepath $args
} else {
& "$basedir/from.exe" $args
& $nodepath $args
}
exit $LASTEXITCODE

Expand Down Expand Up @@ -413,17 +354,13 @@ exports[`test/basic.js TAP just proceed if reading fails > ps1 1`] = `
#!/usr/bin/env pwsh
$basedir=Split-Path $MyInvocation.MyCommand.Definition -Parent

$exe=""
if ($PSVersionTable.PSVersion -lt "6.0" -or $IsWindows) {
# Fix case when both the Windows and Linux builds of Node
# are installed in the same directory
$exe=".exe"
}
$nodepath="$basedir/"

# Support pipeline input
if ($MyInvocation.ExpectingInput) {
$input | & "$basedir/" $args
$input | & $nodepath $args
} else {
& "$basedir/" $args
& $nodepath $args
}
exit $LASTEXITCODE

Expand Down Expand Up @@ -458,17 +395,13 @@ exports[`test/basic.js TAP no shebang > ps1 1`] = `
#!/usr/bin/env pwsh
$basedir=Split-Path $MyInvocation.MyCommand.Definition -Parent

$exe=""
if ($PSVersionTable.PSVersion -lt "6.0" -or $IsWindows) {
# Fix case when both the Windows and Linux builds of Node
# are installed in the same directory
$exe=".exe"
}
$nodepath="$basedir/from.exe"

# Support pipeline input
if ($MyInvocation.ExpectingInput) {
$input | & "$basedir/from.exe" $args
$input | & $nodepath $args
} else {
& "$basedir/from.exe" $args
& $nodepath $args
}
exit $LASTEXITCODE

Expand Down