Jeff Williams 32f8aed805 replace jsdoc/src/filter and jsdoc/src/scanner
Plus some semi-related cleanup.
2019-02-02 14:42:20 -08:00

47 lines
1.0 KiB
JavaScript
Executable File

/**
* @module @jsdoc/util/lib/fs
*/
const _ = require('lodash');
const fs = require('fs');
const klaw = require('klaw-sync');
const path = require('path');
const walkSync = exports.walkSync = (filepaths, opts = {}) => {
const depth = _.has(opts, 'depth') ? opts.depth : -1;
let filter;
let found = [];
if (typeof filepaths === 'string') {
filepaths = [filepaths];
}
if (typeof opts.filter === 'function') {
filter = f => opts.filter.call(null, f.path);
}
filepaths.forEach(f => {
f = path.resolve(f);
if (fs.statSync(f).isFile()) {
found.push({ path: f });
} else {
found = found.concat(
klaw(f, {
depthLimit: depth,
filter: filter,
nodir: true
})
);
}
});
return found.map(f => f.path);
};
exports.lsSync = (dir, opts = {}) => walkSync(dir, {
depth: opts.depth,
filter: (f => !path.basename(f).startsWith('.')),
nodir: true
});