Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ jobs:
strategy:
matrix:
node-version:
- 18.x
- 20.x
- 22.x
- 23.x
- latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
Expand Down
63 changes: 22 additions & 41 deletions lib/util/files/finder.js
Original file line number Diff line number Diff line change
@@ -1,59 +1,40 @@
let { readdir, stat } = require("fs/promises");
let path = require("path");
let { readdir, stat } = require("node:fs/promises");
let path = require("node:path");

exports.FileFinder = class FileFinder {
constructor(directory, { skipDotfiles, filter = () => true } = {}) {
constructor(directory, { skipDotfiles, filter } = {}) {
this.directory = directory;
Comment on lines +5 to 6
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
constructor(directory, { skipDotfiles, filter } = {}) {
this.directory = directory;
constructor(root, { skipDotfiles, filter } = {}) {
this._root = root;

It doesn't look like the directory property is used by anyone; I've checked faucet-static and faucet-images.

this.filter = filename => {
if(skipDotfiles && isDotfile(filename)) {
if(skipDotfiles && path.basename(filename).startsWith(".")) {
return false;
}
return filter(filename);
return filter === undefined ? true : filter(filename);
};
}

// returns a list of relative file paths within the respective directory
all() {
return tree(this.directory).
then(filenames => filenames.filter(this.filter));
async all() {
let filenames = await tree(this.directory);
return filenames.filter(this.filter);
}

// returns all file paths that match the filter function
match(filepaths) {
return filesWithinDirectory(this.directory, filepaths).
then(filepaths => filepaths.filter(this.filter));
async match(filepaths) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

async isn't actually required by the implementation nor by downstream (i.e. faucet-static and faucet-images), but current tests demand it...

... except if the filter option is async, so I guess it's best to leave this as is, if only because it allows for more flexibility.

return filepaths.map(filepath => path.relative(this.directory, filepath)).
filter(filename => !filename.startsWith("..")).
filter(this.filter);
}
};

function tree(filepath, referenceDir = filepath) {
return stat(filepath).
then(res => {
if(!res.isDirectory()) {
return [path.relative(referenceDir, filepath)];
}

return readdir(filepath).
then(entries => {
let res = Promise.all(entries.map(entry => {
return tree(path.join(filepath, entry), referenceDir);
}));
return res.then(flatten);
});
});
}

function filesWithinDirectory(directory, files) {
return new Promise(resolve => {
resolve(files.
map(filepath => path.relative(directory, filepath)).
filter(filename => !filename.startsWith("..")));
});
}

function isDotfile(filename) {
return path.basename(filename).startsWith(".");
}
async function tree(filepath, referenceDir = filepath) {
let res = await stat(filepath);
if(!res.isDirectory()) {
return [path.relative(referenceDir, filepath)];
}

function flatten(arr) {
return [].concat.apply([], arr);
let entries = await readdir(filepath);
entries = await Promise.all(entries.map(entry => {
return tree(path.join(filepath, entry), referenceDir);
}));
return entries.flat();
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"lint": "eslint --cache ./lib ./bin/* ./test ./test/bin/* && echo ✓"
},
"engines": {
"node": ">= 18"
"node": ">= 20"
},
"dependencies": {
"browserslist": "~4.24.4",
Expand Down