Skip to content

Commit e863b72

Browse files
committed
simplified FileFinder utility
mostly `async`/`await` and revisiting module-level utility functions
1 parent dae1cd5 commit e863b72

File tree

3 files changed

+24
-44
lines changed

3 files changed

+24
-44
lines changed

.github/workflows/tests.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@ jobs:
77
strategy:
88
matrix:
99
node-version:
10-
- 18.x
1110
- 20.x
1211
- 22.x
13-
- 23.x
12+
- latest
1413
steps:
1514
- uses: actions/checkout@v4
1615
- uses: actions/setup-node@v4

lib/util/files/finder.js

Lines changed: 22 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,40 @@
1-
let { readdir, stat } = require("fs/promises");
2-
let path = require("path");
1+
let { readdir, stat } = require("node:fs/promises");
2+
let path = require("node:path");
33

44
exports.FileFinder = class FileFinder {
5-
constructor(directory, { skipDotfiles, filter = () => true } = {}) {
5+
constructor(directory, { skipDotfiles, filter } = {}) {
66
this.directory = directory;
77
this.filter = filename => {
8-
if(skipDotfiles && isDotfile(filename)) {
8+
if(skipDotfiles && path.basename(filename).startsWith(".")) {
99
return false;
1010
}
11-
return filter(filename);
11+
return filter === undefined ? true : filter(filename);
1212
};
1313
}
1414

1515
// returns a list of relative file paths within the respective directory
16-
all() {
17-
return tree(this.directory).
18-
then(filenames => filenames.filter(this.filter));
16+
async all() {
17+
let filenames = await tree(this.directory);
18+
return filenames.filter(this.filter);
1919
}
2020

2121
// returns all file paths that match the filter function
22-
match(filepaths) {
23-
return filesWithinDirectory(this.directory, filepaths).
24-
then(filepaths => filepaths.filter(this.filter));
22+
async match(filepaths) {
23+
return filepaths.map(filepath => path.relative(this.directory, filepath)).
24+
filter(filename => !filename.startsWith("..")).
25+
filter(this.filter);
2526
}
2627
};
2728

28-
function tree(filepath, referenceDir = filepath) {
29-
return stat(filepath).
30-
then(res => {
31-
if(!res.isDirectory()) {
32-
return [path.relative(referenceDir, filepath)];
33-
}
34-
35-
return readdir(filepath).
36-
then(entries => {
37-
let res = Promise.all(entries.map(entry => {
38-
return tree(path.join(filepath, entry), referenceDir);
39-
}));
40-
return res.then(flatten);
41-
});
42-
});
43-
}
44-
45-
function filesWithinDirectory(directory, files) {
46-
return new Promise(resolve => {
47-
resolve(files.
48-
map(filepath => path.relative(directory, filepath)).
49-
filter(filename => !filename.startsWith("..")));
50-
});
51-
}
52-
53-
function isDotfile(filename) {
54-
return path.basename(filename).startsWith(".");
55-
}
29+
async function tree(filepath, referenceDir = filepath) {
30+
let res = await stat(filepath);
31+
if(!res.isDirectory()) {
32+
return [path.relative(referenceDir, filepath)];
33+
}
5634

57-
function flatten(arr) {
58-
return [].concat.apply([], arr);
35+
let entries = await readdir(filepath);
36+
entries = await Promise.all(entries.map(entry => {
37+
return tree(path.join(filepath, entry), referenceDir);
38+
}));
39+
return entries.flat();
5940
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"lint": "eslint --cache ./lib ./bin/* ./test ./test/bin/* && echo ✓"
2626
},
2727
"engines": {
28-
"node": ">= 18"
28+
"node": ">= 20"
2929
},
3030
"dependencies": {
3131
"browserslist": "~4.24.4",

0 commit comments

Comments
 (0)