Skip to content

Commit

Permalink
perf: walk dir
Browse files Browse the repository at this point in the history
  • Loading branch information
develar committed Nov 8, 2016
1 parent 895411f commit 525bb91
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 31 deletions.
64 changes: 35 additions & 29 deletions src/asarUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,44 +20,50 @@ const MAX_FILE_REQUESTS = 8
const concurrency = {concurrency: MAX_FILE_REQUESTS}
const NODE_MODULES_PATTERN = path.sep + "node_modules" + path.sep

export async function walk(dirPath: string, consumer?: (file: string, stat: Stats) => void, filter?: Filter, addRootToResult?: boolean): Promise<Array<string>> {
const childNames = await readdir(dirPath)
const stats = await BluebirdPromise.map(childNames, name => lstat(dirPath + path.sep + name), concurrency)
const dirs: Array<string> = []
const files: Array<string> = addRootToResult ? [dirPath] : []
await BluebirdPromise.map(stats, (stat, index): any => {
const filePath = dirPath + path.sep + childNames[index]
if (filter != null && !filter(filePath, stat)) {
return null
}

if (consumer != null) {
consumer(filePath, stat)
}

if (stat.isDirectory()) {
dirs.push(filePath)
export async function walk(initialDirPath: string, consumer?: (file: string, stat: Stats) => void, filter?: Filter): Promise<Array<string>> {
const result: Array<string> = []
const queue: Array<string> = [initialDirPath]
let addDirToResult = false
while (queue.length > 0) {
const dirPath = queue.pop()!
if (addDirToResult) {
result.push(dirPath)
}
else {
files.push(filePath)
addDirToResult = true
}
}, concurrency)

files.sort()
const childNames = await readdir(dirPath)
childNames.sort()

if (dirs.length === 0) {
return files
}
const dirs: Array<string> = []
await BluebirdPromise.map(childNames, name => {
const filePath = dirPath + path.sep + name
return lstat(filePath)
.then(stat => {
if (filter != null && !filter(filePath, stat)) {
return
}

if (consumer != null) {
consumer(filePath, stat)
}

if (stat.isDirectory()) {
dirs.push(filePath)
}
else {
result.push(filePath)
}
})
}, concurrency)

dirs.sort()
const list = await BluebirdPromise.map(dirs, dir => walk(dir, consumer, filter, true), concurrency)
for (let subList of list) {
for (let file of subList) {
files.push(file)
for (let i = dirs.length - 1; i > -1; i--) {
queue.push(dirs[i])
}
}

return files
return result
}

export async function createAsarArchive(src: string, resourcesPath: string, options: AsarOptions, filter: Filter): Promise<any> {
Expand Down
4 changes: 2 additions & 2 deletions src/platformPackager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,12 +195,12 @@ export abstract class PlatformPackager<DC extends PlatformSpecificBuildOptions>
if (defaultMatcher.isEmpty()) {
defaultMatcher.addPattern("**/*")
}
defaultMatcher.addPattern("!**/node_modules/*/{CHANGELOG.md,README.md,README,readme.md,readme,test,__tests__,tests,powered-test,example,examples,.yarn-integrity}")
defaultMatcher.addPattern("!**/node_modules/*/{CHANGELOG.md,README.md,README,readme.md,readme,test,__tests__,tests,powered-test,example,examples}")
defaultMatcher.addPattern("!**/node_modules/.bin")
defaultMatcher.addPattern("!**/*.{o,hprof,orig,pyc,pyo,rbc,swp}")
defaultMatcher.addPattern("!**/._*")
//noinspection SpellCheckingInspection
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}")
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}")

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

0 comments on commit 525bb91

Please sign in to comment.