2020-10-02 17:52:19 -04:00
|
|
|
const fs = require('fs')
|
2022-04-14 21:57:02 +00:00
|
|
|
const { promisify } = require('util')
|
|
|
|
const { readFileSync } = fs
|
2020-10-02 17:52:19 -04:00
|
|
|
const readFile = promisify(fs.readFile)
|
2015-10-09 23:13:57 -07:00
|
|
|
|
2020-10-02 17:52:19 -04:00
|
|
|
const extractPath = (path, cmdshimContents) => {
|
2015-10-09 23:13:57 -07:00
|
|
|
if (/[.]cmd$/.test(path)) {
|
|
|
|
return extractPathFromCmd(cmdshimContents)
|
2019-11-05 14:55:08 -05:00
|
|
|
} else if (/[.]ps1$/.test(path)) {
|
|
|
|
return extractPathFromPowershell(cmdshimContents)
|
2015-10-09 23:13:57 -07:00
|
|
|
} else {
|
|
|
|
return extractPathFromCygwin(cmdshimContents)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-02 17:52:19 -04:00
|
|
|
const extractPathFromPowershell = cmdshimContents => {
|
|
|
|
const matches = cmdshimContents.match(/"[$]basedir[/]([^"]+?)"\s+[$]args/)
|
2019-11-05 14:55:08 -05:00
|
|
|
return matches && matches[1]
|
|
|
|
}
|
|
|
|
|
2020-10-02 17:52:19 -04:00
|
|
|
const extractPathFromCmd = cmdshimContents => {
|
|
|
|
const matches = cmdshimContents.match(/"%(?:~dp0|dp0%)\\([^"]+?)"\s+%[*]/)
|
2015-10-09 23:13:57 -07:00
|
|
|
return matches && matches[1]
|
|
|
|
}
|
|
|
|
|
2020-10-02 17:52:19 -04:00
|
|
|
const extractPathFromCygwin = cmdshimContents => {
|
|
|
|
const matches = cmdshimContents.match(/"[$]basedir[/]([^"]+?)"\s+"[$]@"/)
|
2015-10-09 23:13:57 -07:00
|
|
|
return matches && matches[1]
|
|
|
|
}
|
|
|
|
|
2020-10-02 17:52:19 -04:00
|
|
|
const wrapError = (thrown, newError) => {
|
2015-10-09 23:13:57 -07:00
|
|
|
newError.message = thrown.message
|
|
|
|
newError.code = thrown.code
|
2020-10-02 17:52:19 -04:00
|
|
|
newError.path = thrown.path
|
2015-10-09 23:13:57 -07:00
|
|
|
return newError
|
|
|
|
}
|
|
|
|
|
2020-10-02 17:52:19 -04:00
|
|
|
const notaShim = (path, er) => {
|
2015-10-09 23:13:57 -07:00
|
|
|
if (!er) {
|
|
|
|
er = new Error()
|
|
|
|
Error.captureStackTrace(er, notaShim)
|
|
|
|
}
|
|
|
|
er.code = 'ENOTASHIM'
|
2020-10-02 17:52:19 -04:00
|
|
|
er.message = `Can't read shim path from '${path}', ` +
|
|
|
|
`it doesn't appear to be a cmd-shim`
|
2015-10-09 23:13:57 -07:00
|
|
|
return er
|
|
|
|
}
|
|
|
|
|
2020-10-02 17:52:19 -04:00
|
|
|
const readCmdShim = path => {
|
|
|
|
// create a new error to capture the stack trace from this point,
|
|
|
|
// instead of getting some opaque stack into node's internals
|
|
|
|
const er = new Error()
|
2015-10-09 23:13:57 -07:00
|
|
|
Error.captureStackTrace(er, readCmdShim)
|
2020-10-02 17:52:19 -04:00
|
|
|
return readFile(path).then(contents => {
|
|
|
|
const destination = extractPath(path, contents.toString())
|
2022-04-14 21:57:02 +00:00
|
|
|
if (destination) {
|
|
|
|
return destination
|
|
|
|
}
|
2022-12-06 22:18:33 -05:00
|
|
|
throw notaShim(path, er)
|
|
|
|
}, readFileEr => {
|
|
|
|
throw wrapError(readFileEr, er)
|
|
|
|
})
|
2015-10-09 23:13:57 -07:00
|
|
|
}
|
|
|
|
|
2020-10-02 17:52:19 -04:00
|
|
|
const readCmdShimSync = path => {
|
|
|
|
const contents = readFileSync(path)
|
|
|
|
const destination = extractPath(path, contents.toString())
|
2022-04-14 21:57:02 +00:00
|
|
|
if (!destination) {
|
|
|
|
throw notaShim(path)
|
|
|
|
}
|
2015-10-09 23:13:57 -07:00
|
|
|
return destination
|
|
|
|
}
|
2020-10-02 17:52:19 -04:00
|
|
|
|
|
|
|
readCmdShim.sync = readCmdShimSync
|
|
|
|
module.exports = readCmdShim
|