├── .editorconfig ├── .gitattributes ├── .github ├── security.md └── workflows │ └── main.yml ├── .gitignore ├── .npmrc ├── index.js ├── 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 }} on ${{ matrix.os }} 8 | runs-on: ${{ matrix.os }} 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | node-version: 13 | - 20 14 | - 18 15 | os: 16 | - ubuntu-latest 17 | - macos-latest 18 | - windows-latest 19 | steps: 20 | - uses: actions/checkout@v4 21 | - uses: actions/setup-node@v4 22 | with: 23 | node-version: ${{ matrix.node-version }} 24 | - run: npm install 25 | - run: npm test 26 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | import semver from 'semver'; 2 | import binaryVersion from 'binary-version'; 3 | import semverTruncate from 'semver-truncate'; 4 | 5 | export default async function binaryVersionCheck(binary, semverRange, options) { 6 | if (typeof binary !== 'string' || typeof semverRange !== 'string') { 7 | throw new TypeError('`binary` and `semverRange` arguments required'); 8 | } 9 | 10 | if (!semver.validRange(semverRange)) { 11 | throw new Error('Invalid version range'); 12 | } 13 | 14 | const version = await binaryVersion(binary, options); 15 | 16 | if (semver.satisfies(semverTruncate(version, 'patch'), semverRange)) { 17 | return; 18 | } 19 | 20 | const error = new Error(`${binary} ${version} does not satisfy the version requirement of ${semverRange}`); 21 | error.name = 'InvalidBinaryVersion'; 22 | throw error; 23 | } 24 | -------------------------------------------------------------------------------- /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-check", 3 | "version": "6.1.0", 4 | "description": "Check whether a binary version satisfies a semver range", 5 | "license": "MIT", 6 | "repository": "sindresorhus/binary-version-check", 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": "./index.js", 15 | "sideEffects": false, 16 | "engines": { 17 | "node": ">=18" 18 | }, 19 | "scripts": { 20 | "test": "xo && ava" 21 | }, 22 | "files": [ 23 | "index.js" 24 | ], 25 | "keywords": [ 26 | "cli", 27 | "binary", 28 | "executable", 29 | "version", 30 | "semver", 31 | "semantic", 32 | "range", 33 | "satisfy", 34 | "check", 35 | "validate" 36 | ], 37 | "dependencies": { 38 | "binary-version": "^7.1.0", 39 | "semver": "^7.6.0", 40 | "semver-truncate": "^3.0.0" 41 | }, 42 | "devDependencies": { 43 | "ava": "^6.1.2", 44 | "xo": "^0.58.0" 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # binary-version-check 2 | 3 | > Check whether a binary version satisfies a [semver range](https://github.com/npm/node-semver#ranges) 4 | 5 | Useful when you have a thing that only works with specific versions of a binary. 6 | 7 | ## Install 8 | 9 | ```sh 10 | npm install binary-version-check 11 | ``` 12 | 13 | ## Usage 14 | 15 | ```console 16 | $ curl --version 17 | curl 7.30.0 (x86_64-apple-darwin13.0) 18 | ``` 19 | 20 | ```js 21 | import binaryVersionCheck from 'binary-version-check'; 22 | 23 | try { 24 | await binaryVersionCheck('curl', '>=8'); 25 | } catch (error) { 26 | console.log(error); 27 | //=> 'InvalidBinaryVersion: curl 7.30.0 doesn't satisfy the version requirement of >=8' 28 | } 29 | ``` 30 | 31 | ## API 32 | 33 | ### binaryVersionCheck(binary, semverRange, options?) 34 | 35 | #### binary 36 | 37 | Type: `string` 38 | 39 | The name or path of the binary to check. 40 | 41 | #### semverRange 42 | 43 | Type: `string` 44 | 45 | The [semver range](https://github.com/npm/node-semver#ranges) to check against. 46 | 47 | #### options 48 | 49 | Type: `object` 50 | 51 | ##### args 52 | 53 | Type: `string[]`\ 54 | Default: `['--version']` 55 | 56 | The CLI arguments used to get the binary version. 57 | 58 | ## Related 59 | 60 | - [binary-version-check-cli](https://github.com/sindresorhus/binary-version-check-cli) - CLI for this package 61 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | import binaryVersionCheck from './index.js'; 3 | 4 | test('error when the range does not satisfy the bin version', async t => { 5 | await t.throwsAsync(binaryVersionCheck('curl', '1.29.0'), { 6 | name: 'InvalidBinaryVersion', 7 | }); 8 | }); 9 | 10 | test('no error when the range satisfies the bin version', async t => { 11 | await t.notThrowsAsync(binaryVersionCheck('curl', '>=1')); 12 | }); 13 | --------------------------------------------------------------------------------