Open btea opened 5 months ago
This is basically a duplicate of https://github.com/jprichardson/node-fs-extra/issues/420; though I'm open to reconsidering the decision there.
Can you elaborate more on the use-case here?
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
.
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?
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.
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.
cc @JPeer264 @manidlou @jprichardson for thoughts
@RyanZim I have some thoughts.
fs.readdir
can be extremely slow if a folder contains many items (that's why https://github.com/jprichardson/node-fs-extra/pull/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.
@SukkaW That's an interesting point; in that case I'd be open to a PR adding it.
@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!
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.