Skip to content

Commit

Permalink
fix: restructure .cmd to yield correct errorlevel
Browse files Browse the repository at this point in the history
PR-URL: #46
Credit: @staudenmayerc
Close: #46
Reviewed-by: @isaacs
  • Loading branch information
staudenmayerc authored and isaacs committed Feb 5, 2021
1 parent ebcae48 commit 98176d0
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 47 deletions.
13 changes: 6 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,13 @@ const writeShim_ = (from, to, prog, args, variables) => {
//
// Subroutine trick to fix https://github.com/npm/cmd-shim/issues/10
const head = '@ECHO off\r\n' +
'SETLOCAL\r\n' +
'CALL :find_dp0\r\n'
const foot = 'ENDLOCAL\r\n' +
'EXIT /b %errorlevel%\r\n' +
'GOTO start\r\n' +
':find_dp0\r\n' +
'SET dp0=%~dp0\r\n' +
'EXIT /b\r\n'
'EXIT /b\r\n' +
':start\r\n' +
'SETLOCAL\r\n' +
'CALL :find_dp0\r\n'

let cmd
if (longProg) {
Expand All @@ -125,9 +125,8 @@ const writeShim_ = (from, to, prog, args, variables) => {
+ ')\r\n'
+ '\r\n'
+ `"%_prog%" ${args} ${target} %*\r\n`
+ foot
} else {
cmd = `${head}${prog} ${args} ${target} %*\r\n${foot}`
cmd = `${head}${prog} ${args} ${target} %*\r\n`
}

// #!/bin/sh
Expand Down
80 changes: 40 additions & 40 deletions tap-snapshots/test-basic.js-TAP.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
'use strict'
exports[`test/basic.js TAP env shebang > cmd 1`] = `
@ECHO off\\r
GOTO start\\r
:find_dp0\\r
SET dp0=%~dp0\\r
EXIT /b\\r
:start\\r
SETLOCAL\\r
CALL :find_dp0\\r
\\r
Expand All @@ -18,11 +23,6 @@ IF EXIST "%dp0%\\node.exe" (\\r
)\\r
\\r
"%_prog%" "%dp0%\\from.env" %*\\r
ENDLOCAL\\r
EXIT /b %errorlevel%\\r
:find_dp0\\r
SET dp0=%~dp0\\r
EXIT /b\\r
`

Expand Down Expand Up @@ -79,6 +79,11 @@ exit $ret

exports[`test/basic.js TAP env shebang with args > cmd 1`] = `
@ECHO off\\r
GOTO start\\r
:find_dp0\\r
SET dp0=%~dp0\\r
EXIT /b\\r
:start\\r
SETLOCAL\\r
CALL :find_dp0\\r
\\r
Expand All @@ -90,11 +95,6 @@ IF EXIST "%dp0%\\node.exe" (\\r
)\\r
\\r
"%_prog%" --expose_gc "%dp0%\\from.env.args" %*\\r
ENDLOCAL\\r
EXIT /b %errorlevel%\\r
:find_dp0\\r
SET dp0=%~dp0\\r
EXIT /b\\r
`

Expand Down Expand Up @@ -151,6 +151,11 @@ exit $ret

exports[`test/basic.js TAP env shebang with variables > cmd 1`] = `
@ECHO off\\r
GOTO start\\r
:find_dp0\\r
SET dp0=%~dp0\\r
EXIT /b\\r
:start\\r
SETLOCAL\\r
CALL :find_dp0\\r
@SET NODE_PATH=./lib:%NODE_PATH%\\r
Expand All @@ -163,11 +168,6 @@ IF EXIST "%dp0%\\node.exe" (\\r
)\\r
\\r
"%_prog%" "%dp0%\\from.env.variables" %*\\r
ENDLOCAL\\r
EXIT /b %errorlevel%\\r
:find_dp0\\r
SET dp0=%~dp0\\r
EXIT /b\\r
`

Expand Down Expand Up @@ -224,6 +224,11 @@ exit $ret

exports[`test/basic.js TAP explicit shebang > cmd 1`] = `
@ECHO off\\r
GOTO start\\r
:find_dp0\\r
SET dp0=%~dp0\\r
EXIT /b\\r
:start\\r
SETLOCAL\\r
CALL :find_dp0\\r
\\r
Expand All @@ -235,11 +240,6 @@ IF EXIST "%dp0%\\/usr/bin/sh.exe" (\\r
)\\r
\\r
"%_prog%" "%dp0%\\from.sh" %*\\r
ENDLOCAL\\r
EXIT /b %errorlevel%\\r
:find_dp0\\r
SET dp0=%~dp0\\r
EXIT /b\\r
`

Expand Down Expand Up @@ -296,6 +296,11 @@ exit $ret

exports[`test/basic.js TAP explicit shebang with args > cmd 1`] = `
@ECHO off\\r
GOTO start\\r
:find_dp0\\r
SET dp0=%~dp0\\r
EXIT /b\\r
:start\\r
SETLOCAL\\r
CALL :find_dp0\\r
\\r
Expand All @@ -307,11 +312,6 @@ IF EXIST "%dp0%\\/usr/bin/sh.exe" (\\r
)\\r
\\r
"%_prog%" -x "%dp0%\\from.sh.args" %*\\r
ENDLOCAL\\r
EXIT /b %errorlevel%\\r
:find_dp0\\r
SET dp0=%~dp0\\r
EXIT /b\\r
`

Expand Down Expand Up @@ -368,14 +368,14 @@ exit $ret

exports[`test/basic.js TAP if exists (it does exist) > cmd 1`] = `
@ECHO off\\r
SETLOCAL\\r
CALL :find_dp0\\r
"%dp0%\\from.exe" %*\\r
ENDLOCAL\\r
EXIT /b %errorlevel%\\r
GOTO start\\r
:find_dp0\\r
SET dp0=%~dp0\\r
EXIT /b\\r
:start\\r
SETLOCAL\\r
CALL :find_dp0\\r
"%dp0%\\from.exe" %*\\r
`

Expand Down Expand Up @@ -414,14 +414,14 @@ exit $?

exports[`test/basic.js TAP just proceed if reading fails > cmd 1`] = `
@ECHO off\\r
SETLOCAL\\r
CALL :find_dp0\\r
"%dp0%\\" %*\\r
ENDLOCAL\\r
EXIT /b %errorlevel%\\r
GOTO start\\r
:find_dp0\\r
SET dp0=%~dp0\\r
EXIT /b\\r
:start\\r
SETLOCAL\\r
CALL :find_dp0\\r
"%dp0%\\" %*\\r
`

Expand Down Expand Up @@ -460,14 +460,14 @@ exit $?

exports[`test/basic.js TAP no shebang > cmd 1`] = `
@ECHO off\\r
SETLOCAL\\r
CALL :find_dp0\\r
"%dp0%\\from.exe" %*\\r
ENDLOCAL\\r
EXIT /b %errorlevel%\\r
GOTO start\\r
:find_dp0\\r
SET dp0=%~dp0\\r
EXIT /b\\r
:start\\r
SETLOCAL\\r
CALL :find_dp0\\r
"%dp0%\\from.exe" %*\\r
`

Expand Down

0 comments on commit 98176d0

Please sign in to comment.