diff options
author | (quasar) nebula <qznebula@protonmail.com> | 2023-08-16 09:50:20 -0300 |
---|---|---|
committer | (quasar) nebula <qznebula@protonmail.com> | 2023-08-16 09:50:20 -0300 |
commit | 94e6d8a68d24b6f512360e4ab46438178d62e318 (patch) | |
tree | 8f6efe9e065c506ce79228ee9a6852a0a14c703a /src/util | |
parent | 7e7117e2e1c3d72393289f63695d4f86d358e7ed (diff) |
thumbs: traverse with wiki-matching posix style when verifying paths
Diffstat (limited to 'src/util')
-rw-r--r-- | src/util/node-utils.js | 35 |
1 files changed, 23 insertions, 12 deletions
diff --git a/src/util/node-utils.js b/src/util/node-utils.js index 6c75bab6..3c0dd4cd 100644 --- a/src/util/node-utils.js +++ b/src/util/node-utils.js @@ -58,21 +58,32 @@ export function isMain(importMetaURL) { // Like readdir... but it's recursive! export function traverse(startDirPath, { + pathStyle = 'device', filterFile = () => true, filterDir = () => true } = {}) { + const pathJoin = { + 'device': path.join, + 'posix': path.posix.join, + 'win32': path.win32.join, + }[pathStyle]; + + if (!pathJoin) { + throw new Error(`Expected pathStyle to be device, posix, or win32`); + } + const recursive = (names, subDirPath) => - Promise.all( - names.map((name) => - readdir(path.join(startDirPath, subDirPath, name)).then( - (names) => - filterDir(name) - ? recursive(names, path.join(subDirPath, name)) - : [], - () => (filterFile(name) ? [path.join(subDirPath, name)] : []) - ) - ) - ).then((pathArrays) => pathArrays.flatMap((x) => x)); + Promise.all(names.map(name => + readdir(pathJoin(startDirPath, subDirPath, name)).then( + names => + (filterDir(name) + ? recursive(names, pathJoin(subDirPath, name)) + : []), + () => + (filterFile(name) + ? [pathJoin(subDirPath, name)] + : [])))) + .then(pathArrays => pathArrays.flat()); - return readdir(startDirPath).then((names) => recursive(names, '')); + return readdir(startDirPath).then(names => recursive(names, '')); } |