Last active
July 27, 2020 18:59
-
-
Save monochromer/736d857e2955052fac607c4812d1fbe3 to your computer and use it in GitHub Desktop.
Walk directory on node.js without recursion
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 path = require('path') | |
const fs = require('fs').promises | |
async function walk(fsPath, callback) { | |
const node = { | |
path: fsPath, | |
stat: await fs.stat(fsPath) | |
} | |
const stack = [node] | |
let item | |
while (item = stack.shift()) { | |
if (item.stat.isDirectory()) { | |
const items = await fs.readdir(item.path, { | |
withFileTypes: true | |
}) | |
const nodes = items.map(direntItem => ({ | |
stat: direntItem, | |
path: path.join(item.path, direntItem.name) | |
})) | |
stack.push(...nodes) | |
} else { | |
callback(item) | |
} | |
} | |
} | |
(async function main() { | |
await walk(__dirname, (arg) => console.log(arg.path)) | |
})(); |
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').promises; | |
const path = require('path'); | |
async function* walk(dirPath) { | |
let item; | |
const queue = [dirPath]; | |
while (item = queue.shift()) { | |
const dir = await fs.opendir(item); | |
for await (const dirent of dir) { | |
const direntPath = path.join(item, dirent.name); | |
if (dirent.isDirectory()) { | |
queue.push(direntPath); | |
} | |
yield { dirent, direntPath }; | |
} | |
} | |
} | |
module.exports = walk; |
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 path = require('path') | |
const fs = require('fs').promises | |
async function* walk(fsPath) { | |
const node = { | |
path: fsPath, | |
stat: await fs.stat(fsPath) | |
} | |
const stack = [node] | |
let item | |
while (item = stack.shift()) { | |
if (item.stat.isDirectory()) { | |
const items = await fs.readdir(item.path, { | |
withFileTypes: true | |
}) | |
const nodes = items.map(direntItem => ({ | |
stat: direntItem, | |
path: path.join(item.path, direntItem.name) | |
})) | |
stack.push(...nodes) | |
} else { | |
yield item | |
} | |
} | |
} | |
module.exports = walk; |
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'); | |
const { promisify } = require('util'); | |
function readDir(path) { | |
return promisify(fs.readdir)(path, { withFileTypes: true }); | |
} | |
async function getAllFiles(dirPath, files = []) { | |
const items = await readDir(dirPath); | |
for (const dirItem of items) { | |
const newPath = path.join(dirPath, dirItem.name); | |
if (dirItem.isDirectory()) { | |
await getAllFiles(newPath, files); | |
} else { | |
files.push(newPath); | |
} | |
} | |
return files; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment