-
Notifications
You must be signed in to change notification settings - Fork 29.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Recursively list file paths in directory #34992
Comments
Seems like an interesting proposal I can see added to a module like function could take a single parameter string, which would be the starting directory to begin recursively printing. possibly include a second parameter for searching for a specific file or folder match in regex? |
I don't think it's worth adding a new API for that, it's already possible to do it in user land. I would be more relevant as an npm package rather than a core utility. |
FYI @nodejs/tooling who have previously expressed an interest in adding recursive options to Node.js APIs. |
I'd be keen on developing this first as a standalone module. Then we can consider adopting it into core |
Related, I stumbled across this issue a while ago. There's currently a module with 16m downloads/month called recursive-readdir that seems to be widely used. At the time I encountered an issue with it and reached out to the author - he said he doesn't really have time to maintain it and asked if I'd wanted to be a maintainer. I am now a maintainer 😅 The implementation in the module is pretty good from what I can tell - the best that currently exists, I think. I'd be happy to help migrate it into Node.js if it makes sense. Also happy to have you as a maintainer if you're interested in improving upon it as a base, @Ethan-Arrowood. |
This looks great and yes I'd be happy to help maintain it with you @bnb |
Extending what @richardlau said, I do think it makes sense to move it into Node.js at some point. I've just not done the work to start that discussion 😅 |
Yeah I'm also wondering if this is one of those 'add to core' or 'add to org' like undici |
I don't think we should vendor a module that reads directories via |
I was thinking about contributing the stream version to recursive-readdir |
i.e. maybe something based around this one: https://nodejs.org/api/fs.html#fs_fspromises_opendir_path_options |
async function listDir (path, acc = []) {
const dir = await opendir(path)
for await (const dirent of dir) {
if (dirent.isDirectory()) {
listDir(join(path,dirent.name), acc)
} else {
acc.push(join(path, dirent.name))
}
}
return acc
} |
async function* listDir(path) {
const dir = await opendir(path);
for await (const dirent of dir) {
const name = join(path, dirent.name);
if (dirent.isDirectory()) {
yield* listDir(name);
} else {
yield name;
}
}
} |
Fantastic use of async generators! I think this is really solid solution |
Added support for ignoring the root path async function* listDir(path, opts = { ignoreRoot: false }) {
if (this.meta == null) {
this.meta = { root: path }
}
const dir = await opendir(path)
for await (const dirent of dir) {
const name = join(path, dirent.name)
if (dirent.isDirectory()) {
yield* listDir(name, opts)
} else {
yield opts.ignoreRoot ? relative(this.meta.root, name) : name
}
}
} Alternate: async function* listDir(path, opts = { ignoreRoot: false }, meta = { root: path }) {
const dir = await opendir(path)
for await (const dirent of dir) {
const name = join(path, dirent.name)
if (dirent.isDirectory()) {
yield* listDir(name, opts, meta)
} else {
yield opts.ignoreRoot ? relative(meta.root, name) : name
}
}
} TypeScript version: async function* listDir(
path: string,
opts: options = { ignoreRoot: false },
meta: meta = { root: path }
): AsyncIterable<string> {
const dir = await opendir(path)
for await (const dirent of dir) {
const name = join(path, dirent.name)
if (dirent.isDirectory()) {
yield* listDir(name, opts, meta)
} else {
yield opts.ignoreRoot ? relative(meta.root, name) : name
}
}
} |
I wonder how any of the suggested iterators compare up to |
@Ethan-Arrowood @jimmywarting I missed this issue when I was flagged on it last year, this sounds like a great addition to the |
I'll put this back on top of my stack of things to contribute. I'll try to open a PR soon with something and we can all discuss from there! |
@Ethan-Arrowood 👍 I'd make sure to loop in @bnb on the PR, they've been contributing to |
Adds a naive, linear recursive algorithm for the following methods: readdir, readdirSync, opendir, opendirSync, and the promise based equivalents. Fixes: nodejs#34992
Adds a naive, linear recursive algorithm for the following methods: readdir, readdirSync, opendir, opendirSync, and the promise based equivalents. Fixes: nodejs#34992
Adds a naive, linear recursive algorithm for the following methods: readdir, readdirSync, opendir, opendirSync, and the promise based equivalents. Fixes: nodejs#34992
Adds a naive, linear recursive algorithm for the following methods: readdir, readdirSync, opendir, opendirSync, and the promise based equivalents. Fixes: nodejs#34992
Adds a naive, linear recursive algorithm for the following methods: readdir, readdirSync, opendir, opendirSync, and the promise based equivalents. Fixes: nodejs#34992
Adds a naive, linear recursive algorithm for the following methods: readdir, readdirSync, opendir, opendirSync, and the promise based equivalents. Fixes: nodejs#34992
Adds a naive, linear recursive algorithm for the following methods: readdir, readdirSync, opendir, opendirSync, and the promise based equivalents. Fixes: nodejs#34992
Adds a naive, linear recursive algorithm for the following methods: readdir, readdirSync, opendir, opendirSync, and the promise based equivalents. Fixes: nodejs#34992
Adds a naive, linear recursive algorithm for the following methods: readdir, readdirSync, opendir, opendirSync, and the promise based equivalents. Fixes: nodejs#34992
Adds a naive, linear recursive algorithm for the following methods: readdir, readdirSync, opendir, opendirSync, and the promise based equivalents. Fixes: #34992 PR-URL: #41439 Refs: nodejs/tooling#130 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
Adds a naive, linear recursive algorithm for the following methods: readdir, readdirSync, opendir, opendirSync, and the promise based equivalents. Fixes: #34992 PR-URL: #41439 Refs: nodejs/tooling#130 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
Adds a naive, linear recursive algorithm for the following methods: readdir, readdirSync, opendir, opendirSync, and the promise based equivalents. Fixes: #34992 PR-URL: #41439 Refs: nodejs/tooling#130 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
Adds a naive, linear recursive algorithm for the following methods: readdir, readdirSync, opendir, opendirSync, and the promise based equivalents. Fixes: nodejs#34992 PR-URL: nodejs#41439 Refs: nodejs/tooling#130 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
Is your feature request related to a problem? Please describe.
Please describe the problem you are trying to solve.
Recursively list file paths in a directory
Describe the solution you'd like
Please describe the desired behavior.
Dir structure like:
Should return ['a/b/c/d.js', 'a/b/c/e.js', 'a/b/f.js', 'a/g.js']
Describe alternatives you've considered
Please describe alternative solutions or features you have considered.
Some public solutions that are synchronous and may be able to made faster:
I wrote a version of this using async/await and typescript tweet. Not tested much and needs to be extrapolated for general purpose use
If this something the community is interested in I'd be happy to contribute it!
The text was updated successfully, but these errors were encountered: