Skip to content
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

Feature request: isEmptyDir() #1037

Open
btea opened this issue Mar 19, 2024 · 9 comments
Open

Feature request: isEmptyDir() #1037

btea opened this issue Mar 19, 2024 · 9 comments

Comments

@btea
Copy link

btea commented Mar 19, 2024

Sometimes we encounter situations where we need to determine whether a directory is empty before deleting or performing other operations. Therefore, I think it might be convenient if an API can be provided.

fs.isEmptyDir(dir[,callback])
@RyanZim
Copy link
Collaborator

RyanZim commented Mar 19, 2024

This is basically a duplicate of #420; though I'm open to reconsidering the decision there.

Can you elaborate more on the use-case here?

@btea
Copy link
Author

btea commented Mar 19, 2024

This is basically a duplicate of #420; though I'm open to reconsidering the decision there.

Oh, sorry, I didn't notice this.

Can you elaborate more on the use-case here?

For example, in pnpm, there is a pnpm patch-remove command. After removing a certain patch file, you need to determine whether the directory where the patch file is stored is empty to decide whether to remove the corresponding folder.

There is also scaffolding that when initializing a project, it will also judge whether it is empty to decide whether to overwrite the content and continue or cancel. And I think this method can also be reused internally in emptyDir.

@RyanZim
Copy link
Collaborator

RyanZim commented Mar 20, 2024

I'm especially hesitant to add it as it's trivial to implement wherever it's needed; the code is literally:

!(await fs.readdir(dir)).length

Is it worth creating a method just for this?

@RyanZim
Copy link
Collaborator

RyanZim commented Mar 20, 2024

Also, in the case of something like emptyDir, you wouldn't actually want to use this, as you want the full output from fs.readdir anyhow.

@btea
Copy link
Author

btea commented Mar 21, 2024

I'm especially hesitant to add it as it's trivial to implement wherever it's needed; the code is literally:

!(await fs.readdir(dir)).length

Yes, the code implementation seems relatively simple.

Is it worth creating a method just for this?

It is hoped that providing this wrapper function is just a suggestion. It is difficult for me to give a strong support for whether it is worth adding. The scenario I am currently encountering is the example I provided above.

@RyanZim
Copy link
Collaborator

RyanZim commented Mar 21, 2024

cc @JPeer264 @manidlou @jprichardson for thoughts

@SukkaW
Copy link
Contributor

SukkaW commented Jul 11, 2024

@RyanZim I have some thoughts.

fs.readdir can be extremely slow if a folder contains many items (that's why #1028 is made, which replaces readdir w/ opendir).

fs.opendir is more performant (since it implements stream API), but it can't be used in a one-liner like !(await fs.readdir(dir)).length. Thus a util isEmptyDir using fs.opendir under the hood is trivial for me.

@RyanZim
Copy link
Collaborator

RyanZim commented Jul 11, 2024

@SukkaW That's an interesting point; in that case I'd be open to a PR adding it.

@SukkaW
Copy link
Contributor

SukkaW commented Jul 12, 2024

@SukkaW That's an interesting point; in that case I'd be open to a PR adding it.

I can create a PR adding it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants