Skip to content

Commit

Permalink
perf: dev or extra deps
Browse files Browse the repository at this point in the history
  • Loading branch information
develar committed Nov 8, 2016
1 parent 525bb91 commit e89934d
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 49 deletions.
15 changes: 8 additions & 7 deletions src/platformPackager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import EventEmitter = NodeJS.EventEmitter
import BluebirdPromise from "bluebird-lst-c"
import * as path from "path"
import { readdir, remove } from "fs-extra-p"
import { statOrNull, use, unlinkIfExists, isEmptyOrSpaces, asArray } from "./util/util"
import { statOrNull, use, unlinkIfExists, isEmptyOrSpaces, asArray, dependencies, debug } from "./util/util"
import { Packager } from "./packager"
import { AsarOptions } from "asar-electron-builder"
import { archiveApp } from "./targets/archive"
import { Minimatch } from "minimatch"
import { checkFileInArchive, createAsarArchive } from "./asarUtil"
import { warn, log, task } from "./util/log"
import { AppInfo } from "./appInfo"
import { copyFiltered, devDependencies } from "./util/filter"
import { copyFiltered } from "./util/filter"
import { pack } from "./packager/dirPackager"
import { TmpDir } from "./util/tmp"
import { FileMatchOptions, FileMatcher, FilePattern, deprecatedUserIgnoreFilter } from "./fileMatcher"
Expand Down Expand Up @@ -184,9 +184,10 @@ export abstract class PlatformPackager<DC extends PlatformSpecificBuildOptions>
const p = pack(this, appOutDir, platformName, Arch[arch], this.info.electronVersion, async() => {
const ignoreFiles = new Set([path.resolve(this.info.appDir, outDir), path.resolve(this.info.appDir, this.buildResourcesDir)])
// prune dev or not listed dependencies
const result = await devDependencies(this.info.appDir)
for (let it of result) {
ignoreFiles.add(it)
await dependencies(this.info.appDir, true, ignoreFiles)

if (debug.enabled) {
debug(`Pruned dev or extraneous dependencies: ${Array.from(ignoreFiles).slice(2).join(", ")}`)
}

const patterns = this.getFileMatchers("files", this.info.appDir, path.join(resourcesPath, "app"), false, fileMatchOptions, platformSpecificBuildOptions)
Expand All @@ -206,10 +207,10 @@ export abstract class PlatformPackager<DC extends PlatformSpecificBuildOptions>
const deprecatedIgnore = (<any>this.devMetadata.build).ignore
if (deprecatedIgnore != null) {
if (typeof deprecatedIgnore === "function") {
log(`"ignore is specified as function, may be new "files" option will be suit your needs? Please see https://github.com/electron-userland/electron-builder/wiki/Options#BuildMetadata-files`)
warn(`"ignore" is specified as function, may be new "files" option will be suit your needs? Please see https://github.com/electron-userland/electron-builder/wiki/Options#BuildMetadata-files`)
}
else {
warn(`"ignore is deprecated, please use "files", see https://github.com/electron-userland/electron-builder/wiki/Options#BuildMetadata-files`)
warn(`"ignore" is deprecated, please use "files", see https://github.com/electron-userland/electron-builder/wiki/Options#BuildMetadata-files`)
}
rawFilter = deprecatedUserIgnoreFilter(deprecatedIgnore, this.info.appDir)
}
Expand Down
41 changes: 0 additions & 41 deletions src/util/filter.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { copy, Stats } from "fs-extra-p"
import { Minimatch } from "minimatch"
import * as path from "path"
import BluebirdPromise from "bluebird-lst-c"
const readInstalled = require("read-installed")

// we use relative path to avoid canonical path issue - e.g. /tmp vs /private/tmp
export function copyFiltered(src: string, destination: string, filter: Filter, dereference: boolean): Promise<any> {
Expand Down Expand Up @@ -55,45 +53,6 @@ export function createFilter(src: string, patterns: Array<Minimatch>, ignoreFile
}
}

export function devDependencies(dir: string): Promise<Array<string>> {
return new BluebirdPromise((resolve, reject) => {
readInstalled(dir, (error: Error, data: any) => {
if (error) {
reject(error)
}
else {
resolve(flatDependencies(data, new Set()))
}
})
})
}

function flatDependencies(data: any, seen: Set<string>): any {
const deps = data.dependencies
if (deps == null) {
return []
}

return Object.keys(deps).map(function (d) {
if (typeof deps[d] !== "object" || seen.has(deps[d])) {
return null
}

seen.add(deps[d])
if (deps[d].extraneous) {
const extra = deps[d]
delete deps[d]
return extra.path
}
return flatDependencies(deps[d], seen)
})
.filter(it => it !== null)
.reduce(function flat(l, r): Array<string> {
return l.concat(Array.isArray(r) ? r.reduce(flat, []) : r)
}, [])

}

// https://github.com/joshwnj/minimatch-all/blob/master/index.js
function minimatchAll(path: string, patterns: Array<Minimatch>, stat: Stats): boolean {
let match = false
Expand Down
31 changes: 31 additions & 0 deletions src/util/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,3 +325,34 @@ export function getCacheDirectory(): string {
return path.join(homedir(), ".cache", "electron-builder")
}
}

let readInstalled: any = null
export function dependencies(dir: string, extraneousOnly: boolean, result: Set<string>): Promise<Array<string>> {
if (readInstalled == null) {
readInstalled = BluebirdPromise.promisify(require("read-installed"))
}
return readInstalled(dir)
.then((it: any) => flatDependencies(it, result, new Set(), extraneousOnly))
}

function flatDependencies(data: any, result: Set<string>, seen: Set<string>, extraneousOnly: boolean): void {
const deps = data.dependencies
if (deps == null) {
return
}

for (let d of Object.keys(deps)) {
const dep = deps[d]
if (typeof dep !== "object" || (!extraneousOnly && dep.extraneous) || seen.has(dep)) {
continue
}

if (extraneousOnly === dep.extraneous) {
seen.add(dep)
result.add(dep.path)
}
else {
flatDependencies(dep, result, seen, extraneousOnly)
}
}
}
2 changes: 1 addition & 1 deletion test/src/globTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ test.ifNotWindows("link", app({
}))

// skip on MacOS because we want test only / and \
test.ifNotCiOsx("ignore node_modules known dev dep", () => {
test.ifNotCiOsx("ignore node_modules dev dep", () => {
const build: any = {
asar: false,
ignore: (file: string) => {
Expand Down

0 comments on commit e89934d

Please sign in to comment.