Skip to content

Commit

Permalink
Require Node.js 12 and move to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Apr 28, 2021
1 parent 50b6c44 commit 6250754
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 73 deletions.
3 changes: 0 additions & 3 deletions .github/funding.yml

This file was deleted.

4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Expand Up @@ -10,12 +10,12 @@ jobs:
fail-fast: false
matrix:
node-version:
- 16
- 14
- 12
- 10
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- run: npm install
Expand Down
47 changes: 19 additions & 28 deletions index.d.ts
@@ -1,12 +1,10 @@
declare namespace binVersion {
interface Options {
/**
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.
*/
readonly args?: readonly string[];
}
export interface Options {
/**
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.
*/
readonly args?: readonly string[];
}

/**
Expand All @@ -17,29 +15,22 @@ Get the version of a binary in [semver](https://github.com/npm/node-semver) form
@example
```
import binVersion = require('bin-version');
import binaryVersion from 'bin-version';
(async () => {
// $ curl --version
// curl 7.30.0 (x86_64-apple-darwin13.0)
// $ curl --version
// curl 7.30.0 (x86_64-apple-darwin13.0)
console.log(await binVersion('curl'));
//=> '7.30.0'
console.log(await binaryVersion('curl'));
//=> '7.30.0'
// $ openssl version
// OpenSSL 1.0.2d 9 Jul 2015
// $ openssl version
// OpenSSL 1.0.2d 9 Jul 2015
console.log(await binVersion('openssl'));
//=> '1.0.2'
console.log(await binaryVersion('openssl'));
//=> '1.0.2'
console.log(await binVersion('openssl', {args: ['version']}));
//=> '1.0.2'
})();
console.log(await binaryVersion('openssl', {args: ['version']}));
//=> '1.0.2'
```
*/
declare function binVersion(
binary: string,
options?: binVersion.Options
): Promise<string>;

export = binVersion;
export default function binaryVersion(binary: string, options?: Options): Promise<string>;
9 changes: 4 additions & 5 deletions index.js
@@ -1,6 +1,5 @@
'use strict';
const execa = require('execa');
const findVersions = require('find-versions');
import execa from 'execa';
import findVersions from 'find-versions';

const oneMegabyte = 1000 * 1000;

Expand All @@ -18,7 +17,7 @@ const defaultPossibleArguments = [
['version']
];

module.exports = async (binary, options = {}) => {
export default async function binaryVersion(binary, options = {}) {
let possibleArguments;

if (options.args === undefined) {
Expand Down Expand Up @@ -54,4 +53,4 @@ module.exports = async (binary, options = {}) => {
}

throw new Error(`Couldn't find version of \`${binary}\``);
};
}
6 changes: 3 additions & 3 deletions index.test-d.ts
@@ -1,5 +1,5 @@
import {expectType} from 'tsd';
import binVersion = require('.');
import binaryVersion from './index.js';

expectType<Promise<string>>(binVersion('curl'));
expectType<Promise<string>>(binVersion('openssl', {args: ['version']}));
expectType<Promise<string>>(binaryVersion('curl'));
expectType<Promise<string>>(binaryVersion('openssl', {args: ['version']}));
11 changes: 6 additions & 5 deletions package.json
Expand Up @@ -10,8 +10,10 @@
"email": "sindresorhus@gmail.com",
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": "./index.js",
"engines": {
"node": ">=10"
"node": ">=12"
},
"scripts": {
"test": "xo && ava && tsd"
Expand All @@ -21,7 +23,6 @@
"index.d.ts"
],
"keywords": [
"bin",
"binary",
"executable",
"version",
Expand All @@ -31,11 +32,11 @@
],
"dependencies": {
"execa": "^5.0.0",
"find-versions": "^4.0.0"
"find-versions": "^5.0.0"
},
"devDependencies": {
"ava": "^2.4.0",
"ava": "^3.15.0",
"tsd": "^0.14.0",
"xo": "^0.37.1"
"xo": "^0.39.1"
}
}
26 changes: 10 additions & 16 deletions readme.md
Expand Up @@ -16,12 +16,10 @@ curl 7.30.0 (x86_64-apple-darwin13.0)
```

```js
const binVersion = require('bin-version');
import binaryVersion from 'bin-version';

(async () => {
console.log(await binVersion('curl'));
//=> '7.30.0'
})();
console.log(await binaryVersion('curl'));
//=> '7.30.0'
```

```
Expand All @@ -30,12 +28,10 @@ OpenSSL 1.0.2d 9 Jul 2015
```

```js
const binVersion = require('bin-version');
import binaryVersion from 'bin-version';

(async () => {
console.log(await binVersion('openssl'));
//=> '1.0.2'
})();
console.log(await binaryVersion('openssl'));
//=> '1.0.2'
```

```
Expand All @@ -44,17 +40,15 @@ OpenSSL 1.0.2d 9 Jul 2015
```

```js
const binVersion = require('bin-version');
import binaryVersion from 'bin-version';

(async () => {
console.log(await binVersion('openssl', {args: ['version']}));
//=> '1.0.2'
})();
console.log(await binaryVersion('openssl', {args: ['version']}));
//=> '1.0.2'
```

## API

### binVersion(binary, options?)
### binaryVersion(binary, options?)

Returns a `Promise<string>` with the version of the `binary`.

Expand Down
22 changes: 11 additions & 11 deletions test.js
@@ -1,44 +1,44 @@
import test from 'ava';
import binVersion from './index.js';
import binaryVersion from './index.js';

const versionRegex = /\d+\.\d+\.\d+/;

test('does-not-exist', async t => {
await t.throwsAsync(binVersion('does-not-exist'), /Couldn't find/);
await t.throwsAsync(binaryVersion('does-not-exist'), {message: /Couldn't find/});
});

test('non-executable', async t => {
await t.throwsAsync(binVersion('./fixture/non-executable.js'));
await t.throwsAsync(binaryVersion('./fixture/non-executable.js'));
});

test('non-versioned', async t => {
await t.throwsAsync(binVersion('./fixture/non-versioned.js'), /Couldn't find version/);
await t.throwsAsync(binaryVersion('./fixture/non-versioned.js'), {message: /Couldn't find version/});
});

test('anything accepting `--version`', async t => {
t.is(await binVersion('./fixture/versioned-type1.js'), '1.2.3');
t.is(await binaryVersion('./fixture/versioned-type1.js'), '1.2.3');
});

test('anything accepting `version`', async t => {
t.is(await binVersion('./fixture/versioned-type2.js'), '1.2.3');
t.is(await binaryVersion('./fixture/versioned-type2.js'), '1.2.3');
});

test('curl', async t => {
t.regex(await binVersion('curl'), versionRegex);
t.regex(await binaryVersion('curl'), versionRegex);
});

test('npm', async t => {
t.regex(await binVersion('npm'), versionRegex);
t.regex(await binaryVersion('npm'), versionRegex);
});

test('openssl', async t => {
t.regex(await binVersion('openssl'), versionRegex);
t.regex(await binaryVersion('openssl'), versionRegex);
});

test('custom args', async t => {
t.regex(await binVersion('./fixture/versioned-type1.js', {args: ['--version']}), versionRegex);
t.regex(await binaryVersion('./fixture/versioned-type1.js', {args: ['--version']}), versionRegex);
});

test('php', async t => {
t.is(await binVersion('./fixture/php.js'), '7.0.0');
t.is(await binaryVersion('./fixture/php.js'), '7.0.0');
});

0 comments on commit 6250754

Please sign in to comment.