|
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"); |
3 | 3 |
|
4 | 4 | exports.FileFinder = class FileFinder { |
5 | | - constructor(directory, { skipDotfiles, filter = () => true } = {}) { |
| 5 | + constructor(directory, { skipDotfiles, filter } = {}) { |
6 | 6 | this.directory = directory; |
7 | 7 | this.filter = filename => { |
8 | | - if(skipDotfiles && isDotfile(filename)) { |
| 8 | + if(skipDotfiles && path.basename(filename).startsWith(".")) { |
9 | 9 | return false; |
10 | 10 | } |
11 | | - return filter(filename); |
| 11 | + return filter === undefined ? true : filter(filename); |
12 | 12 | }; |
13 | 13 | } |
14 | 14 |
|
15 | 15 | // 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); |
19 | 19 | } |
20 | 20 |
|
21 | 21 | // returns all file paths that match the filter function |
22 | 22 | match(filepaths) { |
23 | | - return filesWithinDirectory(this.directory, filepaths). |
24 | | - then(filepaths => filepaths.filter(this.filter)); |
| 23 | + return filepaths.map(filepath => path.relative(this.directory, filepath)). |
| 24 | + filter(filename => !filename.startsWith("..")). |
| 25 | + filter(this.filter); |
25 | 26 | } |
26 | 27 | }; |
27 | 28 |
|
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 | + } |
56 | 34 |
|
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(); |
59 | 40 | } |
0 commit comments