Created
September 23, 2021 18:23
-
-
Save robinmollah/e41a9f62c073b9e9295814a952c2d6ab to your computer and use it in GitHub Desktop.
Looks for `searchQuery` subdirectory in `folder`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require("fs"); | |
const path = require("path"); | |
function findFolder(folder, searchQuery) { | |
if (!fs.existsSync(folder)) { | |
console.error("dir not found ", folder); | |
return; | |
} | |
let subdirs = fs | |
.readdirSync(folder, { withFileTypes: true }) | |
.filter((cont) => cont.isDirectory()) | |
.map((cont) => cont.name); | |
if (subdirs.length) { | |
let searchIndex = subdirs.indexOf(searchQuery); | |
if (searchIndex > -1) { | |
const absolutePath = path.join( | |
path.resolve(folder), | |
subdirs[searchIndex] | |
); | |
return [absolutePath]; | |
} | |
for (let subdir of subdirs) { | |
let sub = findFolder(path.join(folder, subdir), searchQuery); | |
if (sub.length) { | |
return sub; | |
} | |
} | |
return []; | |
} else { | |
return []; | |
} | |
} | |
const searchResult = findFolder("./", "is-stream")[0]; | |
console.log(searchResult); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment