Skip to content

Commit c3f9f83

Browse files
realitykingsindresorhus
authored andcommitted
ES2015ify and require Node.js >=4 (#59)
* ES2015ify and support Node.js >=4 * Fix a failing unit test * Switch from lazy-req to [email protected] * Switch from tempfile to tempy * Bump dev-dependencies unblocked by requiring Node.js 4 * Add supported Node.js versions to TravisCI config.
1 parent 12a68d1 commit c3f9f83

4 files changed

Lines changed: 40 additions & 39 deletions

File tree

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
sudo: false
22
language: node_js
33
node_js:
4+
- 'stable'
5+
- '8'
46
- '6'
57
- '4'

index.js

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
'use strict';
2-
var fs = require('fs');
3-
var path = require('path');
4-
var lazyReq = require('lazy-req')(require);
5-
var binCheck = lazyReq('bin-check');
6-
var binVersionCheck = lazyReq('bin-version-check');
7-
var Download = lazyReq('download');
8-
var osFilterObj = lazyReq('os-filter-obj');
2+
const fs = require('fs');
3+
const path = require('path');
4+
const importLazy = require('import-lazy')(require);
5+
6+
const binCheck = importLazy('bin-check');
7+
const binVersionCheck = importLazy('bin-version-check');
8+
const Download = importLazy('download');
9+
const osFilterObj = importLazy('os-filter-obj');
910

1011
/**
1112
* Initialize a new `BinWrapper`
@@ -40,15 +41,15 @@ module.exports = BinWrapper;
4041
*/
4142

4243
BinWrapper.prototype.src = function (src, os, arch) {
43-
if (!arguments.length) {
44+
if (arguments.length === 0) {
4445
return this._src;
4546
}
4647

4748
this._src = this._src || [];
4849
this._src.push({
4950
url: src,
50-
os: os,
51-
arch: arch
51+
os,
52+
arch
5253
});
5354

5455
return this;
@@ -62,7 +63,7 @@ BinWrapper.prototype.src = function (src, os, arch) {
6263
*/
6364

6465
BinWrapper.prototype.dest = function (dest) {
65-
if (!arguments.length) {
66+
if (arguments.length === 0) {
6667
return this._dest;
6768
}
6869

@@ -78,7 +79,7 @@ BinWrapper.prototype.dest = function (dest) {
7879
*/
7980

8081
BinWrapper.prototype.use = function (bin) {
81-
if (!arguments.length) {
82+
if (arguments.length === 0) {
8283
return this._use;
8384
}
8485

@@ -94,7 +95,7 @@ BinWrapper.prototype.use = function (bin) {
9495
*/
9596

9697
BinWrapper.prototype.version = function (range) {
97-
if (!arguments.length) {
98+
if (arguments.length === 0) {
9899
return this._version;
99100
}
100101

@@ -126,7 +127,7 @@ BinWrapper.prototype.run = function (cmd, cb) {
126127
cmd = ['--version'];
127128
}
128129

129-
this.findExisting(function (err) {
130+
this.findExisting(err => {
130131
if (err) {
131132
cb(err);
132133
return;
@@ -138,7 +139,7 @@ BinWrapper.prototype.run = function (cmd, cb) {
138139
}
139140

140141
this.runCheck(cmd, cb);
141-
}.bind(this));
142+
});
142143
};
143144

144145
/**
@@ -150,7 +151,7 @@ BinWrapper.prototype.run = function (cmd, cb) {
150151
*/
151152

152153
BinWrapper.prototype.runCheck = function (cmd, cb) {
153-
binCheck()(this.path(), cmd, function (err, works) {
154+
binCheck()(this.path(), cmd, (err, works) => {
154155
if (err) {
155156
cb(err);
156157
return;
@@ -167,7 +168,7 @@ BinWrapper.prototype.runCheck = function (cmd, cb) {
167168
}
168169

169170
cb();
170-
}.bind(this));
171+
});
171172
};
172173

173174
/**
@@ -178,7 +179,7 @@ BinWrapper.prototype.runCheck = function (cmd, cb) {
178179
*/
179180

180181
BinWrapper.prototype.findExisting = function (cb) {
181-
fs.stat(this.path(), function (err) {
182+
fs.stat(this.path(), err => {
182183
if (err && err.code === 'ENOENT') {
183184
this.download(cb);
184185
return;
@@ -190,7 +191,7 @@ BinWrapper.prototype.findExisting = function (cb) {
190191
}
191192

192193
cb();
193-
}.bind(this));
194+
});
194195
};
195196

196197
/**
@@ -201,21 +202,19 @@ BinWrapper.prototype.findExisting = function (cb) {
201202
*/
202203

203204
BinWrapper.prototype.download = function (cb) {
204-
var files = osFilterObj()(this.src());
205-
var download = new Download()({
205+
const files = osFilterObj()(this.src());
206+
const download = new Download()({
206207
extract: true,
207208
mode: '755',
208209
strip: this.opts.strip
209210
});
210211

211-
if (!files.length) {
212+
if (files.length === 0) {
212213
cb(new Error('No binary found matching your system. It\'s probably not supported.'));
213214
return;
214215
}
215216

216-
files.forEach(function (file) {
217-
download.get(file.url);
218-
});
217+
files.forEach(file => download.get(file.url));
219218

220219
download
221220
.dest(this.dest())

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"url": "https://github.com/kevva"
1111
},
1212
"engines": {
13-
"node": ">=0.10.0"
13+
"node": ">=4"
1414
},
1515
"scripts": {
1616
"test": "xo && ava"
@@ -29,16 +29,16 @@
2929
"bin-version-check": "^2.1.0",
3030
"download": "^4.0.0",
3131
"each-async": "^1.1.1",
32-
"lazy-req": "^1.0.0",
32+
"import-lazy": "^2.1.0",
3333
"os-filter-obj": "^1.0.0"
3434
},
3535
"devDependencies": {
3636
"ava": "*",
37-
"nock": "^8.0.0",
37+
"nock": "^9.2.3",
3838
"path-exists": "^3.0.0",
39-
"pify": "^2.3.0",
39+
"pify": "^3.0.0",
4040
"rimraf": "^2.2.8",
41-
"tempfile": "^1.1.1",
41+
"tempy": "^0.2.1",
4242
"xo": "*"
4343
}
4444
}

test.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import pathExists from 'path-exists';
55
import pify from 'pify';
66
import rimraf from 'rimraf';
77
import test from 'ava';
8-
import tempfile from 'tempfile';
9-
import Fn from './';
8+
import tempy from 'tempy';
9+
import Fn from '.';
1010

1111
const fsP = pify(fs);
1212
const rimrafP = pify(rimraf);
@@ -64,7 +64,7 @@ test('get the binary path', t => {
6464
test('verify that a binary is working', async t => {
6565
const bin = new Fn()
6666
.src('http://foo.com/gifsicle.tar.gz')
67-
.dest(tempfile())
67+
.dest(tempy.directory())
6868
.use(process.platform === 'win32' ? 'gifsicle.exe' : 'gifsicle');
6969

7070
await pify(bin.run.bind(bin))();
@@ -75,7 +75,7 @@ test('verify that a binary is working', async t => {
7575
test('meet the desired version', async t => {
7676
const bin = new Fn()
7777
.src('http://foo.com/gifsicle.tar.gz')
78-
.dest(tempfile())
78+
.dest(tempy.directory())
7979
.use(process.platform === 'win32' ? 'gifsicle.exe' : 'gifsicle')
8080
.version('>=1.71');
8181

@@ -89,7 +89,7 @@ test('download files even if they are not used', async t => {
8989
.src('http://foo.com/gifsicle-darwin.tar.gz')
9090
.src('http://foo.com/gifsicle-win32.tar.gz')
9191
.src('http://foo.com/test.js')
92-
.dest(tempfile())
92+
.dest(tempy.directory())
9393
.use(process.platform === 'win32' ? 'gifsicle.exe' : 'gifsicle');
9494

9595
await pify(bin.run.bind(bin))();
@@ -106,18 +106,18 @@ test('download files even if they are not used', async t => {
106106
test('skip running binary check', async t => {
107107
const bin = new Fn({skipCheck: true})
108108
.src('http://foo.com/gifsicle.tar.gz')
109-
.dest(tempfile())
109+
.dest(tempy.directory())
110110
.use(process.platform === 'win32' ? 'gifsicle.exe' : 'gifsicle');
111111

112112
await pify(bin.run.bind(bin))(['--shouldNotFailAnyway']);
113113
t.true(await pathExists(bin.path()));
114114
await rimrafP(bin.dest());
115115
});
116116

117-
test('error if no binary is found and no source is provided', t => {
117+
test('error if no binary is found and no source is provided', async t => {
118118
const bin = new Fn()
119-
.dest(tempfile())
119+
.dest(tempy.directory())
120120
.use(process.platform === 'win32' ? 'gifsicle.exe' : 'gifsicle');
121121

122-
t.throws(pify(bin.run.bind(bin))(), 'No binary found matching your system. It\'s probably not supported.');
122+
await t.throws(pify(bin.run.bind(bin))(), 'No binary found matching your system. It\'s probably not supported.');
123123
});

0 commit comments

Comments
 (0)