diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index 842fef5..b702ff6 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -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 diff --git a/lib/util/files/finder.js b/lib/util/files/finder.js index 2cf86d9..97900be 100644 --- a/lib/util/files/finder.js +++ b/lib/util/files/finder.js @@ -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; 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) { + 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(); } diff --git a/package.json b/package.json index 48bd905..2bca3d0 100644 --- a/package.json +++ b/package.json @@ -25,7 +25,7 @@ "lint": "eslint --cache ./lib ./bin/* ./test ./test/bin/* && echo ✓" }, "engines": { - "node": ">= 18" + "node": ">= 20" }, "dependencies": { "browserslist": "~4.24.4",