@zkochan/cmd-shim
Advanced tools
Comparing version 2.0.1 to 2.1.0
28
index.js
@@ -46,3 +46,4 @@ 'use strict' | ||
function writeShim (src, to, opts) { | ||
const defaultArgs = opts && opts.preserveSymlinks ? '--preserve-symlinks' : '' | ||
opts = opts || {} | ||
const defaultArgs = opts.preserveSymlinks ? '--preserve-symlinks' : '' | ||
// make a cmd file and a sh script | ||
@@ -58,19 +59,21 @@ // First, check if the bin is a #! of some sort. | ||
const shebang = firstLine.match(shebangExpr) | ||
if (!shebang) return writeShim_(src, to, null, defaultArgs) | ||
if (!shebang) return writeShim_(src, to, {args: defaultArgs, nodePath: opts.nodePath}) | ||
const prog = shebang[1] | ||
const args = shebang[2] && (defaultArgs && (shebang[2] + ' ' + defaultArgs) || shebang[2]) || defaultArgs | ||
return writeShim_(src, to, prog, args) | ||
return writeShim_(src, to, {prog, args, nodePath: opts.nodePath}) | ||
}) | ||
.catch(() => writeShim_(src, to, null, defaultArgs)) | ||
.catch(() => writeShim_(src, to, {args: defaultArgs, nodePath: opts.nodePath})) | ||
}) | ||
} | ||
function writeShim_ (src, to, prog, args) { | ||
function writeShim_ (src, to, opts) { | ||
opts = opts || {} | ||
let shTarget = path.relative(path.dirname(to), src) | ||
let target = shTarget.split('/').join('\\') | ||
let longProg | ||
let prog = opts.prog | ||
let shProg = prog && prog.split('\\').join('/') | ||
let shLongProg | ||
shTarget = shTarget.split('\\').join('/') | ||
args = args || '' | ||
let args = opts.args || '' | ||
if (!prog) { | ||
@@ -96,5 +99,5 @@ prog = `"%~dp0\\${target}"` | ||
// ) | ||
let cmd | ||
let cmd = opts.nodePath ? `@SET NODE_PATH=${opts.nodePath}\r\n` : '' | ||
if (longProg) { | ||
cmd = '@IF EXIST ' + longProg + ' (\r\n' + | ||
cmd += '@IF EXIST ' + longProg + ' (\r\n' + | ||
' ' + longProg + ' ' + args + ' ' + target + ' %*\r\n' + | ||
@@ -107,3 +110,3 @@ ') ELSE (\r\n' + | ||
} else { | ||
cmd = `@${prog} ${args} ${target} %*\r\n` | ||
cmd += `@${prog} ${args} ${target} %*\r\n` | ||
} | ||
@@ -128,2 +131,3 @@ | ||
let sh = '#!/bin/sh\n' | ||
const env = opts.nodePath ? `NODE_PATH=${opts.nodePath} ` : '' | ||
@@ -141,6 +145,6 @@ if (shLongProg) { | ||
'if [ -x ' + shLongProg + ' ]; then\n' + | ||
' ' + shLongProg + ' ' + args + ' ' + shTarget + ' "$@"\n' + | ||
' ' + env + shLongProg + ' ' + args + ' ' + shTarget + ' "$@"\n' + | ||
' ret=$?\n' + | ||
'else \n' + | ||
' ' + shProg + ' ' + args + ' ' + shTarget + ' "$@"\n' + | ||
' ' + env + shProg + ' ' + args + ' ' + shTarget + ' "$@"\n' + | ||
' ret=$?\n' + | ||
@@ -150,3 +154,3 @@ 'fi\n' + | ||
} else { | ||
sh = shProg + ' ' + args + ' ' + shTarget + ' "$@"\n' + | ||
sh = env + shProg + ' ' + args + ' ' + shTarget + ' "$@"\n' + | ||
'exit $?\n' | ||
@@ -153,0 +157,0 @@ } |
{ | ||
"name": "@zkochan/cmd-shim", | ||
"version": "2.0.1", | ||
"version": "2.1.0", | ||
"description": "Used in pnpm for command line application support", | ||
@@ -5,0 +5,0 @@ "author": { |
@@ -40,2 +40,3 @@ # @zkochan/cmd-shim | ||
- `opts.preserveSymlinks` - _Boolean_ - if true, `--preserve-symlinks` is added to the options passed to NodeJS. | ||
- `opts.nodePath` - _String_ - sets the [NODE_PATH](https://nodejs.org/api/cli.html#cli_node_path_path) env variable. | ||
@@ -42,0 +43,0 @@ ```javascript |
@@ -60,2 +60,40 @@ 'use strict' | ||
test('env shebang with NODE_PATH', function (t) { | ||
const src = path.resolve(fixtures, 'src.env') | ||
const to = path.resolve(fixtures, 'env.shim') | ||
return cmdShim(src, to, {nodePath: '/john/src/node_modules'}) | ||
.then(() => { | ||
console.error('%j', fs.readFileSync(to, 'utf8')) | ||
console.error('%j', fs.readFileSync(to + '.cmd', 'utf8')) | ||
t.equal(fs.readFileSync(to, 'utf8'), | ||
'#!/bin/sh' + | ||
"\nbasedir=$(dirname \"$(echo \"$0\" | sed -e 's,\\\\,/,g')\")" + | ||
'\n' + | ||
'\ncase `uname` in' + | ||
'\n *CYGWIN*) basedir=`cygpath -w "$basedir"`;;' + | ||
'\nesac' + | ||
'\n' + | ||
'\nif [ -x "$basedir/node" ]; then' + | ||
'\n NODE_PATH=/john/src/node_modules "$basedir/node" "$basedir/src.env" "$@"' + | ||
'\n ret=$?' + | ||
'\nelse ' + | ||
'\n NODE_PATH=/john/src/node_modules node "$basedir/src.env" "$@"' + | ||
'\n ret=$?' + | ||
'\nfi' + | ||
'\nexit $ret' + | ||
'\n') | ||
t.equal(fs.readFileSync(to + '.cmd', 'utf8'), | ||
'@SET NODE_PATH=/john/src/node_modules\r' + | ||
'\n@IF EXIST "%~dp0\\node.exe" (\r' + | ||
'\n "%~dp0\\node.exe" "%~dp0\\src.env" %*\r' + | ||
'\n) ELSE (\r' + | ||
'\n @SETLOCAL\r' + | ||
'\n @SET PATHEXT=%PATHEXT:;.JS;=;%\r' + | ||
'\n node "%~dp0\\src.env" %*\r' + | ||
'\n)') | ||
t.end() | ||
}) | ||
}) | ||
test('env shebang with default args', function (t) { | ||
@@ -62,0 +100,0 @@ const src = path.resolve(fixtures, 'src.env') |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
19155
422
51