-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Expand file tree
/
Copy pathcopy.js
More file actions
33 lines (28 loc) · 828 Bytes
/
copy.js
File metadata and controls
33 lines (28 loc) · 828 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import { cp, access } from "node:fs/promises";
import { join } from "node:path";
const copy = async () => {
const sourcePath = join(process.cwd(), "src", "fs", "files");
const destPath = join(process.cwd(), "src", "fs", "files_copy");
try {
// Check if files directory exists
await access(sourcePath);
// Check if files_copy directory exists
try {
await access(destPath);
throw new Error("FS operation failed");
} catch (error) {
if (error.code === "ENOENT") {
// do copy
await cp(sourcePath, destPath, { recursive: true });
} else {
throw new Error("FS operation failed");
}
}
} catch (error) {
if (error.message === "FS operation failed") {
throw error;
}
throw new Error("FS operation failed");
}
};
await copy();