├── .editorconfig ├── .gitattributes ├── .github ├── security.md └── workflows │ └── main.yml ├── .gitignore ├── .npmrc ├── fixture ├── non-executable.js ├── non-versioned.js ├── php.js ├── versioned-type1.js └── versioned-type2.js ├── index.d.ts ├── index.js ├── index.test-d.ts ├── license ├── package.json ├── readme.md └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.github/security.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. 4 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | - push 4 | - pull_request 5 | jobs: 6 | test: 7 | name: Node.js ${{ matrix.node-version }} 8 | runs-on: ubuntu-latest 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | node-version: 13 | - 20 14 | - 18 15 | steps: 16 | - uses: actions/checkout@v4 17 | - uses: actions/setup-node@v4 18 | with: 19 | node-version: ${{ matrix.node-version }} 20 | - run: npm install 21 | - run: npm test 22 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /fixture/non-executable.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 'use strict'; 3 | 4 | if (process.argv[2] === '--version') { 5 | console.log('1.2.3'); 6 | } 7 | -------------------------------------------------------------------------------- /fixture/non-versioned.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 'use strict'; 3 | 4 | if (process.argv[2] === '--version') { 5 | process.exit(1); 6 | } 7 | -------------------------------------------------------------------------------- /fixture/php.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 'use strict'; 3 | 4 | console.log('PHP 7.0.0RC6 (cli) (built: Oct 29 2015 13:46:05) ( NTS )\nCopyright (c) 1997-2015 The PHP Group\nZend Engine v3.0.0-dev, Copyright (c) 1998-2015 Zend Technologies'); 5 | -------------------------------------------------------------------------------- /fixture/versioned-type1.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 'use strict'; 3 | 4 | if (process.argv[2] === '--version') { 5 | console.log('1.2.3'); 6 | } 7 | -------------------------------------------------------------------------------- /fixture/versioned-type2.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 'use strict'; 3 | 4 | if (process.argv[2] === 'version') { 5 | console.log('1.2.3'); 6 | } 7 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | export type Options = { 2 | /** 3 | The arguments to pass to `binary` so that it will print its version. 4 | 5 | If not specified, predefined arguments will be used for known binaries, or `['--version']` and `['version']` arguments will be tried. 6 | */ 7 | readonly args?: readonly string[]; 8 | }; 9 | 10 | /** 11 | Get the version of a binary in [semver](https://github.com/npm/node-semver) format. 12 | 13 | @param binary - The name of or path to the binary to get the version from. 14 | @returns The version of the `binary`. 15 | 16 | @example 17 | ``` 18 | import binaryVersion from 'binary-version'; 19 | 20 | // $ curl --version 21 | // curl 7.30.0 (x86_64-apple-darwin13.0) 22 | 23 | console.log(await binaryVersion('curl')); 24 | //=> '7.30.0' 25 | 26 | // $ openssl version 27 | // OpenSSL 1.0.2d 9 Jul 2015 28 | 29 | console.log(await binaryVersion('openssl')); 30 | //=> '1.0.2' 31 | 32 | console.log(await binaryVersion('openssl', {args: ['version']})); 33 | //=> '1.0.2' 34 | ``` 35 | */ 36 | export default function binaryVersion(binary: string, options?: Options): Promise; 37 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | import {execa} from 'execa'; 2 | import findVersions from 'find-versions'; 3 | 4 | const oneMegabyte = 1000 * 1000; 5 | 6 | const knownBinaryArguments = new Map([ 7 | ...[ 8 | 'ffmpeg', 9 | 'ffprobe', 10 | 'ffplay', 11 | ].map(name => [name, ['-version']]), 12 | ['openssl', ['version']], 13 | ]); 14 | 15 | const defaultPossibleArguments = [ 16 | ['--version'], 17 | ['version'], 18 | ]; 19 | 20 | export default async function binaryVersion(binary, options = {}) { 21 | let possibleArguments; 22 | 23 | if (options.args === undefined) { 24 | const customArguments = knownBinaryArguments.get(binary); 25 | possibleArguments = customArguments === undefined ? defaultPossibleArguments : [customArguments]; 26 | } else { 27 | possibleArguments = [options.args]; 28 | } 29 | 30 | for (const arguments_ of possibleArguments) { 31 | try { 32 | // eslint-disable-next-line no-await-in-loop 33 | const {all} = await execa(binary, arguments_, { 34 | all: true, 35 | maxBuffer: oneMegabyte, 36 | }); 37 | 38 | const [version] = findVersions(all, {loose: true}); 39 | if (version !== undefined) { 40 | return version; 41 | } 42 | } catch (error) { 43 | if (error.code === 'ENOENT') { 44 | const newError = new Error(`Couldn't find the \`${binary}\` binary. Make sure it's installed and in your $PATH.`); 45 | newError.sourceError = error; 46 | throw newError; 47 | } 48 | 49 | if (error.code === 'EACCES') { 50 | throw error; 51 | } 52 | } 53 | } 54 | 55 | throw new Error(`Couldn't find version of \`${binary}\``); 56 | } 57 | -------------------------------------------------------------------------------- /index.test-d.ts: -------------------------------------------------------------------------------- 1 | import {expectType} from 'tsd'; 2 | import binaryVersion from './index.js'; 3 | 4 | expectType>(binaryVersion('curl')); 5 | expectType>(binaryVersion('openssl', {args: ['version']})); 6 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Sindre Sorhus (https://sindresorhus.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "binary-version", 3 | "version": "7.1.0", 4 | "description": "Get the version of a binary in semver format", 5 | "license": "MIT", 6 | "repository": "sindresorhus/binary-version", 7 | "funding": "https://github.com/sponsors/sindresorhus", 8 | "author": { 9 | "name": "Sindre Sorhus", 10 | "email": "sindresorhus@gmail.com", 11 | "url": "https://sindresorhus.com" 12 | }, 13 | "type": "module", 14 | "exports": { 15 | "types": "./index.d.ts", 16 | "default": "./index.js" 17 | }, 18 | "sideEffects": false, 19 | "engines": { 20 | "node": ">=18" 21 | }, 22 | "scripts": { 23 | "test": "xo && ava && tsd" 24 | }, 25 | "files": [ 26 | "index.js", 27 | "index.d.ts" 28 | ], 29 | "keywords": [ 30 | "binary", 31 | "executable", 32 | "version", 33 | "semver", 34 | "semantic", 35 | "cli" 36 | ], 37 | "dependencies": { 38 | "execa": "^8.0.1", 39 | "find-versions": "^6.0.0" 40 | }, 41 | "devDependencies": { 42 | "ava": "^6.1.2", 43 | "tsd": "^0.31.0", 44 | "xo": "^0.58.0" 45 | }, 46 | "xo": { 47 | "ignores": [ 48 | "fixture" 49 | ] 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # binary-version 2 | 3 | > Get the version of a binary in [semver](https://github.com/npm/node-semver) format 4 | 5 | ## Install 6 | 7 | ```sh 8 | npm install binary-version 9 | ``` 10 | 11 | ## Usage 12 | 13 | ```console 14 | $ curl --version 15 | curl 7.30.0 (x86_64-apple-darwin13.0) 16 | ``` 17 | 18 | ```js 19 | import binaryVersion from 'binary-version'; 20 | 21 | console.log(await binaryVersion('curl')); 22 | //=> '7.30.0' 23 | ``` 24 | 25 | ```console 26 | $ openssl version 27 | OpenSSL 1.0.2d 9 Jul 2015 28 | ``` 29 | 30 | ```js 31 | import binaryVersion from 'binary-version'; 32 | 33 | console.log(await binaryVersion('openssl')); 34 | //=> '1.0.2' 35 | ``` 36 | 37 | ```console 38 | $ openssl version 39 | OpenSSL 1.0.2d 9 Jul 2015 40 | ``` 41 | 42 | ```js 43 | import binaryVersion from 'binary-version'; 44 | 45 | console.log(await binaryVersion('openssl', {args: ['version']})); 46 | //=> '1.0.2' 47 | ``` 48 | 49 | ## API 50 | 51 | ### binaryVersion(binary, options?) 52 | 53 | Returns a `Promise` with the version of the `binary`. 54 | 55 | #### binary 56 | 57 | Type: `string` 58 | 59 | The name of or path to the binary to get the version from. 60 | 61 | #### options 62 | 63 | Type: `object` 64 | 65 | ##### args 66 | 67 | Type: `string[]` 68 | 69 | The arguments to pass to `binary` so that it will print its version. 70 | 71 | If not specified, predefined arguments will be used for known binaries, or `['--version']` and `['version']` arguments will be tried. 72 | 73 | ## Related 74 | 75 | - [binary-version-cli](https://github.com/sindresorhus/binary-version-cli) - CLI for this module 76 | - [find-versions](https://github.com/sindresorhus/find-versions) - Find semver versions in a string 77 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | import binaryVersion from './index.js'; 3 | 4 | const versionRegex = /\d+\.\d+\.\d+/; 5 | 6 | test('does-not-exist', async t => { 7 | await t.throwsAsync(binaryVersion('does-not-exist'), {message: /Couldn't find/}); 8 | }); 9 | 10 | test('non-executable', async t => { 11 | await t.throwsAsync(binaryVersion('./fixture/non-executable.js')); 12 | }); 13 | 14 | test('non-versioned', async t => { 15 | await t.throwsAsync(binaryVersion('./fixture/non-versioned.js'), {message: /Couldn't find version/}); 16 | }); 17 | 18 | test('anything accepting `--version`', async t => { 19 | t.is(await binaryVersion('./fixture/versioned-type1.js'), '1.2.3'); 20 | }); 21 | 22 | test('anything accepting `version`', async t => { 23 | t.is(await binaryVersion('./fixture/versioned-type2.js'), '1.2.3'); 24 | }); 25 | 26 | test('curl', async t => { 27 | t.regex(await binaryVersion('curl'), versionRegex); 28 | }); 29 | 30 | test('npm', async t => { 31 | t.regex(await binaryVersion('npm'), versionRegex); 32 | }); 33 | 34 | test('openssl', async t => { 35 | t.regex(await binaryVersion('openssl'), versionRegex); 36 | }); 37 | 38 | test('custom args', async t => { 39 | t.regex(await binaryVersion('./fixture/versioned-type1.js', {args: ['--version']}), versionRegex); 40 | }); 41 | 42 | test('php', async t => { 43 | t.is(await binaryVersion('./fixture/php.js'), '7.0.0'); 44 | }); 45 | --------------------------------------------------------------------------------