node-fs-extra
node-fs-extra copied to clipboard
Default content in ensureFile()/ensureFileSync()
Hi!
This is a feature request.
I though about an additionnal option to ensureFile(...)
/ensureFileSync(...)
. It consists adding a default content to the file whenever it gets created.
Whenever the file already exists:
- do nothing, like before
If the file is created:
- create the file and its directories like before, AND write given data in the file
This way we could ensure that the content of the file a JSON Object or a JSON Array.
-
Operating System:
-
Windows 10
-
Node.js version:
-
Node:
v12.18.3
-
npm:
v7.6.3
-
fs-extra
version:
"devDependencies": {
"@types/fs-extra": "^9.0.9",
},
"dependencies": {
"fs-extra": "^9.1.0"
},
Thanks for taking the time to read this !
I don't see a technical reason why this couldn't be done; but I'd like feedback from the other maintainers if this is a common enough use-case to be worth supporting. @jprichardson @manidlou @JPeer264
@MartinHeywang I'm curious, what's the real-world use-case you have in mind for this?
@RyanZim On a personal project, I want to create a JSON config file.
As far as I know, JSON.parse()
doesn't accept empty strings. (I probably should check that)
The feature I'm asking would allow to create default values in such a config file, or simply make sure the data in it is parsable JSON, either an array or an object.
For the time being, I just check if the file exists, and then write it if it doesn't. (and I think that's what you would do to implement it). It's neither urgent nor important, but it could be useful.
If you don't think this is useful enough (and don't have time for it) , I may create a PR, so you don't have to do it yourself.
PS: I 😍 fs-extra
This is a very useful feature, please add ensureFile with the default value or clone the static file.
Why there is fse.readJSONSync but not fse.ensureJSONSync?
@dmatora what would ensureJSON's behavior be?
@RyanZim would now be a good time to reconsider the closed PR? https://github.com/jprichardson/node-fs-extra/pull/194
Would it at least be possible for ensureFile to return the "ensured" file? Otherwise need to perform another read to get it's handle.
The more I think about this, the more I'm opposed to adding additional functionality to ensureFile
. There's a potentially limitless number of operations we could chain onto ensureFile
, such as writing content if it doesn't exist, reading the file if it does exist, etc. At the end of the day, these are all niche use-cases, best served by custom logic.
Seems that writing default content and having a handle to the file are being requested because there's a need for them -- are they really that niche? With the PR above from 2015 and this issue from 2021 seems that over the years there's still demand for augmenting ensureFile
's capabilities. How would having ensureFile
fulfill these needs be detrimental to the library? For sure the possibilities for ensureFile are limitless -- when aren't they for any API? These in particular though do seem to be useful and requested by users.
fs-extra
has over 300M downloads a month, yet only a handful of people have indicated interest in the feature over the years; I would argue that this is the definition of a niche request.