From 525bb91b84a93db44ced7ff3eaed7d17e534994b Mon Sep 17 00:00:00 2001 From: develar Date: Tue, 8 Nov 2016 08:04:29 +0100 Subject: [PATCH] perf: walk dir --- src/asarUtil.ts | 64 ++++++++++++++++++++++------------------- src/platformPackager.ts | 4 +-- 2 files changed, 37 insertions(+), 31 deletions(-) diff --git a/src/asarUtil.ts b/src/asarUtil.ts index 0a27462e177..6c2b9d96d94 100644 --- a/src/asarUtil.ts +++ b/src/asarUtil.ts @@ -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> { - const childNames = await readdir(dirPath) - const stats = await BluebirdPromise.map(childNames, name => lstat(dirPath + path.sep + name), concurrency) - const dirs: Array = [] - const files: Array = 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> { + const result: Array = [] + const queue: Array = [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 = [] + 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 { diff --git a/src/platformPackager.ts b/src/platformPackager.ts index 600e96ebf73..ba5ded15ff7 100644 --- a/src/platformPackager.ts +++ b/src/platformPackager.ts @@ -195,12 +195,12 @@ export abstract class PlatformPackager 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 = (this.devMetadata.build).ignore