node/deps/npm/node_modules/read-cmd-shim/index.js
claudiahdz 17e420b23f deps: update npm to 6.11.3
PR-URL: https://github.com/nodejs/node/pull/29430
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: David Carlier <devnexen@gmail.com>
Reviewed-By: Jiawen Geng <technicalcute@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
2019-09-17 18:51:21 -07:00

55 lines
1.5 KiB
JavaScript

'use strict'
var fs = require('graceful-fs')
function extractPath (path, cmdshimContents) {
if (/[.]cmd$/.test(path)) {
return extractPathFromCmd(cmdshimContents)
} else {
return extractPathFromCygwin(cmdshimContents)
}
}
function extractPathFromCmd (cmdshimContents) {
var matches = cmdshimContents.match(/"%(?:~dp0|dp0%)\\([^"]+?)"\s+%[*]/)
return matches && matches[1]
}
function extractPathFromCygwin (cmdshimContents) {
var matches = cmdshimContents.match(/"[$]basedir[/]([^"]+?)"\s+"[$]@"/)
return matches && matches[1]
}
function wrapError (thrown, newError) {
newError.message = thrown.message
newError.code = thrown.code
return newError
}
function notaShim (path, er) {
if (!er) {
er = new Error()
Error.captureStackTrace(er, notaShim)
}
er.code = 'ENOTASHIM'
er.message = "Can't read shim path from '" + path + "', it doesn't appear to be a cmd-shim"
return er
}
var readCmdShim = module.exports = function (path, cb) {
var er = new Error()
Error.captureStackTrace(er, readCmdShim)
fs.readFile(path, function (readFileEr, contents) {
if (readFileEr) return cb(wrapError(readFileEr, er))
var destination = extractPath(path, contents.toString())
if (destination) return cb(null, destination)
return cb(notaShim(path, er))
})
}
module.exports.sync = function (path) {
var contents = fs.readFileSync(path)
var destination = extractPath(path, contents.toString())
if (!destination) throw notaShim(path)
return destination
}