From 2d277f8e84d45401747b0b9470058f168b974ad5 Mon Sep 17 00:00:00 2001 From: Igor Klopov Date: Sun, 9 Jul 2017 14:07:01 +0300 Subject: [PATCH] fixes 'no shebang' case PR-URL: https://github.com/npm/cmd-shim/pull/25 Credit: @igorklopov Close: #25 Reviewed-by: @isaacs Close: https://github.com/npm/cmd-shim/pull/36 Fix: https://github.com/npm/cmd-shim/pull/35 --- index.js | 19 ++++++++++--------- test/basic.js | 9 ++++++++- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/index.js b/index.js index 79bb14e..e0c3e99 100644 --- a/index.js +++ b/index.js @@ -140,15 +140,15 @@ function writeShim_ (from, to, prog, args, variables, cb) { var sh = "#!/bin/sh\n" - if (shLongProg) { - sh = sh - + "basedir=$(dirname \"$(echo \"$0\" | sed -e 's,\\\\,/,g')\")\n" - + "\n" - + "case `uname` in\n" - + " *CYGWIN*) basedir=`cygpath -w \"$basedir\"`;;\n" - + "esac\n" - + "\n" + sh = sh + + "basedir=$(dirname \"$(echo \"$0\" | sed -e 's,\\\\,/,g')\")\n" + + "\n" + + "case `uname` in\n" + + " *CYGWIN*) basedir=`cygpath -w \"$basedir\"`;;\n" + + "esac\n" + + "\n" + if (shLongProg) { sh = sh + "if [ -x "+shLongProg+" ]; then\n" + " " + variables + shLongProg + " " + args + " " + shTarget + " \"$@\"\n" @@ -159,7 +159,8 @@ function writeShim_ (from, to, prog, args, variables, cb) { + "fi\n" + "exit $ret\n" } else { - sh = shProg + " " + args + " " + shTarget + " \"$@\"\n" + sh = sh + + shProg + " " + args + " " + shTarget + " \"$@\"\n" + "exit $?\n" } diff --git a/test/basic.js b/test/basic.js index ae1e0c5..f28575e 100755 --- a/test/basic.js +++ b/test/basic.js @@ -13,7 +13,14 @@ test('no shebang', function (t) { if (er) throw er t.equal(fs.readFileSync(to, 'utf8'), - "\"$basedir/from.exe\" \"$@\"\nexit $?\n") + "#!/bin/sh"+ + "\nbasedir=$(dirname \"$(echo \"$0\" | sed -e 's,\\\\,/,g')\")"+ + "\n"+ + "\ncase `uname` in"+ + "\n *CYGWIN*) basedir=`cygpath -w \"$basedir\"`;;"+ + "\nesac"+ + "\n"+ + "\n\"$basedir/from.exe\" \"$@\"\nexit $?\n") t.equal(fs.readFileSync(to + '.cmd', 'utf8'), "@\"%~dp0\\from.exe\" %*\r\n") t.end()