@@ -20,44 +20,50 @@ const MAX_FILE_REQUESTS = 8
20
20
const concurrency = { concurrency : MAX_FILE_REQUESTS }
21
21
const NODE_MODULES_PATTERN = path . sep + "node_modules" + path . sep
22
22
23
- export async function walk ( dirPath : string , consumer ?: ( file : string , stat : Stats ) => void , filter ?: Filter , addRootToResult ?: boolean ) : Promise < Array < string > > {
24
- const childNames = await readdir ( dirPath )
25
- const stats = await BluebirdPromise . map ( childNames , name => lstat ( dirPath + path . sep + name ) , concurrency )
26
- const dirs : Array < string > = [ ]
27
- const files : Array < string > = addRootToResult ? [ dirPath ] : [ ]
28
- await BluebirdPromise . map ( stats , ( stat , index ) : any => {
29
- const filePath = dirPath + path . sep + childNames [ index ]
30
- if ( filter != null && ! filter ( filePath , stat ) ) {
31
- return null
32
- }
33
-
34
- if ( consumer != null ) {
35
- consumer ( filePath , stat )
36
- }
37
-
38
- if ( stat . isDirectory ( ) ) {
39
- dirs . push ( filePath )
23
+ export async function walk ( initialDirPath : string , consumer ?: ( file : string , stat : Stats ) => void , filter ?: Filter ) : Promise < Array < string > > {
24
+ const result : Array < string > = [ ]
25
+ const queue : Array < string > = [ initialDirPath ]
26
+ let addDirToResult = false
27
+ while ( queue . length > 0 ) {
28
+ const dirPath = queue . pop ( ) !
29
+ if ( addDirToResult ) {
30
+ result . push ( dirPath )
40
31
}
41
32
else {
42
- files . push ( filePath )
33
+ addDirToResult = true
43
34
}
44
- } , concurrency )
45
35
46
- files . sort ( )
36
+ const childNames = await readdir ( dirPath )
37
+ childNames . sort ( )
47
38
48
- if ( dirs . length === 0 ) {
49
- return files
50
- }
39
+ const dirs : Array < string > = [ ]
40
+ await BluebirdPromise . map ( childNames , name => {
41
+ const filePath = dirPath + path . sep + name
42
+ return lstat ( filePath )
43
+ . then ( stat => {
44
+ if ( filter != null && ! filter ( filePath , stat ) ) {
45
+ return
46
+ }
47
+
48
+ if ( consumer != null ) {
49
+ consumer ( filePath , stat )
50
+ }
51
+
52
+ if ( stat . isDirectory ( ) ) {
53
+ dirs . push ( filePath )
54
+ }
55
+ else {
56
+ result . push ( filePath )
57
+ }
58
+ } )
59
+ } , concurrency )
51
60
52
- dirs . sort ( )
53
- const list = await BluebirdPromise . map ( dirs , dir => walk ( dir , consumer , filter , true ) , concurrency )
54
- for ( let subList of list ) {
55
- for ( let file of subList ) {
56
- files . push ( file )
61
+ for ( let i = dirs . length - 1 ; i > - 1 ; i -- ) {
62
+ queue . push ( dirs [ i ] )
57
63
}
58
64
}
59
65
60
- return files
66
+ return result
61
67
}
62
68
63
69
export async function createAsarArchive ( src : string , resourcesPath : string , options : AsarOptions , filter : Filter ) : Promise < any > {
0 commit comments