@@ -2106,6 +2106,47 @@ static void OpenFileHandle(const FunctionCallbackInfo<Value>& args) {
2106
2106
}
2107
2107
}
2108
2108
2109
+ // TODO(@anonrig): Implement v8 fast APi calls for `cpSync`.
2110
+ static void CpSync (const FunctionCallbackInfo<Value>& args) {
2111
+ Environment* env = Environment::GetCurrent (args);
2112
+ CHECK (args.Length () == 8 ); // src, dest, preserveTimestamps, dereference, errorOnExist, force, recursive, verbatimSymlinks
2113
+ BufferValue path (env->isolate (), args[0 ]);
2114
+ CHECK_NOT_NULL (*path);
2115
+ ToNamespacedPath (env, &path);
2116
+
2117
+ BufferValue dest (env->isolate (), args[1 ]);
2118
+ CHECK_NOT_NULL (*dest);
2119
+ ToNamespacedPath (env, &dest);
2120
+
2121
+ bool preserveTimestamps = args[2 ]->IsTrue ();
2122
+ bool dereference = args[3 ]->IsTrue ();
2123
+ bool errorOnExist = args[4 ]->IsTrue ();
2124
+ bool force = args[5 ]->IsTrue ();
2125
+ bool recursive = args[6 ]->IsTrue ();
2126
+ bool verbatimSymlinks = args[7 ]->IsTrue ();
2127
+
2128
+ if (errorOnExist) {
2129
+ if (std::filesystem::exists (*path)) {
2130
+ return env->ThrowUVException (UV_EEXIST, " EEXIST" , " File already exists" );
2131
+ }
2132
+ }
2133
+
2134
+ std::filesystem::copy_options options;
2135
+
2136
+ // When true timestamps from src will be preserved.
2137
+ if (preserveTimestamps) options |= std::filesystem::copy_options::create_hard_links;
2138
+ // Dereference symbolic links.
2139
+ if (dereference) options |= std::filesystem::copy_options::copy_symlinks;
2140
+ // Overwrite existing file or directory.
2141
+ if (force) options |= std::filesystem::copy_options::overwrite_existing;
2142
+ // Copy directories recursively.
2143
+ if (recursive) options |= std::filesystem::copy_options::recursive;
2144
+ // When true, path resolution for symlinks will be skipped.
2145
+ if (verbatimSymlinks) options |= std::filesystem::copy_options::skip_symlinks;
2146
+
2147
+ std::filesystem::copy (*path, *dest, options);
2148
+ }
2149
+
2109
2150
static void CopyFile (const FunctionCallbackInfo<Value>& args) {
2110
2151
Environment* env = Environment::GetCurrent (args);
2111
2152
Isolate* isolate = env->isolate ();
@@ -3344,6 +3385,7 @@ static void CreatePerIsolateProperties(IsolateData* isolate_data,
3344
3385
SetMethod (isolate, target, " writeFileUtf8" , WriteFileUtf8);
3345
3386
SetMethod (isolate, target, " realpath" , RealPath);
3346
3387
SetMethod (isolate, target, " copyFile" , CopyFile);
3388
+ SetMethod (isolate, target, " cpSync" , CpSync);
3347
3389
3348
3390
SetMethod (isolate, target, " chmod" , Chmod);
3349
3391
SetMethod (isolate, target, " fchmod" , FChmod);
@@ -3466,6 +3508,7 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
3466
3508
registry->Register (WriteFileUtf8);
3467
3509
registry->Register (RealPath);
3468
3510
registry->Register (CopyFile);
3511
+ registry->Register (CpSync);
3469
3512
3470
3513
registry->Register (Chmod);
3471
3514
registry->Register (FChmod);
0 commit comments