forked from yortus/asyncawait
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcountFiles.js
More file actions
20 lines (16 loc) · 785 Bytes
/
countFiles.js
File metadata and controls
20 lines (16 loc) · 785 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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;
// Return the number of files in the given directory
var countFiles = async (function(dir) {
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); }));
return _.filter(stats, function (stat) { return stat.isFile(); }).length;
});
countFiles(__dirname)
.then (function (num) { console.log('There are ' + num + ' files in ' + __dirname); })
.catch(function (err) { console.log('Something went wrong: ' + err); });