Skip to content

Commit

Permalink
feat(jsdoc-core): add @jsdoc/core.util.fs.lsSync
Browse files Browse the repository at this point in the history
  • Loading branch information
hegemonic committed Dec 8, 2019
1 parent 7be06df commit 840352f
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 1 deletion.
19 changes: 19 additions & 0 deletions packages/jsdoc-core/lib/util/fs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* @module @jsdoc/util/lib/fs
*/

const _ = require('lodash');
const klawSync = require('klaw-sync');
const path = require('path');

exports.lsSync = ((dir, opts = {}) => {
const depth = _.has(opts, 'depth') ? opts.depth : -1;

const files = klawSync(dir, {
depthLimit: depth,
filter: (f => !path.basename(f.path).startsWith('.')),
nodir: true
});

return files.map(f => f.path);
});
4 changes: 3 additions & 1 deletion packages/jsdoc-core/lib/util/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const cast = require('./cast');
const fs = require('./fs');

module.exports = {
cast
cast,
fs
};
26 changes: 26 additions & 0 deletions packages/jsdoc-core/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions packages/jsdoc-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
},
"dependencies": {
"cosmiconfig": "^6.0.0",
"klaw-sync": "^6.0.0",
"lodash": "^4.17.15",
"mkdirp": "^0.5.1",
"ow": "^0.15.0",
"strip-bom": "^4.0.0",
"strip-json-comments": "^3.0.1",
Expand Down
71 changes: 71 additions & 0 deletions packages/jsdoc-core/test/specs/lib/util/fs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
describe('@jsdoc/util/lib/fs', () => {
const mockFs = require('mock-fs');
const fsUtil = require('../../../../lib/util/fs');
const path = require('path');

afterEach(() => mockFs.restore());

it('has an lsSync method', () => {
expect(fsUtil.lsSync).toBeFunction();
});

describe('lsSync', () => {
beforeEach(() => {
mockFs({
head: {
eyes: '',
ears: '',
mouth: '',
nose: '',
shoulders: {
knees: {
meniscus: '',
toes: {
phalanx: '',
'.big-toe-phalanx': ''
}
}
}
}
});
});

const cwd = process.cwd();

function resolvePaths(files) {
return files.map(f => path.join(cwd, f)).sort();
}

const allFiles = resolvePaths([
'head/eyes',
'head/ears',
'head/mouth',
'head/nose',
'head/shoulders/knees/meniscus',
'head/shoulders/knees/toes/phalanx'
]);

it('gets all non-hidden files from all levels by default', () => {
const files = fsUtil.lsSync(cwd).sort();

expect(files).toEqual(allFiles);
});

it('limits recursion depth when asked', () => {
const files = fsUtil.lsSync(cwd, { depth: 1 }).sort();

expect(files).toEqual(resolvePaths([
'head/eyes',
'head/ears',
'head/mouth',
'head/nose'
]));
});

it('treats a depth of -1 as infinite', () => {
const files = fsUtil.lsSync('head', { depth: -1 }).sort();

expect(files).toEqual(allFiles);
});
});
});
8 changes: 8 additions & 0 deletions packages/jsdoc-core/test/specs/lib/util/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,12 @@ describe('@jsdoc/core/lib/util', () => {
expect(util.cast).toBe(cast);
});
});

describe('fs', () => {
it('is lib/util/fs', () => {
const fs = require('../../../../lib/util/fs');

expect(util.fs).toBe(fs);
});
});
});

0 comments on commit 840352f

Please sign in to comment.