In Files Matching a Given Pattern.
Install Node >= v6 and replem.
Start a replem session using the command below:
$ replem glob fs-extra:fs bluebird:Promise
then paste the following lines in and press enter:
const plus = (a, b) => a + b;
const sum = s => s.reduce(plus, 0);
const countText = t => (t.match(/[^\x00-\xff]/g) || []).length;
const limitTo = (s, l, t) => s.length <= l ? s : s.substr(0, l - t - 3) + '...' + s.substr(s.length - t);
const str = o => limitTo(JSON.stringify(o), 80, 50);
const read = Promise.promisify(fs.readFile);
const glop = Promise.promisify(glob);
const logPromise = fn => arg => {
var sArg = str(arg);
console.log('start', sArg);
return fn(arg).tap(ret => console.log(' got', sArg, '=', ret));
};
const [countInFile, countInFiles, countByPattern] = [
file => read(file, 'utf-8').then(countText),
files => Promise.all(files.map(countInFile)).then(sum),
pattern => glop(pattern).then(countInFiles)
].map(logPromise);
Then run this, with your own pattern:
countByPattern('/code/private/draft/translation/**/*.md')
\w/