jprichardson / node-fs-extra

Node.js: extra methods for the fs object like copy(), remove(), mkdirs()
MIT License
9.43k stars 775 forks source link

Create file's directory when writing a file #1010

Closed musicq closed 1 year ago

musicq commented 1 year ago

Isn't it better to create the file's parent directories automatically when writing a file.

async function writeFile(fp, data) {
  await ensureDir(path.dirname(fp))
  fs.writeFile(fp, data)
}

So when we create the file, we no longer need to care about if the parent directory exists or not.

await writeFile('path/to/target/file.txt', 'data')
RyanZim commented 1 year ago

This is what .outputFile() does.