Skip to content

Instantly share code, notes, and snippets.

@yuryshulaev
Last active May 31, 2016 18:06
Show Gist options
  • Save yuryshulaev/3ec50bf6b1b4331ac9d6 to your computer and use it in GitHub Desktop.
Save yuryshulaev/3ec50bf6b1b4331ac9d6 to your computer and use it in GitHub Desktop.
'use strict'
// babel countfiles_es7.js -o countfiles_es7.out.js --optional runtime --source-maps inline
require('source-map-support').install();
import Promise from 'bluebird';
import path from 'path';
let fs = Promise.promisifyAll(require('fs'));
class FileCounter {
static async countFiles(dir) {
let files = await fs.readdirAsync(dir);
let paths = files.map(file => path.join(dir, file));
let stats = await* paths.map(path => fs.statAsync(path));
return stats.filter(stat => stat.isFile()).length;
}
}
(async function () {
try {
let num = await FileCounter.countFiles(__dirname);
console.log('There are ' + num + ' files in ' + __dirname);
} catch (err) {
console.error(err);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment