@@ -3,13 +3,56 @@ import * as path from "path"
33
44import { ViewsContainer , Views , Menus , Configuration , contributesSchema } from "./types.js"
55
6+ function copyDir ( srcDir : string , dstDir : string , count : number ) : number {
7+ const entries = fs . readdirSync ( srcDir , { withFileTypes : true } )
8+
9+ for ( const entry of entries ) {
10+ const srcPath = path . join ( srcDir , entry . name )
11+ const dstPath = path . join ( dstDir , entry . name )
12+
13+ if ( entry . isDirectory ( ) ) {
14+ fs . mkdirSync ( dstPath , { recursive : true } )
15+ count = copyDir ( srcPath , dstPath , count )
16+ } else {
17+ count = count + 1
18+ fs . copyFileSync ( srcPath , dstPath )
19+ }
20+ }
21+
22+ return count
23+ }
24+
25+ function rmDir ( dirPath : string , maxRetries : number = 3 ) : void {
26+ for ( let attempt = 1 ; attempt <= maxRetries ; attempt ++ ) {
27+ try {
28+ fs . rmSync ( dirPath , { recursive : true , force : true } )
29+ return
30+ } catch ( error ) {
31+ const isLastAttempt = attempt === maxRetries
32+ const isEnotemptyError = error instanceof Error && "code" in error && ( error . code === 'ENOTEMPTY' || error . code === 'EBUSY' )
33+
34+ if ( isLastAttempt || ! isEnotemptyError ) {
35+ throw error // Re-throw if it's the last attempt or not a locking error.
36+ }
37+
38+ // Wait with exponential backoff before retrying.
39+ const delay = Math . min ( 100 * Math . pow ( 2 , attempt - 1 ) , 1000 ) // Cap at 1s.
40+ console . warn ( `[rmDir] Attempt ${ attempt } failed for ${ dirPath } , retrying in ${ delay } ms...` )
41+
42+ // Synchronous sleep for simplicity in build scripts.
43+ const start = Date . now ( )
44+ while ( Date . now ( ) - start < delay ) { /* Busy wait */ }
45+ }
46+ }
47+ }
48+
649export function copyPaths ( copyPaths : [ string , string ] [ ] , srcDir : string , dstDir : string ) {
750 copyPaths . forEach ( ( [ srcRelPath , dstRelPath ] ) => {
851 const stats = fs . lstatSync ( path . join ( srcDir , srcRelPath ) )
952
1053 if ( stats . isDirectory ( ) ) {
1154 if ( fs . existsSync ( path . join ( dstDir , dstRelPath ) ) ) {
12- fs . rmSync ( path . join ( dstDir , dstRelPath ) , { recursive : true , force : true } )
55+ rmDir ( path . join ( dstDir , dstRelPath ) )
1356 }
1457
1558 fs . mkdirSync ( path . join ( dstDir , dstRelPath ) , { recursive : true } )
@@ -23,25 +66,6 @@ export function copyPaths(copyPaths: [string, string][], srcDir: string, dstDir:
2366 } )
2467}
2568
26- export function copyDir ( srcDir : string , dstDir : string , count : number ) : number {
27- const entries = fs . readdirSync ( srcDir , { withFileTypes : true } )
28-
29- for ( const entry of entries ) {
30- const srcPath = path . join ( srcDir , entry . name )
31- const dstPath = path . join ( dstDir , entry . name )
32-
33- if ( entry . isDirectory ( ) ) {
34- fs . mkdirSync ( dstPath , { recursive : true } )
35- count = copyDir ( srcPath , dstPath , count )
36- } else {
37- count = count + 1
38- fs . copyFileSync ( srcPath , dstPath )
39- }
40- }
41-
42- return count
43- }
44-
4569export function copyWasms ( srcDir : string , distDir : string ) : void {
4670 const nodeModulesDir = path . join ( srcDir , "node_modules" )
4771
0 commit comments