Skip to content

Commit 525bb91

Browse files
committed
perf: walk dir
1 parent 895411f commit 525bb91

File tree

2 files changed

+37
-31
lines changed

2 files changed

+37
-31
lines changed

src/asarUtil.ts

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -20,44 +20,50 @@ const MAX_FILE_REQUESTS = 8
2020
const concurrency = {concurrency: MAX_FILE_REQUESTS}
2121
const NODE_MODULES_PATTERN = path.sep + "node_modules" + path.sep
2222

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)
4031
}
4132
else {
42-
files.push(filePath)
33+
addDirToResult = true
4334
}
44-
}, concurrency)
4535

46-
files.sort()
36+
const childNames = await readdir(dirPath)
37+
childNames.sort()
4738

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)
5160

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])
5763
}
5864
}
5965

60-
return files
66+
return result
6167
}
6268

6369
export async function createAsarArchive(src: string, resourcesPath: string, options: AsarOptions, filter: Filter): Promise<any> {

src/platformPackager.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,12 +195,12 @@ export abstract class PlatformPackager<DC extends PlatformSpecificBuildOptions>
195195
if (defaultMatcher.isEmpty()) {
196196
defaultMatcher.addPattern("**/*")
197197
}
198-
defaultMatcher.addPattern("!**/node_modules/*/{CHANGELOG.md,README.md,README,readme.md,readme,test,__tests__,tests,powered-test,example,examples,.yarn-integrity}")
198+
defaultMatcher.addPattern("!**/node_modules/*/{CHANGELOG.md,README.md,README,readme.md,readme,test,__tests__,tests,powered-test,example,examples}")
199199
defaultMatcher.addPattern("!**/node_modules/.bin")
200200
defaultMatcher.addPattern("!**/*.{o,hprof,orig,pyc,pyo,rbc,swp}")
201201
defaultMatcher.addPattern("!**/._*")
202202
//noinspection SpellCheckingInspection
203-
defaultMatcher.addPattern("!**/{.DS_Store,.git,.hg,.svn,CVS,RCS,SCCS,__pycache__,thumbs.db,.gitignore,.gitattributes,.editorconfig,.flowconfig,.yarn-metadata.json,.idea,appveyor.yml,.travis.yml,circle.yml,npm-debug.log,.nyc_output,yarn.lock}")
203+
defaultMatcher.addPattern("!**/{.DS_Store,.git,.hg,.svn,CVS,RCS,SCCS,__pycache__,thumbs.db,.gitignore,.gitattributes,.editorconfig,.flowconfig,.yarn-metadata.json,.idea,appveyor.yml,.travis.yml,circle.yml,npm-debug.log,.nyc_output,yarn.lock,.yarn-integrity}")
204204

205205
let rawFilter: any = null
206206
const deprecatedIgnore = (<any>this.devMetadata.build).ignore

0 commit comments

Comments
 (0)