Skip to content

Commit 2cc9785

Browse files
realitykingsindresorhus
authored andcommitted
Change the public API to use Promises (#66)
1 parent 7fee790 commit 2cc9785

4 files changed

Lines changed: 39 additions & 66 deletions

File tree

index.js

Lines changed: 27 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
'use strict';
22
const fs = require('fs');
33
const path = require('path');
4+
const pify = require('pify');
45
const importLazy = require('import-lazy')(require);
56

67
const binCheck = importLazy('bin-check');
78
const binVersionCheck = importLazy('bin-version-check');
89
const download = importLazy('download');
910
const osFilterObj = importLazy('os-filter-obj');
1011

12+
const statAsync = pify(fs.stat);
13+
1114
/**
1215
* Initialize a new `BinWrapper`
1316
*
@@ -106,39 +109,27 @@ module.exports = class BinWrapper {
106109
* Run
107110
*
108111
* @param {Array} cmd
109-
* @param {Function} cb
110112
* @api public
111113
*/
112-
run(cmd, cb) {
113-
if (typeof cmd === 'function' && !cb) {
114-
cb = cmd;
115-
cmd = ['--version'];
116-
}
117-
118-
this.findExisting(err => {
119-
if (err) {
120-
cb(err);
121-
return;
122-
}
123-
124-
if (this.options.skipCheck) {
125-
cb();
126-
return;
127-
}
114+
run(cmd = ['--version']) {
115+
return this.findExisting()
116+
.then(() => {
117+
if (this.options.skipCheck) {
118+
return;
119+
}
128120

129-
this.runCheck(cmd, cb);
130-
});
121+
return this.runCheck(cmd);
122+
});
131123
}
132124

133125
/**
134126
* Run binary check
135127
*
136128
* @param {Array} cmd
137-
* @param {Function} cb
138129
* @api private
139130
*/
140-
runCheck(cmd, cb) {
141-
binCheck(this.path(), cmd)
131+
runCheck(cmd) {
132+
return binCheck(this.path(), cmd)
142133
.then(works => {
143134
if (!works) {
144135
throw new Error(`The \`${this.path()}\` binary doesn't seem to work correctly`);
@@ -149,56 +140,44 @@ module.exports = class BinWrapper {
149140
}
150141

151142
return Promise.resolve();
152-
})
153-
.then(() => cb())
154-
.catch(err => cb(err));
143+
});
155144
}
156145

157146
/**
158147
* Find existing files
159148
*
160-
* @param {Function} cb
161149
* @api private
162150
*/
163-
findExisting(cb) {
164-
fs.stat(this.path(), err => {
165-
if (err && err.code === 'ENOENT') {
166-
this.download(cb);
167-
return;
168-
}
169-
170-
if (err) {
171-
cb(err);
172-
return;
173-
}
174-
175-
cb();
176-
});
151+
findExisting() {
152+
return statAsync(this.path())
153+
.catch(err => {
154+
if (err && err.code === 'ENOENT') {
155+
return this.download();
156+
}
157+
158+
return Promise.reject(err);
159+
});
177160
}
178161

179162
/**
180163
* Download files
181164
*
182-
* @param {Function} cb
183165
* @api private
184166
*/
185-
download(cb) {
167+
download() {
186168
const files = osFilterObj(this.src() || []);
187169
const urls = [];
188170

189171
if (files.length === 0) {
190-
cb(new Error('No binary found matching your system. It\'s probably not supported.'));
191-
return;
172+
return Promise.reject(new Error('No binary found matching your system. It\'s probably not supported.'));
192173
}
193174

194175
files.forEach(file => urls.push(file.url));
195176

196-
Promise.all(urls.map(url => download(url, this.dest(), {
177+
return Promise.all(urls.map(url => download(url, this.dest(), {
197178
extract: true,
198179
mode: '755',
199180
strip: this.options.strip
200-
}))).then(() => {
201-
cb();
202-
});
181+
})));
203182
}
204183
};

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@
2929
"bin-version-check": "^3.0.0",
3030
"download": "^7.1.0",
3131
"import-lazy": "^3.1.0",
32-
"os-filter-obj": "^2.0.0"
32+
"os-filter-obj": "^2.0.0",
33+
"pify": "^3.0.0"
3334
},
3435
"devDependencies": {
3536
"ava": "*",
3637
"nock": "^9.4.2",
3738
"path-exists": "^3.0.0",
38-
"pify": "^3.0.0",
3939
"rimraf": "^2.6.2",
4040
"tempy": "^0.2.1",
4141
"xo": "*"

readme.md

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@ const bin = new BinWrapper()
2424
.use(process.platform === 'win32' ? 'gifsicle.exe' : 'gifsicle')
2525
.version('>=1.71');
2626

27-
bin.run(['--version'], err => {
28-
console.log('gifsicle is working');
29-
});
27+
bin.run(['--version'])
28+
.then(()) => {
29+
console.log('gifsicle is working');
30+
});
3031
```
3132

3233
Get the path to your binary with `bin.path()`:
@@ -110,7 +111,7 @@ Type: `string`
110111
Define a [semver range](https://github.com/isaacs/node-semver#ranges) to check
111112
the binary against.
112113

113-
### .run([arguments], callback)
114+
### .run([arguments])
114115

115116
Runs the search for the binary. If no binary is found it will download the file
116117
using the URL provided in `.src()`.
@@ -123,13 +124,6 @@ Default: `['--version']`
123124
Command to run the binary with. If it exits with code `0` it means that the
124125
binary is working.
125126

126-
#### callback(err)
127-
128-
Type: `Function`
129-
130-
Returns nothing but a possible error.
131-
132-
133127
## License
134128

135129
MIT © [Kevin Mårtensson](http://kevinmartensson.com)

test.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ test('verify that a binary is working', async t => {
6767
.dest(tempy.directory())
6868
.use(process.platform === 'win32' ? 'gifsicle.exe' : 'gifsicle');
6969

70-
await pify(bin.run.bind(bin))();
70+
await bin.run();
7171
t.true(await pathExists(bin.path()));
7272
await rimrafP(bin.dest());
7373
});
@@ -79,7 +79,7 @@ test('meet the desired version', async t => {
7979
.use(process.platform === 'win32' ? 'gifsicle.exe' : 'gifsicle')
8080
.version('>=1.71');
8181

82-
await pify(bin.run.bind(bin))();
82+
await bin.run();
8383
t.true(await pathExists(bin.path()));
8484
await rimrafP(bin.dest());
8585
});
@@ -92,7 +92,7 @@ test('download files even if they are not used', async t => {
9292
.dest(tempy.directory())
9393
.use(process.platform === 'win32' ? 'gifsicle.exe' : 'gifsicle');
9494

95-
await pify(bin.run.bind(bin))();
95+
await bin.run();
9696
const files = await fsP.readdirSync(bin.dest());
9797

9898
t.is(files.length, 3);
@@ -109,7 +109,7 @@ test('skip running binary check', async t => {
109109
.dest(tempy.directory())
110110
.use(process.platform === 'win32' ? 'gifsicle.exe' : 'gifsicle');
111111

112-
await pify(bin.run.bind(bin))(['--shouldNotFailAnyway']);
112+
await bin.run(['--shouldNotFailAnyway']);
113113
t.true(await pathExists(bin.path()));
114114
await rimrafP(bin.dest());
115115
});
@@ -119,5 +119,5 @@ test('error if no binary is found and no source is provided', async t => {
119119
.dest(tempy.directory())
120120
.use(process.platform === 'win32' ? 'gifsicle.exe' : 'gifsicle');
121121

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

0 commit comments

Comments
 (0)