-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
drop real-executable-path-callback dependency
- Loading branch information
Showing
4 changed files
with
80 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,58 @@ | ||
'use strict'; | ||
|
||
const realExecutablePathCallback = require('real-executable-path-callback'); | ||
const {promisify} = require('util'); | ||
const {realpath} = require('fs'); | ||
|
||
const inspectWithKind = require('inspect-with-kind'); | ||
const isPlainObj = require('is-plain-obj'); | ||
const which = require('which'); | ||
|
||
const ARG_ERROR = 'Expected 1 or 2 arguments (<string>[, <Object>])'; | ||
const TYPE_ERROR = 'Expected an executable name inside the PATH (<string>), for exmaple `ls`, `git` and `node`'; | ||
const OPTION_ERROR = 'Expected an option object to be passed to `node-which` https://www.npmjs.com/package/which (`null` by default)'; | ||
const promisifiedRealpath = promisify(realpath); | ||
const promisifiedWhich = promisify(which); | ||
|
||
module.exports = async function realExecutablePath(...args) { | ||
const argLen = args.length; | ||
|
||
if (argLen === 0) { | ||
const error = new RangeError(`${ARG_ERROR}, but got no arguments.`); | ||
error.code = 'ERR_MISSING_ARGS'; | ||
|
||
throw error; | ||
} | ||
|
||
if (argLen !== 1 && argLen !== 2) { | ||
throw new RangeError(`Expected 1 or 2 arguments (<string>[, <Object>]), but got ${ | ||
argLen === 0 ? 'no' : argLen | ||
} arguments.`); | ||
throw new RangeError(`${ARG_ERROR}, but got ${argLen} arguments.`); | ||
} | ||
|
||
const [cmd, options] = args; | ||
|
||
if (typeof cmd !== 'string') { | ||
const error = new TypeError(`${TYPE_ERROR}, but got a non-string value ${inspectWithKind(cmd)}.`); | ||
error.code = 'ERR_INVALID_ARG_TYPE'; | ||
|
||
throw error; | ||
} | ||
|
||
if (cmd.length === 0) { | ||
throw new Error(`${TYPE_ERROR.replace(' (<string>)', '')}, but got '' (empty string).`); | ||
} | ||
|
||
if (argLen === 1) { | ||
return promisifiedRealpath(await promisifiedWhich(cmd)); | ||
} | ||
|
||
if (!isPlainObj(options)) { | ||
throw new TypeError(`${OPTION_ERROR}, but got ${inspectWithKind(options)}.`); | ||
} | ||
|
||
return new Promise((resolve, reject) => { | ||
realExecutablePathCallback(...args, (err, filePath) => { | ||
if (err) { | ||
reject(err); | ||
return; | ||
} | ||
if (options.all) { | ||
throw new Error(`\`all\` option is not supported, but a value ${ | ||
inspectWithKind(options.all) | ||
} was provided.`); | ||
} | ||
|
||
resolve(filePath); | ||
}); | ||
}); | ||
return promisifiedRealpath(await promisifiedWhich(cmd, options)); | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters