Skip to content

Commit 5737998

Browse files
committed
Merge branch 'promise-support'
2 parents f7181a9 + 4fac3ac commit 5737998

14 files changed

+334
-79
lines changed

examples/promise.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use strict';
2+
3+
var glob = require('..');
4+
5+
glob.on('files', function(files) {
6+
console.log(files);
7+
});
8+
9+
glob.promise(['**/*.js', '**/*.md'])
10+
.then(null, console.error)
11+

index.js

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
var fs = require('fs');
44
var path = require('path');
55
var isGlob = require('is-glob');
6-
var each = require('async-each');
6+
var each = require('each-parallel-async');
77
var spawn = require('cross-spawn');
88
var isExtglob = require('is-extglob');
99
var extend = require('extend-shallow');
@@ -38,7 +38,10 @@ function glob(pattern, options, cb) {
3838
}
3939

4040
if (typeof cb !== 'function') {
41-
throw new TypeError('expected callback to be a function');
41+
if (typeof cb !== 'undefined') {
42+
throw new TypeError('expected callback to be a function');
43+
}
44+
return glob.promise.apply(glob, arguments);
4245
}
4346

4447
if (typeof pattern !== 'string') {
@@ -57,7 +60,7 @@ function glob(pattern, options, cb) {
5760
files = err;
5861
}
5962

60-
if (Array.isArray(files) && !files.length && opts.nullglob) {
63+
if (opts.nullglob === true && Array.isArray(files) && !files.length) {
6164
files = [pattern];
6265
}
6366

@@ -207,6 +210,22 @@ glob.sync = function(pattern, options) {
207210
return files;
208211
};
209212

213+
/**
214+
* Emit `end` and remove listeners
215+
*/
216+
217+
glob.promise = function(pattern, options, cb) {
218+
return new Promise(function(resolve, reject) {
219+
glob(pattern, options, function(err, files) {
220+
if (err) {
221+
reject(err);
222+
} else {
223+
resolve(files);
224+
}
225+
});
226+
});
227+
};
228+
210229
/**
211230
* Emit `end` and remove listeners
212231
*/
@@ -261,8 +280,6 @@ function bash(pattern, options, cb) {
261280
cb(code, getFiles(buf.toString(), pattern, options));
262281
});
263282
});
264-
265-
return glob;
266283
}
267284

268285
/**
@@ -271,12 +288,7 @@ function bash(pattern, options, cb) {
271288

272289
function normalize(val) {
273290
if (Array.isArray(val)) {
274-
var len = val.length;
275-
var idx = -1;
276-
while (++idx < len) {
277-
val[idx] = normalize(val[idx]);
278-
}
279-
return val.join(' ');
291+
val = val.join(' ');
280292
}
281293
return val.split(' ').join('\\ ');
282294
}
@@ -287,14 +299,25 @@ function normalize(val) {
287299

288300
function cmd(patterns, options) {
289301
var str = normalize(patterns);
290-
var valid = ['dotglob', 'extglob', 'failglob', 'globstar', 'nocaseglob', 'nullglob'];
302+
var keys = Object.keys(options);
291303
var args = [];
292-
for (var key in options) {
293-
if (options.hasOwnProperty(key) && valid.indexOf(key) !== -1) {
304+
var valid = [
305+
'dotglob',
306+
'extglob',
307+
'failglob',
308+
'globstar',
309+
'nocaseglob',
310+
'nullglob'
311+
];
312+
313+
for (var i = 0; i < keys.length; i++) {
314+
var key = keys[i];
315+
if (valid.indexOf(key) !== -1) {
294316
args.push('-O', key);
295317
}
296318
}
297-
args.push('-c', `for i in ${str}; do echo $i; done`);
319+
320+
args.push('-c', 'for i in ' + str + '; do echo $i; done');
298321
return args;
299322
}
300323

@@ -368,7 +391,10 @@ function getFiles(res, pattern, options) {
368391
return new Error('no matches:' + pattern);
369392
}
370393
}
371-
return files;
394+
395+
return files.filter(function(filepath) {
396+
return filepath !== '.' && filepath !== '..';
397+
});
372398
}
373399

374400
/**

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@
2020
"test": "mocha"
2121
},
2222
"dependencies": {
23-
"async-each": "^1.0.1",
2423
"bash-path": "^1.0.1",
2524
"component-emitter": "^1.2.1",
2625
"cross-spawn": "^5.1.0",
26+
"each-parallel-async": "^1.0.0",
2727
"extend-shallow": "^2.0.1",
2828
"is-extglob": "^2.1.1",
2929
"is-glob": "^4.0.0"

test/_setup.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
'use strict';
2+
3+
require('mocha');
4+
var del = require('delete');
5+
var path = require('path');
6+
var symlinks = require('./support/symlinks');
7+
var setup = require('./support/setup');
8+
var home = require('./support/home');
9+
10+
var fixtures = path.join(__dirname, 'fixtures');
11+
var files = [
12+
'a/.abcdef/x/y/z/a',
13+
'a/abcdef/g/h',
14+
'a/abcfed/g/h',
15+
'a/b/c/d',
16+
'a/bc/e/f',
17+
'a/c/d/c/b',
18+
'a/cb/e/f',
19+
'a/x/.y/b',
20+
'a/z/.y/b'
21+
];
22+
23+
describe('setup', function() {
24+
it('should remove fixtures', function(cb) {
25+
del([fixtures, '/tmp/glob-test'], {force: true}, cb);
26+
});
27+
28+
it('should create test fixtures', function(cb) {
29+
setup(files, fixtures, cb);
30+
});
31+
32+
it('should setup symlinks', function(cb) {
33+
symlinks(fixtures, cb);
34+
});
35+
36+
it('should setup fixtures in user home', function(cb) {
37+
home(['foo', 'bar', 'baz', 'asdf', 'quux', 'qwer', 'rewq'], cb);
38+
});
39+
});

test/glob.js renamed to test/api.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ describe('bash-glob', function() {
1111
assert.equal(typeof glob, 'function');
1212
});
1313

14-
it('should export a `.sync` method', function() {
14+
it('should expose a `.sync` method', function() {
1515
assert.equal(typeof glob.sync, 'function');
1616
});
1717
});
1818

1919
describe('async: errors', function() {
2020
it('should throw an error when a callback is not passed', function(cb) {
2121
try {
22-
glob();
22+
glob('*', {}, null);
2323
cb(new Error('expected an error'));
2424
} catch (err) {
2525
assert(err);

test/async.js

Lines changed: 0 additions & 41 deletions
This file was deleted.

test/broken-symlink.js

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ describe('set up broken symlink', function() {
3535
process.chdir(__dirname);
3636
cleanup();
3737
mkdirp.sync('fixtures/a/broken-link');
38-
fs.symlinkSync('this-does-not-exist', 'fixtures/a/broken-link/link');
39-
cb();
38+
fs.symlink('this-does-not-exist', 'fixtures/a/broken-link/link', cb);
4039
});
4140

4241
after(function(cb) {
@@ -45,7 +44,7 @@ describe('set up broken symlink', function() {
4544
cb();
4645
});
4746

48-
describe('async test', function() {
47+
describe('async', function() {
4948
patterns.forEach(function(pattern) {
5049
it(pattern, function(cb) {
5150
if (isWindows) {
@@ -76,7 +75,61 @@ describe('set up broken symlink', function() {
7675
});
7776
});
7877

79-
describe('sync test', function() {
78+
describe('promise - implied (no callback)', function() {
79+
patterns.forEach(function(pattern) {
80+
it(pattern, function(cb) {
81+
if (isWindows) {
82+
this.skip();
83+
return;
84+
}
85+
86+
each(options, function(opts, next) {
87+
glob(pattern, opts)
88+
.then(function(files) {
89+
var msg = pattern + ' options=' + JSON.stringify(opts);
90+
if (opts && opts.follow === true) {
91+
assert.equal(files.indexOf(link), -1, msg);
92+
} else if (pattern !== link || (opts && opts.nonull)) {
93+
assert.notEqual(files.indexOf(link), -1, msg);
94+
} else {
95+
assert(!files.length);
96+
}
97+
setImmediate(next);
98+
})
99+
.catch(next);
100+
}, cb);
101+
});
102+
});
103+
});
104+
105+
describe('promise - explicit', function() {
106+
patterns.forEach(function(pattern) {
107+
it(pattern, function(cb) {
108+
if (isWindows) {
109+
this.skip();
110+
return;
111+
}
112+
113+
each(options, function(opts, next) {
114+
glob.promise(pattern, opts)
115+
.then(function(files) {
116+
var msg = pattern + ' options=' + JSON.stringify(opts);
117+
if (opts && opts.follow === true) {
118+
assert.equal(files.indexOf(link), -1, msg);
119+
} else if (pattern !== link || (opts && opts.nonull)) {
120+
assert.notEqual(files.indexOf(link), -1, msg);
121+
} else {
122+
assert(!files.length);
123+
}
124+
setImmediate(next);
125+
})
126+
.catch(next);
127+
}, cb);
128+
});
129+
});
130+
});
131+
132+
describe('sync', function() {
80133
patterns.forEach(function(pattern) {
81134
it(pattern, function() {
82135
if (isWindows) {

test/empty-set.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,24 @@ var patterns = [
1313

1414
describe('empty sets', function() {
1515
patterns.forEach(function(pattern) {
16-
it(JSON.stringify(pattern), function(cb) {
16+
it('async: ' + JSON.stringify(pattern), function(cb) {
1717
glob(pattern, function(err, files) {
1818
if (err) return cb(err);
1919
assert.deepEqual(files, [], 'expected an empty array');
2020
cb();
2121
});
2222
});
23+
24+
it('promise: ' + JSON.stringify(pattern), function() {
25+
return glob(pattern)
26+
.then(function(files) {
27+
assert.deepEqual(files, [], 'expected an empty array');
28+
});
29+
});
30+
31+
it('sync: ' + JSON.stringify(pattern), function() {
32+
var files = glob.sync(pattern);
33+
assert.deepEqual(files, [], 'expected an empty array');
34+
});
2335
});
2436
});

test/options.cwd.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,10 @@ describe('options.cwd', function() {
7171
var notdir = 'a/b/c/d';
7272
var expected = 'cwd is not a directory: ' + notdir;
7373

74-
it('sync', function(cb) {
75-
try {
74+
it('sync', function() {
75+
assert.throws(function() {
7676
glob.sync('*', {cwd: notdir});
77-
cb(new Error('expected an error'));
78-
} catch (err) {
79-
assert.equal(err.message, expected);
80-
cb();
81-
}
77+
});
8278
});
8379

8480
it('async', function(cb) {
@@ -87,6 +83,16 @@ describe('options.cwd', function() {
8783
cb();
8884
});
8985
});
86+
87+
it('promise', function() {
88+
return glob('*', {cwd: notdir})
89+
.then(function() {
90+
return Promise.reject(new Error('expected glob error to be thrown'));
91+
})
92+
.catch(function(err) {
93+
assert.equal(err.message, expected);
94+
});
95+
});
9096
});
9197
});
9298

0 commit comments

Comments
 (0)