Skip to content

Commit

Permalink
Switch to tap and standard
Browse files Browse the repository at this point in the history
  • Loading branch information
bcomnes committed Dec 27, 2019
1 parent eb58691 commit fcc3ab1
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 91 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
sandbox.js
.nyc_output
64 changes: 32 additions & 32 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const fs = require('fs');
const path = require('path');
const fs = require('fs')
const path = require('path')

const { readdir, lstat } = fs.promises;
const { readdir, lstat } = fs.promises

/**
* pathFilter lets you filter files based on a resolved `filepath`.
Expand All @@ -10,7 +10,7 @@ const { readdir, lstat } = fs.promises;
*
* @return {Boolean} Return false to filter the given `filepath` and true to include it.
*/
const pathFilter = filepath => true;
const pathFilter = filepath => true

/**
* statFilter lets you filter files based on a lstat object.
Expand All @@ -19,7 +19,7 @@ const pathFilter = filepath => true;
*
* @return {Boolean} Return false to filter the given `filepath` and true to include it.
*/
const statFilter = filepath => true;
const statFilter = filepath => true

/**
* FWStats is the object that the okdistribute/folder-walker module returns by default.
Expand All @@ -39,7 +39,7 @@ const statFilter = filepath => true;
*
* @return {*} - Whatever you want returned from the directory walk.
*/
const shaper = ({ root, filepath, stat, relname, basename }) => filepath;
const shaper = ({ root, filepath, stat, relname, basename }) => filepath

/**
* Options object
Expand Down Expand Up @@ -70,33 +70,33 @@ async function * asyncFolderWalker (dirs, opts) {
statFilter,
maxDepth: Infinity,
shaper
}, opts);
}, opts)

const roots = [dirs].flat().filter(opts.pathFilter);
const pending = [];
const roots = [dirs].flat().filter(opts.pathFilter)
const pending = []

while (roots.length) {
const root = roots.shift();
pending.push(root);
const root = roots.shift()
pending.push(root)

while (pending.length) {
const current = pending.shift();
if (typeof current === 'undefined') continue;
const st = await lstat(current);
const current = pending.shift()
if (typeof current === 'undefined') continue
const st = await lstat(current)
if ((!st.isDirectory() || depthLimiter(current, root, opts.maxDepth)) && opts.statFilter(st)) {
yield opts.shaper(fwShape(root, current, st));
continue;
yield opts.shaper(fwShape(root, current, st))
continue
}

const files = await readdir(current);
files.sort();
const files = await readdir(current)
files.sort()

for (const file of files) {
var next = path.join(current, file);
if (opts.pathFilter(next)) pending.unshift(next);
var next = path.join(current, file)
if (opts.pathFilter(next)) pending.unshift(next)
}
if (current === root || !opts.statFilter(st)) continue;
else yield opts.shaper(fwShape(root, current, st));
if (current === root || !opts.statFilter(st)) continue
else yield opts.shaper(fwShape(root, current, st))
}
}
}
Expand All @@ -119,7 +119,7 @@ function fwShape (root, name, st) {
stat: st,
relname: root === name ? path.basename(name) : path.relative(root, name),
basename: path.basename(name)
};
}
}

/**
Expand All @@ -134,10 +134,10 @@ function fwShape (root, name, st) {
* @returns {Boolean} - Return true to signal stop descending.
*/
function depthLimiter (filePath, relativeTo, maxDepth) {
if (maxDepth === Infinity) return false;
const rootDepth = relativeTo.split(path.sep).length;
const fileDepth = filePath.split(path.sep).length;
return fileDepth - rootDepth > maxDepth;
if (maxDepth === Infinity) return false
const rootDepth = relativeTo.split(path.sep).length
const fileDepth = filePath.split(path.sep).length
return fileDepth - rootDepth > maxDepth
}

/**
Expand All @@ -148,13 +148,13 @@ function depthLimiter (filePath, relativeTo, maxDepth) {
* @private
*/
async function all (iterator) {
const collect = [];
const collect = []

for await (const result of iterator) {
collect.push(result);
collect.push(result)
}

return collect;
return collect
}

/**
Expand All @@ -169,11 +169,11 @@ async function all (iterator) {
* @returns {Promise<String[]|any>} - An async iterator that returns anything.
*/
async function allFiles (...args) {
return all(asyncFolderWalker(...args));
return all(asyncFolderWalker(...args))
}

module.exports = {
asyncFolderWalker,
allFiles,
all
};
}
11 changes: 5 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@
"dependency-check": "^4.1.0",
"npm-run-all": "^4.1.5",
"p-temporary-directory": "^1.1.1",
"semistandard": "^14.2.0",
"tape": "^4.11.0",
"tape-promise": "^4.0.0"
"standard": "^14.3.1",
"tap": "^14.10.5"
},
"homepage": "https://github.com/bcomnes/async-folder-walker",
"keywords": [],
Expand All @@ -26,11 +25,11 @@
"scripts": {
"test": "run-s test:*",
"test:deps": "dependency-check . --no-dev --no-peer",
"test:semistandard": "semistandard",
"test:tape": "tape test.js",
"test:standard": "standard",
"test:tap": "tap",
"debug": "node --nolazy --inspect-brk=9229 -r esm test.js"
},
"semistandard": {
"standard": {
"ignore": [
"dist"
]
Expand Down
104 changes: 51 additions & 53 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,94 +1,92 @@
const tape = require('tape');
const ptape = require('tape-promise').default;
const { asyncFolderWalker, allFiles } = require('.');
const path = require('path');
const tmp = require('p-temporary-directory');
const test = ptape(tape);
const tap = require('tap')
const { asyncFolderWalker, allFiles } = require('.')
const path = require('path')
const tmp = require('p-temporary-directory')

const fixtures = path.join(__dirname, 'fixtures');
const fixtures = path.join(__dirname, 'fixtures')

test('for of multiple folders', async t => {
tap.test('for of multiple folders', async t => {
for await (const file of asyncFolderWalker([
path.join(fixtures, 'sub-folder'),
path.join(fixtures, 'another-folder')
])) {
t.ok(file, file);
t.ok(file, file)
}
});
})

test('Array from async iterator', async t => {
tap.test('Array from async iterator', async t => {
const files = await allFiles([
path.join(fixtures, 'sub-folder'),
path.join(fixtures, 'another-folder')
]);
t.equal(files.length, 4, 'expected number of files are found');
});
])
t.equal(files.length, 4, 'expected number of files are found')
})

test('No args', async t => {
tap.test('No args', async t => {
for await (const file of asyncFolderWalker()) {
t.fail(file, 'no files should be found!');
t.fail(file, 'no files should be found!')
}
t.pass('for of executed');
});
t.pass('for of executed')
})

test('No folders', async t => {
const [dir, cleanup] = await tmp();
tap.test('No folders', async t => {
const [dir, cleanup] = await tmp()
try {
for await (const file of asyncFolderWalker(dir)) {
t.fail(file, 'no files should be found!');
t.fail(file, 'no files should be found!')
}
t.pass('for of executed');
t.pass('for of executed')
} finally {
await cleanup();
await cleanup()
}
});
})

test('When you just pass a file', async t => {
const [dir, cleanup] = await tmp();
tap.test('When you just pass a file', async t => {
const [dir, cleanup] = await tmp()
try {
const theFile = path.join(fixtures, 'test.json');
const files = await allFiles([theFile, dir]);
t.equal(files.length, 1, 'only one file is found');
t.equal(theFile, files[0], 'only one file is found');
const theFile = path.join(fixtures, 'test.json')
const files = await allFiles([theFile, dir])
t.equal(files.length, 1, 'only one file is found')
t.equal(theFile, files[0], 'only one file is found')
} finally {
await cleanup();
await cleanup()
}
});
})

test('pathFilter works', async t => {
const filterStrig = 'sub-folder';
tap.test('pathFilter works', async t => {
const filterStrig = 'sub-folder'
const files = await allFiles(fixtures, {
pathFilter: p => !p.includes(filterStrig)
});
})

t.false(files.some(f => f.includes(filterStrig)), 'No paths include the excluded string');
});
t.false(files.some(f => f.includes(filterStrig)), 'No paths include the excluded string')
})

test('statFilter works', async t => {
tap.test('statFilter works', async t => {
const stats = await allFiles(fixtures, {
statFilter: st => !st.isDirectory(), // Exclude files
shaper: ({ root, filepath, stat, relname, basename }) => stat // Lets get the stats instead of paths
});
})

for (const st of stats) {
t.false(st.isDirectory(), 'none of the files are directories');
t.false(st.isDirectory(), 'none of the files are directories')
}
});
})

test('dont include root directory in response', async (t) => {
const root = process.cwd();
tap.test('dont include root directory in response', async (t) => {
const root = process.cwd()
for await (const file of asyncFolderWalker(root)) {
if (file === root) t.fail('root directory should not be in results');
if (file === root) t.fail('root directory should not be in results')
}
t.pass('The root was not included in results.');
});
t.pass('The root was not included in results.')
})

test('dont walk past the maxDepth', async t => {
const maxDepth = 3;
const walker = asyncFolderWalker(['.git', 'node_modules'], { maxDepth });
tap.test('dont walk past the maxDepth', async t => {
const maxDepth = 3
const walker = asyncFolderWalker(['.git', 'node_modules'], { maxDepth })
for await (const file of walker) {
const correctLength = file.split(path.sep).length - process.cwd().split(path.sep).length <= maxDepth;
if (!correctLength) t.fail('walker walked past the depth it was supposed to');
const correctLength = file.split(path.sep).length - process.cwd().split(path.sep).length <= maxDepth
if (!correctLength) t.fail('walker walked past the depth it was supposed to')
}
t.pass('Walker was depth limited');
});
t.pass('Walker was depth limited')
})

0 comments on commit fcc3ab1

Please sign in to comment.