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: provide deep (recursive) fs.writeFile() #37733

Closed
bnb opened this issue Mar 12, 2021 · 8 comments
Closed

Feature request: provide deep (recursive) fs.writeFile() #37733

bnb opened this issue Mar 12, 2021 · 8 comments
Labels
feature request Issues that request new features to be added to Node.js. fs Issues and PRs related to the fs subsystem / file system. stale

Comments

@bnb
Copy link
Contributor

bnb commented Mar 12, 2021

Is your feature request related to a problem? Please describe.
Please describe the problem you are trying to solve.

Having to write the same boilerplate for writing a file to a directory that doesn't yet exist over and over again.

Describe the solution you'd like
Please describe the desired behavior.

Ideally, I'd be able to use fs.writeFile() (or fs.writeFileSync()) with the { recursive: true } option that's available on fs.mkdir() and its friends.

const path = require('path')

const resolvedPath = path.resolve('./somePath')
const data = {
  some: 'data'
}

fs.writeFile(path, data, { recursive: true }, (error) => {
  if (error) throw error
})

Describe alternatives you've considered
Please describe alternative solutions or features you have considered.

  • Checking if the directory exits recursively then writing the file if that succeeds
    • This is annoying and frankly ugly boilerplate for a common operation.
  • Using community tooling
    • Since this is an extension of a core method, I don't particularly feel great about taking on a third-party dependency to fill a gap so I can be relatively lazy.
    • This is also an option that could theoretically benefit anyone writing files to a directory that may not exist. I think that filling that gap in our own API rather than pushing folks to community tools will be a win for end-users' time in addition to their dependency tree.
@cjihrig
Copy link
Contributor

cjihrig commented Mar 12, 2021

Checking if the directory exits recursively then writing the file if that succeeds

Can't you use mkdir()'s recursive option followed by the file write?

@bnb
Copy link
Contributor Author

bnb commented Mar 12, 2021

@cjihrig yep, you can - that's what I was referring to. I've written that boilerplate often and it feels relatively unnecessary if we know users will follow that pattern.

@Fishrock123
Copy link
Contributor

Fishrock123 commented Mar 12, 2021

Just here to note that "recursive" doesn't make sense in this context. (Recursively writing to a file? What would that even mean? Infinite loop into a pipe?)

Something like "createDirs" would be better.

@bnb
Copy link
Contributor Author

bnb commented Mar 12, 2021

I used recursive here for consistency with fs.writeFile()'s API since I generally prefer to avoid one-off terminology, but I've got zero investment in it being the final term that the feature would land with.

@aduh95 aduh95 added feature request Issues that request new features to be added to Node.js. fs Issues and PRs related to the fs subsystem / file system. labels Mar 13, 2021
@bnb
Copy link
Contributor Author

bnb commented Mar 26, 2021

I don't quite know if this is high enough quality to make it into core, or if we can even do something like using one public method inside another public method, but here's a tiny example of how it can be done with consumer code. I imagine that if we don't like consuming one public method in another, the code from fs.mkdir() could just be effectively copy/pasted or made into an internal module.

https://gist.github.com/bnb/2345f18a7fd1bb16ee5d8476b9833bd3

@github-actions
Copy link
Contributor

There has been no activity on this feature request for 5 months and it is unlikely to be implemented. It will be closed 6 months after the last non-automated comment.

For more information on how the project manages feature requests, please consult the feature request management document.

@github-actions github-actions bot added the stale label Mar 22, 2022
@targos targos moved this from Pending Triage to Stale in Node.js feature requests Mar 22, 2022
@targos targos moved this to Pending Triage in Node.js feature requests Mar 22, 2022
@github-actions
Copy link
Contributor

There has been no activity on this feature request and it is being closed. If you feel closing this issue is not the right thing to do, please leave a comment.

For more information on how the project manages feature requests, please consult the feature request management document.

@William-McGonagle
Copy link

This seemed like a cool request– is there any status on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Issues that request new features to be added to Node.js. fs Issues and PRs related to the fs subsystem / file system. stale
Projects
None yet
Development

No branches or pull requests

5 participants