Skip to content

Commit be8e5c2

Browse files
committed
Update all the stuff, switch to ESM, require Node.js 12
1 parent 44dca3d commit be8e5c2

5 files changed

Lines changed: 68 additions & 77 deletions

File tree

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
strategy:
1717
fail-fast: false
1818
matrix:
19-
node: [6, 8, 10, 12, 14, 16]
19+
node: [12, 14, 16]
2020
os: [ubuntu-latest, windows-latest]
2121

2222
steps:

index.js

Lines changed: 18 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,17 @@
1-
'use strict';
2-
const fs = require('fs');
3-
const path = require('path');
4-
const url = require('url');
5-
const pify = require('pify');
6-
const importLazy = require('import-lazy')(require);
7-
8-
const binCheck = importLazy('bin-check');
9-
const binVersionCheck = importLazy('bin-version-check');
10-
const download = importLazy('download');
11-
const osFilterObj = importLazy('os-filter-obj');
12-
13-
const statAsync = pify(fs.stat);
14-
const chmodAsync = pify(fs.chmod);
1+
import {promises as fs} from 'node:fs';
2+
import path from 'node:path';
3+
import binVersionCheck from 'bin-version-check';
4+
import binCheck from 'bin-check';
5+
import download from 'download';
6+
import osFilterObject from 'os-filter-obj';
157

168
/**
179
* Initialize a new `BinWrapper`
1810
*
1911
* @param {Object} options
2012
* @api public
2113
*/
22-
module.exports = class BinWrapper {
14+
export default class BinWrapper {
2315
constructor(options = {}) {
2416
this.options = options;
2517

@@ -47,7 +39,7 @@ module.exports = class BinWrapper {
4739
this._src.push({
4840
url: src,
4941
os,
50-
arch
42+
arch,
5143
});
5244

5345
return this;
@@ -149,7 +141,7 @@ module.exports = class BinWrapper {
149141
* @api private
150142
*/
151143
findExisting() {
152-
return statAsync(this.path()).catch(error => {
144+
return fs.stat(this.path()).catch(error => {
153145
if (error && error.code === 'ENOENT') {
154146
return this.download();
155147
}
@@ -164,45 +156,33 @@ module.exports = class BinWrapper {
164156
* @api private
165157
*/
166158
download() {
167-
const files = osFilterObj(this.src() || []);
159+
const files = osFilterObject(this.src() || []);
168160
const urls = [];
169161

170162
if (files.length === 0) {
171163
return Promise.reject(new Error('No binary found matching your system. It\'s probably not supported.'));
172164
}
173165

174-
files.forEach(file => urls.push(file.url));
166+
for (const file of files) {
167+
urls.push(file.url);
168+
}
175169

176170
return Promise.all(urls.map(url => download(url, this.dest(), {
177171
extract: true,
178-
strip: this.options.strip
172+
strip: this.options.strip,
179173
}))).then(result => {
180-
const resultingFiles = flatten(result.map((item, index) => {
174+
const resultingFiles = result.flatMap((item, index) => {
181175
if (Array.isArray(item)) {
182176
return item.map(file => file.path);
183177
}
184178

185-
const parsedUrl = url.parse(files[index].url);
179+
const parsedUrl = new URL(files[index].url);
186180
const parsedPath = path.parse(parsedUrl.pathname);
187181

188182
return parsedPath.base;
189-
}));
183+
});
190184

191-
return Promise.all(resultingFiles.map(fileName => {
192-
return chmodAsync(path.join(this.dest(), fileName), 0o755);
193-
}));
185+
return Promise.all(resultingFiles.map(fileName => fs.chmod(path.join(this.dest(), fileName), 0o755)));
194186
});
195187
}
196-
};
197-
198-
function flatten(arr) {
199-
return arr.reduce((acc, elem) => {
200-
if (Array.isArray(elem)) {
201-
acc.push(...elem);
202-
} else {
203-
acc.push(elem);
204-
}
205-
206-
return acc;
207-
}, []);
208188
}

package.json

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,17 @@
1010
"url": "https://github.com/kevva"
1111
},
1212
"engines": {
13-
"node": ">=6"
13+
"node": "^12.20.0 || ^14.14.0 || >=16.0.0"
1414
},
1515
"scripts": {
16-
"test": "xo && ava"
16+
"ava": "ava",
17+
"xo": "xo",
18+
"test": "npm run xo && npm run ava"
19+
},
20+
"main": "index.js",
21+
"type": "module",
22+
"exports": {
23+
".": "./index.js"
1724
},
1825
"files": [
1926
"index.js"
@@ -26,19 +33,17 @@
2633
],
2734
"dependencies": {
2835
"bin-check": "^4.1.0",
29-
"bin-version-check": "^4.0.0",
30-
"download": "^7.1.0",
31-
"import-lazy": "^3.1.0",
32-
"os-filter-obj": "^2.0.0",
33-
"pify": "^4.0.1"
36+
"bin-version-check": "^5.0.0",
37+
"download": "^8.0.0",
38+
"os-filter-obj": "^2.0.0"
3439
},
3540
"devDependencies": {
36-
"ava": "*",
41+
"ava": "^4.0.1",
3742
"executable": "^4.1.1",
38-
"nock": "^10.0.2",
39-
"path-exists": "^3.0.0",
40-
"rimraf": "^2.6.2",
41-
"tempy": "^0.2.1",
42-
"xo": "*"
43+
"nock": "^13.2.2",
44+
"path-exists": "^5.0.0",
45+
"rimraf": "^3.0.2",
46+
"tempy": "^2.0.0",
47+
"xo": "^0.47.0"
4348
}
4449
}

readme.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ npm install bin-wrapper
1313
## Usage
1414

1515
```js
16-
const BinWrapper = require('bin-wrapper');
16+
import path from 'node:path';
17+
import BinWrapper from 'bin-wrapper';
1718

1819
const base = 'https://github.com/imagemin/gifsicle-bin/raw/main/vendor';
1920
const bin = new BinWrapper()

test.js

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
1-
import fs from 'fs';
2-
import path from 'path';
1+
import fs from 'node:fs';
2+
import path from 'node:path';
3+
import process from 'node:process';
4+
import {promisify} from 'node:util';
5+
import {fileURLToPath} from 'node:url';
36
import nock from 'nock';
4-
import pathExists from 'path-exists';
5-
import pify from 'pify';
7+
import {pathExists} from 'path-exists';
8+
import executable from 'executable';
69
import rimraf from 'rimraf';
7-
import test from 'ava';
810
import tempy from 'tempy';
9-
import executable from 'executable';
10-
import Fn from '.';
11+
import test from 'ava';
12+
import BinWrapper from './index.js';
13+
14+
const __filename = fileURLToPath(import.meta.url);
15+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
1116

12-
const rimrafP = pify(rimraf);
17+
const rimrafP = promisify(rimraf);
1318
const fixture = path.join.bind(path, __dirname, 'fixtures');
1419

1520
test.beforeEach(() => {
@@ -25,44 +30,44 @@ test.beforeEach(() => {
2530
});
2631

2732
test('expose a constructor', t => {
28-
t.is(typeof Fn, 'function');
33+
t.is(typeof BinWrapper, 'function');
2934
});
3035

3136
test('add a source', t => {
32-
const bin = new Fn().src('http://foo.com/bar.tar.gz');
37+
const bin = new BinWrapper().src('http://foo.com/bar.tar.gz');
3338
t.is(bin._src[0].url, 'http://foo.com/bar.tar.gz');
3439
});
3540

3641
test('add a source to a specific os', t => {
37-
const bin = new Fn().src('http://foo.com', process.platform);
42+
const bin = new BinWrapper().src('http://foo.com', process.platform);
3843
t.is(bin._src[0].os, process.platform);
3944
});
4045

4146
test('set destination directory', t => {
42-
const bin = new Fn().dest(path.join(__dirname, 'foo'));
47+
const bin = new BinWrapper().dest(path.join(__dirname, 'foo'));
4348
t.is(bin._dest, path.join(__dirname, 'foo'));
4449
});
4550

4651
test('set which file to use as the binary', t => {
47-
const bin = new Fn().use('foo');
52+
const bin = new BinWrapper().use('foo');
4853
t.is(bin._use, 'foo');
4954
});
5055

5156
test('set a version range to test against', t => {
52-
const bin = new Fn().version('1.0.0');
57+
const bin = new BinWrapper().version('1.0.0');
5358
t.is(bin._version, '1.0.0');
5459
});
5560

5661
test('get the binary path', t => {
57-
const bin = new Fn()
62+
const bin = new BinWrapper()
5863
.dest('tmp')
5964
.use('foo');
6065

6166
t.is(bin.path(), path.join('tmp', 'foo'));
6267
});
6368

6469
test('verify that a binary is working', async t => {
65-
const bin = new Fn()
70+
const bin = new BinWrapper()
6671
.src('http://foo.com/gifsicle.tar.gz')
6772
.dest(tempy.directory())
6873
.use(process.platform === 'win32' ? 'gifsicle.exe' : 'gifsicle');
@@ -73,7 +78,7 @@ test('verify that a binary is working', async t => {
7378
});
7479

7580
test('meet the desired version', async t => {
76-
const bin = new Fn()
81+
const bin = new BinWrapper()
7782
.src('http://foo.com/gifsicle.tar.gz')
7883
.dest(tempy.directory())
7984
.use(process.platform === 'win32' ? 'gifsicle.exe' : 'gifsicle')
@@ -85,7 +90,7 @@ test('meet the desired version', async t => {
8590
});
8691

8792
test('download files even if they are not used', async t => {
88-
const bin = new Fn({strip: 0, skipCheck: true})
93+
const bin = new BinWrapper({strip: 0, skipCheck: true})
8994
.src('http://foo.com/gifsicle-darwin.tar.gz')
9095
.src('http://foo.com/gifsicle-win32.tar.gz')
9196
.src('http://foo.com/test.js')
@@ -104,7 +109,7 @@ test('download files even if they are not used', async t => {
104109
});
105110

106111
test('skip running binary check', async t => {
107-
const bin = new Fn({skipCheck: true})
112+
const bin = new BinWrapper({skipCheck: true})
108113
.src('http://foo.com/gifsicle.tar.gz')
109114
.dest(tempy.directory())
110115
.use(process.platform === 'win32' ? 'gifsicle.exe' : 'gifsicle');
@@ -115,15 +120,15 @@ test('skip running binary check', async t => {
115120
});
116121

117122
test('error if no binary is found and no source is provided', async t => {
118-
const bin = new Fn()
123+
const bin = new BinWrapper()
119124
.dest(tempy.directory())
120125
.use(process.platform === 'win32' ? 'gifsicle.exe' : 'gifsicle');
121126

122-
await t.throws(bin.run(), 'No binary found matching your system. It\'s probably not supported.');
127+
await t.throwsAsync(bin.run(), undefined, 'No binary found matching your system. It\'s probably not supported.');
123128
});
124129

125130
test('downloaded files are set to be executable', async t => {
126-
const bin = new Fn({strip: 0, skipCheck: true})
131+
const bin = new BinWrapper({strip: 0, skipCheck: true})
127132
.src('http://foo.com/gifsicle-darwin.tar.gz')
128133
.src('http://foo.com/gifsicle-win32.tar.gz')
129134
.src('http://foo.com/test.js')
@@ -134,7 +139,7 @@ test('downloaded files are set to be executable', async t => {
134139

135140
const files = fs.readdirSync(bin.dest());
136141

137-
files.forEach(fileName => {
142+
for (const fileName of files) {
138143
t.true(executable.sync(path.join(bin.dest(), fileName)));
139-
});
144+
}
140145
});

0 commit comments

Comments
 (0)