Skip to content

Commit

Permalink
feat: support setting the NODE_PATH env variable
Browse files Browse the repository at this point in the history
  • Loading branch information
zkochan committed Jan 8, 2017
1 parent ce22069 commit 68dc8ce
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 10 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -37,6 +37,7 @@ The same as above, but will just continue if the file does not exist.
#### Arguments:

- `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.

```javascript
const cmdShim = require('@zkochan/cmd-shim')
Expand Down
22 changes: 12 additions & 10 deletions index.js
Expand Up @@ -43,7 +43,8 @@ function cmdShim_ (src, to, opts) {
}

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
// First, check if the bin is a #! of some sort.
// If not, then assume it's something that'll be compiled, or some other
Expand All @@ -54,12 +55,12 @@ function writeShim (src, to, opts) {
.then(data => {
const firstLine = data.trim().split(/\r*\n/)[0]
const shebang = firstLine.match(shebangExpr)
if (!shebang) return writeShim_(src, to, {args: 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, {args: defaultArgs}))
.catch(() => writeShim_(src, to, {args: defaultArgs, nodePath: opts.nodePath}))
})
}

Expand Down Expand Up @@ -93,17 +94,17 @@ function writeShim_ (src, to, opts) {
// SET PATHEXT=%PATHEXT:;.JS;=;%
// node "%~dp0\.\node_modules\npm\bin\npm-cli.js" %*
// )
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' +
') ELSE (\r\n' +
' @SETLOCAL\r\n' +
' @SET PATHEXT=%PATHEXT:;.JS;=;%\r\n' +
' ' + prog + ' ' + args + ' ' + target + ' %*\r\n' +
')'
} else {
cmd = `@${prog} ${args} ${target} %*\r\n`
cmd += `@${prog} ${args} ${target} %*\r\n`
}

// #!/bin/sh
Expand All @@ -123,6 +124,7 @@ function writeShim_ (src, to, opts) {
// exit $ret

let sh = '#!/bin/sh\n'
const env = opts.nodePath ? `NODE_PATH=${opts.nodePath} ` : ''

if (shLongProg) {
sh = sh +
Expand All @@ -135,15 +137,15 @@ function writeShim_ (src, to, opts) {

sh = sh +
'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' +
'fi\n' +
'exit $ret\n'
} else {
sh = shProg + ' ' + args + ' ' + shTarget + ' "$@"\n' +
sh = env + shProg + ' ' + args + ' ' + shTarget + ' "$@"\n' +
'exit $?\n'
}

Expand Down
38 changes: 38 additions & 0 deletions test/basic.js
Expand Up @@ -57,6 +57,44 @@ test('env shebang', function (t) {
})
})

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) {
const src = path.resolve(fixtures, 'src.env')
const to = path.resolve(fixtures, 'env.shim')
Expand Down

0 comments on commit 68dc8ce

Please sign in to comment.