Skip to content

Commit

Permalink
Update deps and coding style, require node >= 10
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Jan 27, 2020
1 parent bf5df11 commit c5f9b8e
Show file tree
Hide file tree
Showing 6 changed files with 2,286 additions and 4,221 deletions.
238 changes: 114 additions & 124 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,97 +8,86 @@
// Write a binroot/pkg.bin + ".cmd" file that has this line in it:
// @<prog> <args...> %dp0%<target> %*

module.exports = cmdShim
cmdShim.ifExists = cmdShimIfExists
const fs = require('fs')

var fs = require("graceful-fs")
const mkdir = require('mkdirp')
const path = require('path')
const toBatchSyntax = require('./lib/to-batch-syntax')
const shebangExpr = /^#\!\s*(?:\/usr\/bin\/env)?\s*([^ \t]+=[^ \t]+\s+)*\s*([^ \t]+)(.*)$/

var mkdir = require("mkdirp")
, path = require("path")
, toBatchSyntax = require("./lib/to-batch-syntax")
, shebangExpr = /^#\!\s*(?:\/usr\/bin\/env)?\s*([^ \t]+=[^ \t]+\s+)*\s*([^ \t]+)(.*)$/

function cmdShimIfExists (from, to, cb) {
fs.stat(from, function (er) {
const cmdShimIfExists = (from, to, cb) => {
fs.stat(from, er => {
if (er) return cb()
cmdShim(from, to, cb)
})
}

// Try to unlink, but ignore errors.
// Any problems will surface later.
function rm (path, cb) {
fs.unlink(path, function(er) {
cb()
})
}
const rm = (path, cb) => fs.unlink(path, () => cb())

function cmdShim (from, to, cb) {
fs.stat(from, function (er, stat) {
const cmdShim = (from, to, cb) => {
fs.stat(from, (er, stat) => {
if (er)
return cb(er)

cmdShim_(from, to, cb)
})
}

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

function next(er) {
writeShim(from, to, cb)
}
rm(to, then)
rm(to + '.cmd', then)
rm(to + '.ps1', then)
}

function writeShim (from, to, cb) {
const writeShim = (from, to, cb) => {
// 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
// sort of script, and just call it directly.
mkdir(path.dirname(to), function (er) {
if (er)
return cb(er)
fs.readFile(from, "utf8", function (er, data) {
mkdir(path.dirname(to)).then(() => {
fs.readFile(from, 'utf8', (er, data) => {
if (er) return writeShim_(from, to, null, null, null, cb)
var firstLine = data.trim().split(/\r*\n/)[0]
, shebang = firstLine.match(shebangExpr)
const firstLine = data.trim().split(/\r*\n/)[0]
const shebang = firstLine.match(shebangExpr)
if (!shebang) return writeShim_(from, to, null, null, null, cb)
var vars = shebang[1] || ""
, prog = shebang[2]
, args = shebang[3] || ""
const vars = shebang[1] || ''
const prog = shebang[2]
const args = shebang[3] || ''
return writeShim_(from, to, prog, args, vars, cb)
})
})
}, cb)
}


function writeShim_ (from, to, prog, args, variables, cb) {
var shTarget = path.relative(path.dirname(to), from)
, target = shTarget.split("/").join("\\")
, longProg
, shProg = prog && prog.split("\\").join("/")
, shLongProg
, pwshProg = shProg && "\"" + shProg + "$exe\""
, pwshLongProg
shTarget = shTarget.split("\\").join("/")
args = args || ""
variables = variables || ""
const writeShim_ = (from, to, prog, args, variables, cb) => {
let shTarget = path.relative(path.dirname(to), from)
let target = shTarget.split('/').join('\\')
let longProg
let shProg = prog && prog.split('\\').join('/')
let shLongProg
let pwshProg = shProg && `"${shProg}$exe"`
let pwshLongProg
shTarget = shTarget.split('\\').join('/')
args = args || ''
variables = variables || ''
if (!prog) {
prog = "\"%dp0%\\" + target + "\""
shProg = "\"$basedir/" + shTarget + "\""
prog = `"%dp0%\\${target}"`
shProg = `"$basedir/${shTarget}"`
pwshProg = shProg
args = ""
target = ""
shTarget = ""
args = ''
target = ''
shTarget = ''
} else {
longProg = "\"%dp0%\\" + prog + ".exe\""
shLongProg = "\"$basedir/" + prog + "\""
pwshLongProg = "\"$basedir/" + prog + "$exe\""
target = "\"%dp0%\\" + target + "\""
shTarget = "\"$basedir/" + shTarget + "\""
longProg = `"%dp0%\\${prog}.exe"`
shLongProg = `"$basedir/${prog}"`
pwshLongProg = `"$basedir/${prog}$exe"`
target = `"%dp0%\\${target}"`
shTarget = `"$basedir/${shTarget}"`
}

// @SETLOCAL
Expand All @@ -120,34 +109,34 @@ function writeShim_ (from, to, prog, args, variables, cb) {
// EXIT /b
//
// Subroutine trick to fix https://github.com/npm/cmd-shim/issues/10
var head = '@ECHO off\r\n' +
const head = '@ECHO off\r\n' +
'SETLOCAL\r\n' +
'CALL :find_dp0\r\n'
var foot = 'ENDLOCAL\r\n' +
const foot = 'ENDLOCAL\r\n' +
'EXIT /b %errorlevel%\r\n' +
':find_dp0\r\n' +
'SET dp0=%~dp0\r\n' +
'EXIT /b\r\n'

var cmd
let cmd
if (longProg) {
shLongProg = shLongProg.trim();
args = args.trim();
var variableDeclarationsAsBatch = toBatchSyntax.convertToSetCommands(variables)
const variablesBatch = toBatchSyntax.convertToSetCommands(variables)
cmd = head
+ variableDeclarationsAsBatch
+ "\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"
+ ")\r\n"
+ "\r\n"
+ "\"%_prog%\" " + args + " " + target + " %*\r\n"
+ variablesBatch
+ '\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'
+ ')\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${foot}`
}

// #!/bin/sh
Expand All @@ -166,30 +155,30 @@ function writeShim_ (from, to, prog, args, variables, cb) {
// fi
// exit $ret

var sh = "#!/bin/sh\n"
let sh = "#!/bin/sh\n"

sh = sh
+ "basedir=$(dirname \"$(echo \"$0\" | sed -e 's,\\\\,/,g')\")\n"
+ "\n"
+ "case `uname` in\n"
+ " *CYGWIN*|*MINGW*|*MSYS*) basedir=`cygpath -w \"$basedir\"`;;\n"
+ "esac\n"
+ "\n"
+ `basedir=$(dirname "$(echo "$0" | sed -e 's,\\\\,/,g')")\n`
+ '\n'
+ 'case `uname` in\n'
+ ' *CYGWIN*|*MINGW*|*MSYS*) basedir=`cygpath -w "$basedir"`;;\n'
+ 'esac\n'
+ '\n'

if (shLongProg) {
sh = sh
+ "if [ -x "+shLongProg+" ]; then\n"
+ " " + variables + shLongProg + " " + args + " " + shTarget + " \"$@\"\n"
+ " ret=$?\n"
+ "else \n"
+ " " + variables + shProg + " " + args + " " + shTarget + " \"$@\"\n"
+ " ret=$?\n"
+ "fi\n"
+ "exit $ret\n"
+ `if [ -x ${shLongProg} ]; then\n`
+ ` ${variables}${shLongProg} ${args} ${shTarget} "$@"\n`
+ ' ret=$?\n'
+ 'else \n'
+ ` ${variables}${shProg} ${args} ${shTarget} "$@"\n`
+ ' ret=$?\n'
+ 'fi\n'
+ 'exit $ret\n'
} else {
sh = sh
+ shProg + " " + args + " " + shTarget + " \"$@\"\n"
+ "exit $?\n"
+ `${shProg} ${args} ${shTarget} "$@"\n`
+ 'exit $?\n'
}

// #!/usr/bin/env pwsh
Expand All @@ -210,51 +199,49 @@ function writeShim_ (from, to, prog, args, variables, cb) {
// $ret=$LASTEXITCODE
// }
// exit $ret
var pwsh = "#!/usr/bin/env pwsh\n"
+ "$basedir=Split-Path $MyInvocation.MyCommand.Definition -Parent\n"
+ "\n"
+ "$exe=\"\"\n"
+ "if ($PSVersionTable.PSVersion -lt \"6.0\" -or $IsWindows) {\n"
+ " # Fix case when both the Windows and Linux builds of Node\n"
+ " # are installed in the same directory\n"
+ " $exe=\".exe\"\n"
+ "}\n"
let pwsh = '#!/usr/bin/env pwsh\n'
+ '$basedir=Split-Path $MyInvocation.MyCommand.Definition -Parent\n'
+ '\n'
+ '$exe=""\n'
+ 'if ($PSVersionTable.PSVersion -lt \"6.0\" -or $IsWindows) {\n'
+ ' # Fix case when both the Windows and Linux builds of Node\n'
+ ' # are installed in the same directory\n'
+ ' $exe=".exe"\n'
+ '}\n'
if (shLongProg) {
pwsh = pwsh
+ "$ret=0\n"
+ "if (Test-Path " + pwshLongProg + ") {\n"
+ " & " + pwshLongProg + " " + args + " " + shTarget + " $args\n"
+ " $ret=$LASTEXITCODE\n"
+ "} else {\n"
+ " & " + pwshProg + " " + args + " " + shTarget + " $args\n"
+ " $ret=$LASTEXITCODE\n"
+ "}\n"
+ "exit $ret\n"
+ '$ret=0\n'
+ `if (Test-Path ${pwshLongProg}) {\n`
+ ` & ${pwshLongProg} ${args} ${shTarget} $args\n`
+ ' $ret=$LASTEXITCODE\n'
+ '} else {\n'
+ ` & ${pwshProg} ${args} ${shTarget} $args\n`
+ ' $ret=$LASTEXITCODE\n'
+ '}\n'
+ 'exit $ret\n'
} else {
pwsh = pwsh
+ "& " + pwshProg + " " + args + " " + shTarget + " $args\n"
+ "exit $LASTEXITCODE\n"
+ `& ${pwshProg} ${args} ${shTarget} $args\n`
+ 'exit $LASTEXITCODE\n'
}

var then = times(3, next, cb)
fs.writeFile(to + ".ps1", pwsh, "utf8", then)
fs.writeFile(to + ".cmd", cmd, "utf8", then)
fs.writeFile(to, sh, "utf8", then)
function next () {
chmodShim(to, cb)
}
const next = () => chmodShim(to, cb)
const then = times(3, next, cb)
fs.writeFile(to + '.ps1', pwsh, 'utf8', then)
fs.writeFile(to + '.cmd', cmd, 'utf8', then)
fs.writeFile(to, sh, 'utf8', then)
}

function chmodShim (to, cb) {
var then = times(3, cb, cb)
fs.chmod(to, "0755", then)
fs.chmod(to + ".cmd", "0755", then)
fs.chmod(to + ".ps1", "0755", then)
const chmodShim = (to, cb) => {
const then = times(3, cb, cb)
fs.chmod(to, 0o755, then)
fs.chmod(to + '.cmd', 0o755, then)
fs.chmod(to + '.ps1', 0o755, then)
}

function times(n, ok, cb) {
var errState = null
return function(er) {
const times = (n, ok, cb) => {
let errState = null
return er => {
if (!errState) {
if (er)
cb(errState = er)
Expand All @@ -263,3 +250,6 @@ function times(n, ok, cb) {
}
}
}

module.exports = cmdShim
cmdShim.ifExists = cmdShimIfExists
Loading

0 comments on commit c5f9b8e

Please sign in to comment.