Skip to content

Commit

Permalink
Work around quoted batch file names
Browse files Browse the repository at this point in the history
Fix: #10

Read %~dp0 in a subroutine, so that it does not get an incorrect
value when the command name is quoted.

While we're at it, bring this module up to 100% test coverage,
and fix a few obvious bugs that full coverage uncovered.

BREAKING CHANGE: This requires an update to read-cmd-shim, because the
format of the command invocation changed, and it reads that out of the
generated batch script.
  • Loading branch information
isaacs committed Aug 14, 2019
1 parent 9cc5388 commit 4c37e04
Show file tree
Hide file tree
Showing 6 changed files with 570 additions and 277 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ results
npm-debug.log

node_modules
/coverage
/.nyc_output
52 changes: 35 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// "#!<prog> <args...>"
//
// Write a binroot/pkg.bin + ".cmd" file that has this line in it:
// @<prog> <args...> %~dp0<target> %*
// @<prog> <args...> %dp0%<target> %*

module.exports = cmdShim
cmdShim.ifExists = cmdShimIfExists
Expand Down Expand Up @@ -43,9 +43,10 @@ function cmdShim (from, to, cb) {
}

function cmdShim_ (from, to, cb) {
var then = times(2, next, cb)
var then = times(3, next, cb)
rm(to, then)
rm(to + ".cmd", then)
rm(to + ".ps1", then)

function next(er) {
writeShim(from, to, cb)
Expand All @@ -61,7 +62,7 @@ function writeShim (from, to, cb) {
if (er)
return cb(er)
fs.readFile(from, "utf8", function (er, data) {
if (er) return writeShim_(from, to, null, null, cb)
if (er) return writeShim_(from, to, null, null, null, cb)
var firstLine = data.trim().split(/\r*\n/)[0]
, shebang = firstLine.match(shebangExpr)
if (!shebang) return writeShim_(from, to, null, null, null, cb)
Expand All @@ -86,50 +87,67 @@ function writeShim_ (from, to, prog, args, variables, cb) {
args = args || ""
variables = variables || ""
if (!prog) {
prog = "\"%~dp0\\" + target + "\""
prog = "\"%dp0%\\" + target + "\""
shProg = "\"$basedir/" + shTarget + "\""
pwshProg = shProg
args = ""
target = ""
shTarget = ""
} else {
longProg = "\"%~dp0\\" + prog + ".exe\""
longProg = "\"%dp0%\\" + prog + ".exe\""
shLongProg = "\"$basedir/" + prog + "\""
pwshLongProg = "\"$basedir/" + prog + "$exe\""
target = "\"%~dp0\\" + target + "\""
target = "\"%dp0%\\" + target + "\""
shTarget = "\"$basedir/" + shTarget + "\""
}

// @SETLOCAL
// @CALL :find_dp0
//
// @IF EXIST "%~dp0\node.exe" (
// @SET "_prog=%~dp0\node.exe"
// @IF EXIST "%dp0%\node.exe" (
// @SET "_prog=%dp0%\node.exe"
// ) ELSE (
// @SET "_prog=node"
// @SET PATHEXT=%PATHEXT:;.JS;=;%
// )
//
// "%_prog%" "%~dp0\.\node_modules\npm\bin\npm-cli.js" %*
// "%_prog%" "%dp0%\.\node_modules\npm\bin\npm-cli.js" %*
// @ENDLOCAL
// @EXIT /b
//
// :find_dp0
// SET dp0=%~dp0
// EXIT /b
//
// Subroutine trick to fix https://github.com/npm/cmd-shim/issues/10
var head = '@ECHO off\r\n' +
'SETLOCAL\r\n' +
'CALL find_dp0\r\n'
var foot = 'ENDLOCAL\r\n' +
'EXIT /b\r\n' +
'find_dp0:\r\n' +
'SET dp0=%~dp0\r\n' +
'EXIT /b\r\n'

var cmd
if (longProg) {
shLongProg = shLongProg.trim();
args = args.trim();
var variableDeclarationsAsBatch = toBatchSyntax.convertToSetCommands(variables)
cmd = "@SETLOCAL\r\n"
cmd = head
+ variableDeclarationsAsBatch
+ "\r\n"
+ "@IF EXIST " + longProg + " (\r\n"
+ " @SET \"_prog=" + longProg.replace(/(^")|("$)/g, '') + "\"\r\n"
+ "IF EXIST " + longProg + " (\r\n"
+ " SET \"_prog=" + longProg.replace(/(^")|("$)/g, '') + "\"\r\n"
+ ") ELSE (\r\n"
+ " @SET \"_prog=" + prog.replace(/(^")|("$)/g, '') + "\"\r\n"
+ " @SET PATHEXT=%PATHEXT:;.JS;=;%\r\n"
+ " SET \"_prog=" + prog.replace(/(^")|("$)/g, '') + "\"\r\n"
+ " SET PATHEXT=%PATHEXT:;.JS;=;%\r\n"
+ ")\r\n"
+ "\r\n"
+ "\"%_prog%\" " + args + " " + target + " %*\r\n"
+ '@ENDLOCAL\r\n'
+ foot
} else {
cmd = "@" + prog + " " + args + " " + target + " %*\r\n"
cmd = head + prog + " " + args + " " + target + " %*\r\n" + foot
}

// #!/bin/sh
Expand Down Expand Up @@ -228,7 +246,7 @@ function writeShim_ (from, to, prog, args, variables, cb) {
}

function chmodShim (to, cb) {
var then = times(2, cb, cb)
var then = times(3, cb, cb)
fs.chmod(to, "0755", then)
fs.chmod(to + ".cmd", "0755", then)
fs.chmod(to + ".ps1", "0755", then)
Expand Down
1 change: 0 additions & 1 deletion lib/to-batch-syntax.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ function replaceDollarWithPercentPair(value) {
var dollarExpressions = /\$\{?([^\$@#\?\- \t{}:]+)\}?/g
var result = ""
var startIndex = 0
value = value || ""
do {
var match = dollarExpressions.exec(value)
if(match) {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"version": "2.1.0",
"description": "Used in npm for command line application support",
"scripts": {
"test": "tap test/*.js"
"test": "tap test/*.js --100",
"snap": "TAP_SNAPSHOT=1 tap test/*.js --100"
},
"repository": {
"type": "git",
Expand Down
Loading

0 comments on commit 4c37e04

Please sign in to comment.