From 0a5d2a8195fbbaab7ff1f40ad54ba94726bcc104 Mon Sep 17 00:00:00 2001 From: dave caruso Date: Wed, 30 Aug 2023 18:30:06 -0700 Subject: feat(node:fs): add `cp`/`cpSync`/`promises.cp` + async `copyFile` (#4340) * half working disaster code * this * async copyFile * . * its failing symlink tests * asdfg * asdf * hmm * okay i think ti works * small edits * fix test on linux * i hate atomics / atomics hate me back <3 * add a message in the builtins bundler that 0.8 is needed. it breaks on older versions lol. * fixed * rebase --- src/js/node/fs.promises.ts | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) (limited to 'src/js/node/fs.promises.ts') diff --git a/src/js/node/fs.promises.ts b/src/js/node/fs.promises.ts index 406858f7f..dfc24cae7 100644 --- a/src/js/node/fs.promises.ts +++ b/src/js/node/fs.promises.ts @@ -89,11 +89,28 @@ function watch( }; } +let lazy_cp: any = null; +// attempt to use the native code version if possible +// and on MacOS, simple cases of recursive directory trees can be done in a single `clonefile()` +// using filter and other options uses a lazily loaded js fallback ported from node.js +function cp(src, dest, options) { + if (!options) return fs.cp(src, dest); + if (typeof options !== "object") { + throw new TypeError("options must be an object"); + } + if (options.dereference || options.filter || options.preserveTimestamps || options.verbatimSymlinks) { + if (!lazy_cp) lazy_cp = require("../internal/fs/cp-sync"); + return lazy_cp!(src, dest, options); + } + return fs.cp(src, dest, options.recursive, options.errorOnExist, options.force ?? true, options.mode); +} + export default { access: promisify(fs.accessSync), appendFile: promisify(fs.appendFileSync), close: promisify(fs.closeSync), - copyFile: promisify(fs.copyFileSync), + copyFile: fs.copyFile.bind(fs), + cp, exists: promisify(fs.existsSync), chown: promisify(fs.chownSync), chmod: promisify(fs.chmodSync), -- cgit v1.2.3