bin-version
Advanced tools
Comparing version 3.1.0 to 4.0.0
@@ -6,5 +6,5 @@ declare namespace binVersion { | ||
@default ['--version'] | ||
If not specified, predefined arguments will be used for known binaries, or `['--version']` and `['version']` arguments will be tried. | ||
*/ | ||
args?: string[]; | ||
readonly args?: readonly string[]; | ||
} | ||
@@ -30,6 +30,8 @@ } | ||
// $ openssl version | ||
// OpenSSL 1.0.2d 9 Jul 2015 | ||
console.log(await binVersion('openssl')); | ||
//=> '1.0.2' | ||
console.log(await binVersion('openssl', {args: ['version']})); | ||
@@ -36,0 +38,0 @@ //=> '1.0.2' |
50
index.js
@@ -5,12 +5,48 @@ 'use strict'; | ||
module.exports = (binary, options = {}) => { | ||
return execa(binary, options.args || ['--version']) | ||
.then(result => findVersions(result.stdout || result.stderr, {loose: true})[0]) | ||
.catch(error => { | ||
const knownBinaryArguments = new Map([ | ||
...['ffmpeg', 'ffprobe', 'ffplay'].map(name => [name, ['-version']]), | ||
['openssl', ['version']] | ||
]); | ||
const defaultPossibleArguments = [ | ||
['--version'], | ||
['version'] | ||
]; | ||
module.exports = async (binary, options = {}) => { | ||
let possibleArguments; | ||
if (options.args === undefined) { | ||
const customArgs = knownBinaryArguments.get(binary); | ||
if (customArgs === undefined) { | ||
possibleArguments = defaultPossibleArguments; | ||
} else { | ||
possibleArguments = [customArgs]; | ||
} | ||
} else { | ||
possibleArguments = [options.args]; | ||
} | ||
for (const args of possibleArguments) { | ||
try { | ||
// TODO: Use `{all}` instead of `{stdout, stderr}` when execa v2 is out | ||
const {stdout, stderr} = await execa(binary, args); // eslint-disable-line no-await-in-loop | ||
const [version] = findVersions(stdout || stderr, {loose: true}); | ||
if (version !== undefined) { | ||
return version; | ||
} | ||
} catch (error) { | ||
if (error.code === 'ENOENT') { | ||
error.message = `Couldn't find the \`${binary}\` binary. Make sure it's installed and in your $PATH.`; | ||
const newError = new Error(`Couldn't find the \`${binary}\` binary. Make sure it's installed and in your $PATH.`); | ||
newError.sourceError = error; | ||
throw newError; | ||
} | ||
throw error; | ||
}); | ||
if (error.code === 'EACCES') { | ||
throw error; | ||
} | ||
} | ||
} | ||
throw new Error(`Couldn't find version of \`${binary}\``); | ||
}; |
{ | ||
"name": "bin-version", | ||
"version": "3.1.0", | ||
"version": "4.0.0", | ||
"description": "Get the version of a binary in semver format", | ||
@@ -13,3 +13,3 @@ "license": "MIT", | ||
"engines": { | ||
"node": ">=6" | ||
"node": ">=8" | ||
}, | ||
@@ -37,6 +37,6 @@ "scripts": { | ||
"devDependencies": { | ||
"ava": "^1.4.1", | ||
"tsd": "^0.7.2", | ||
"ava": "^2.1.0", | ||
"tsd": "^0.7.3", | ||
"xo": "^0.24.0" | ||
} | ||
} |
@@ -35,3 +35,19 @@ # bin-version [![Build Status](https://travis-ci.com/sindresorhus/bin-version.svg?branch=master)](https://travis-ci.com/sindresorhus/bin-version) | ||
```js | ||
const binVersion = require('bin-version'); | ||
(async () => { | ||
console.log(await binVersion('openssl')); | ||
//=> '1.0.2' | ||
})(); | ||
``` | ||
``` | ||
$ openssl version | ||
OpenSSL 1.0.2d 9 Jul 2015 | ||
``` | ||
```js | ||
const binVersion = require('bin-version'); | ||
(async () => { | ||
console.log(await binVersion('openssl', {args: ['version']})); | ||
@@ -42,5 +58,6 @@ //=> '1.0.2' | ||
## API | ||
### binVersion(binary, [options]) | ||
### binVersion(binary, options?) | ||
@@ -62,6 +79,8 @@ Returns a `Promise<string>` with the version of the `binary`. | ||
Type: `string[]` | ||
Default: `['--version']` | ||
The arguments to pass to `binary` so that it will print its version. | ||
If not specified, predefined arguments will be used for known binaries, or `['--version']` and `['version']` arguments will be tried. | ||
## Related | ||
@@ -71,6 +90,1 @@ | ||
- [find-versions](https://github.com/sindresorhus/find-versions) - Find semver versions in a string | ||
## License | ||
MIT © [Sindre Sorhus](https://sindresorhus.com) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
5709
79
87