Skip to content

feature-request fs.fileWrite with option.mkdirp#33559

@kaizhu256

Description

@kaizhu256

Is your feature request related to a problem? Please describe.

i am author of npm-package istanbul-lite.
when creating coverage-reports and artifacts,
its common to lazily write data to file-directories that have yet to be created.

Describe the solution you'd like

add extra boolean <options>.mkdirp to functions fs.writeFile and fs.writeFileSync,
that will lazily create missing file-directories when writing to file.
the common-use-case are:

  • lazily generate directories when writing coverage-report of instrumented files
  • lazily generate directories when writing artifacts in ci and testing
  • lazily generate directories when scaffolding new web-projects

Describe alternatives you've considered

i currently use this helper-function in all my projects,
to lazily generate directories when writing artifacts and coverage-reports.

functionfsWriteFileWithMkdirpSync(file,data){/* * this function will sync write <data> to <file> with "mkdir -p" */letfs;// do nothing if module does not existtry{fs=require("fs");}catch(ignore){return;}// try to write filetry{fs.writeFileSync(file,data);returntrue;}catch(ignore){// mkdir -pfs.mkdirSync(require("path").dirname(file),{recursive: true});// rewrite filefs.writeFileSync(file,data);returntrue;}};

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature requestIssues that request new features to be added to Node.js.fsIssues and PRs related to the fs subsystem / file system.stale

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions