From 45e7b64fda6f97b01a886e8d6dbcbdce611f3a16 Mon Sep 17 00:00:00 2001 From: kai zhu Date: Sun, 10 Jan 2021 22:57:02 -0600 Subject: [PATCH] fs: add option `parents` to - fs.open[Sync], - fs.writeFile[Sync], - fs.appendFile[Sync] this feature is intended to improve ergonomics/simplify-scripting when: - creating build-artifacts/coverage-files during ci - scaffolding new web-projects - cloning website with web-crawler allowing user to lazily create ad-hoc directory-structures as need during file-creation with ergonomic syntax: ``` fs.writeFileSync( "foo/bar/baz/qux.txt", "hello world!", { parents: true } // will lazily create parent foo/bar/baz/ as needed ); ``` fs: add new signature-form fs.open(path[, options], callback) fs: add new signature-form fs.openSync(path[, options]) Fixes: https://github.com/nodejs/node/issues/33559 --- doc/api/fs.md | 64 +++++ lib/fs.js | 95 ++++++- test/parallel/test-fs-open-options.js | 251 ++++++++++++++++++ .../test-fs-write-file-with-parents.js | 199 ++++++++++++++ 4 files changed, 596 insertions(+), 13 deletions(-) create mode 100644 test/parallel/test-fs-open-options.js create mode 100644 test/parallel/test-fs-write-file-with-parents.js diff --git a/doc/api/fs.md b/doc/api/fs.md index 8f896437d7438dd..a775d22255fdcce 100644 --- a/doc/api/fs.md +++ b/doc/api/fs.md @@ -1354,6 +1354,9 @@ try { + +* `path` {string|Buffer|URL} +* `options` {Object|string} + * `flags` {string|number} See [support of file system `flags`][]. + **Default:** `'r'`. + * `mode` {string|integer} **Default:** `0o666` (readable and writable) + * `parents` {boolean} create parent-directories if they do not exist. + **Default:** `false`. + * `callback` {Function} + * `err` {Error} + * `fd` {integer} + +Asynchronous file open using `options` signature-form. + ## `fs.opendir(path[, options], callback)` + +* `path` {string|Buffer|URL} +* `options` {Object|string} + * `flags` {string|number} See [support of file system `flags`][]. + **Default:** `'r'`. + * `mode` {string|integer} **Default:** `0o666` (readable and writable) + * `parents` {boolean} create parent-directories if they do not exist. + **Default:** `false`. + * `callback` {Function} + * `err` {Error} + * `fd` {integer} + +Synchronous file open using `options` signature-form. + ## `fs.read(fd, buffer, offset, length, position, callback)`