Reputation: 10093
I am creating test files from swagger API definition file. I use fs.writeFile
for creating test files (which are mostly stubs, so I need to add details to these files manually). Now the problem is that whenever I generate the test files again, it overwrites all the existing files as well.
So, my question is, is there some option for fs.writeFile
that we can set to specify that if a file is already existing, do not overwrite it?
Most obvious way seems to be to first check if a file exists and only generate it if it doesn't exist already. But, if there is some option in fs.writeFile
itself, that would be more convenient and compact.
Upvotes: 2
Views: 889
Reputation: 575
fs.writeFile don't have any options as such to check if file already existing.
I would recommend to use fs-extra. Below is one of the solution.
async function writeFile(f) {
try {
if ((await fs.pathExists(f)) === false) {
await fs.outputFile(f, 'hello!');
}
} catch (err) {
console.error(err);
}
}
writeFile('/tmp/this/path/does/not/exist/file.txt');
Upvotes: 1