@@ -29,7 +29,9 @@ function rmDir(dirPath: string, maxRetries: number = 3): void {
2929 return
3030 } catch ( error ) {
3131 const isLastAttempt = attempt === maxRetries
32- const isEnotemptyError = error instanceof Error && "code" in error && ( error . code === 'ENOTEMPTY' || error . code === 'EBUSY' )
32+
33+ const isEnotemptyError =
34+ error instanceof Error && "code" in error && ( error . code === "ENOTEMPTY" || error . code === "EBUSY" )
3335
3436 if ( isLastAttempt || ! isEnotemptyError ) {
3537 throw error // Re-throw if it's the last attempt or not a locking error.
@@ -41,27 +43,42 @@ function rmDir(dirPath: string, maxRetries: number = 3): void {
4143
4244 // Synchronous sleep for simplicity in build scripts.
4345 const start = Date . now ( )
44- while ( Date . now ( ) - start < delay ) { /* Busy wait */ }
46+
47+ while ( Date . now ( ) - start < delay ) {
48+ /* Busy wait */
49+ }
4550 }
4651 }
4752}
4853
49- export function copyPaths ( copyPaths : [ string , string ] [ ] , srcDir : string , dstDir : string ) {
50- copyPaths . forEach ( ( [ srcRelPath , dstRelPath ] ) => {
51- const stats = fs . lstatSync ( path . join ( srcDir , srcRelPath ) )
54+ type CopyPathOptions = {
55+ optional ?: boolean
56+ }
5257
53- if ( stats . isDirectory ( ) ) {
54- if ( fs . existsSync ( path . join ( dstDir , dstRelPath ) ) ) {
55- rmDir ( path . join ( dstDir , dstRelPath ) )
56- }
58+ export function copyPaths ( copyPaths : [ string , string , CopyPathOptions ? ] [ ] , srcDir : string , dstDir : string ) {
59+ copyPaths . forEach ( ( [ srcRelPath , dstRelPath , options = { } ] ) => {
60+ try {
61+ const stats = fs . lstatSync ( path . join ( srcDir , srcRelPath ) )
5762
58- fs . mkdirSync ( path . join ( dstDir , dstRelPath ) , { recursive : true } )
63+ if ( stats . isDirectory ( ) ) {
64+ if ( fs . existsSync ( path . join ( dstDir , dstRelPath ) ) ) {
65+ rmDir ( path . join ( dstDir , dstRelPath ) )
66+ }
5967
60- const count = copyDir ( path . join ( srcDir , srcRelPath ) , path . join ( dstDir , dstRelPath ) , 0 )
61- console . log ( `[copyPaths] Copied ${ count } files from ${ srcRelPath } to ${ dstRelPath } ` )
62- } else {
63- fs . copyFileSync ( path . join ( srcDir , srcRelPath ) , path . join ( dstDir , dstRelPath ) )
64- console . log ( `[copyPaths] Copied ${ srcRelPath } to ${ dstRelPath } ` )
68+ fs . mkdirSync ( path . join ( dstDir , dstRelPath ) , { recursive : true } )
69+
70+ const count = copyDir ( path . join ( srcDir , srcRelPath ) , path . join ( dstDir , dstRelPath ) , 0 )
71+ console . log ( `[copyPaths] Copied ${ count } files from ${ srcRelPath } to ${ dstRelPath } ` )
72+ } else {
73+ fs . copyFileSync ( path . join ( srcDir , srcRelPath ) , path . join ( dstDir , dstRelPath ) )
74+ console . log ( `[copyPaths] Copied ${ srcRelPath } to ${ dstRelPath } ` )
75+ }
76+ } catch ( error ) {
77+ if ( options . optional ) {
78+ console . warn ( `[copyPaths] Optional file not found: ${ srcRelPath } ` )
79+ } else {
80+ throw error
81+ }
6582 }
6683 } )
6784}
0 commit comments