Closed
Description
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.
function fsWriteFileWithMkdirpSync (file, data) {
/*
* this function will sync write <data> to <file> with "mkdir -p"
*/
let fs;
// do nothing if module does not exist
try {
fs = require("fs");
} catch (ignore) {
return;
}
// try to write file
try {
fs.writeFileSync(file, data);
return true;
} catch (ignore) {
// mkdir -p
fs.mkdirSync(require("path").dirname(file), {
recursive: true
});
// rewrite file
fs.writeFileSync(file, data);
return true;
}
};