Skip to content

Commit 8a25b5b

Browse files
committed
adding copyDir utility
1 parent 9102ffc commit 8a25b5b

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

src/copyDir.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { dirname, join, relative } from "path";
2+
import { Config } from "./Config";
3+
import { copyFile, mkdir, readdir, stat } from "fs/promises";
4+
5+
export const copyDir = async (sourceDir: string, targetDir: string) => {
6+
const rootDir = Config.getRootDir();
7+
const fullSourceDir = join(rootDir, sourceDir);
8+
const dirsToCopy = [fullSourceDir];
9+
while (dirsToCopy.length > 0) {
10+
const dirToCopy = dirsToCopy.shift();
11+
const files = await readdir(dirToCopy);
12+
await Promise.all(
13+
files.map(async file => {
14+
const fileOrDirPath = join(dirToCopy, file);
15+
const stats = await stat(fileOrDirPath);
16+
if (stats.isDirectory()) {
17+
dirsToCopy.push(fileOrDirPath);
18+
} else {
19+
const targetPath = join(
20+
targetDir,
21+
relative(fullSourceDir, fileOrDirPath)
22+
);
23+
const targetDirname = dirname(targetPath);
24+
await mkdir(targetDirname, { recursive: true });
25+
await copyFile(fileOrDirPath, targetPath);
26+
}
27+
})
28+
);
29+
}
30+
};

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ export * from "./Config";
33
export * from "./getContent";
44
export * from "./listContent";
55
export * from "./getFile";
6+
export * from "./copyDir";

0 commit comments

Comments
 (0)