forked from yortus/asyncawait
-
Notifications
You must be signed in to change notification settings - Fork 0
/
descendentFilePaths.js
37 lines (29 loc) · 1.02 KB
/
descendentFilePaths.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
var Promise = require('bluebird');
var fs = Promise.promisifyAll(require('fs'));
var path = require('path');
var _ = require('lodash');
var async = require('..').async;
var await = require('..').await;
var descendentFilePaths = async.iterable (function self(yield_, dir, recursive) {
var files = await (fs.readdirSync(dir));
var paths = _.map(files, function (file) { return path.join(dir, file); });
var stats = await (_.map(paths, function (path) { return fs.statAsync(path); }));
_.each(stats, function(stat, i) {
if (stat.isFile()) yield_ (paths[i]);
else if (recursive) self(yield_, paths[i], true);
});
});
var program = async (function(dir) {
var paths = descendentFilePaths(dir, true);
paths.forEach(console.log);
return 'Finished!';
});
console.log('running...');
program(path.join(__dirname, '.'))
.then(function (result) {
console.log(result);
})
.catch(function(err) {
console.log('----- rejected: -----');
console.log(err);
});