File tree Expand file tree Collapse file tree 2 files changed +31
-0
lines changed Expand file tree Collapse file tree 2 files changed +31
-0
lines changed Original file line number Diff line number Diff line change
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
+ } ;
Original file line number Diff line number Diff line change @@ -3,3 +3,4 @@ export * from "./Config";
3
3
export * from "./getContent" ;
4
4
export * from "./listContent" ;
5
5
export * from "./getFile" ;
6
+ export * from "./copyDir" ;
You can’t perform that action at this time.
0 commit comments