From 94e6d8a68d24b6f512360e4ab46438178d62e318 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Wed, 16 Aug 2023 09:50:20 -0300 Subject: thumbs: traverse with wiki-matching posix style when verifying paths --- src/util/node-utils.js | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) (limited to 'src/util') 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, '')); } -- cgit 1.3.0-6-gf8a5