From e89934d955d5c6689833f0f86d53695ef65fa9f7 Mon Sep 17 00:00:00 2001 From: develar Date: Tue, 8 Nov 2016 09:00:40 +0100 Subject: [PATCH] perf: dev or extra deps --- src/platformPackager.ts | 15 ++++++++------- src/util/filter.ts | 41 ----------------------------------------- src/util/util.ts | 31 +++++++++++++++++++++++++++++++ test/src/globTest.ts | 2 +- 4 files changed, 40 insertions(+), 49 deletions(-) diff --git a/src/platformPackager.ts b/src/platformPackager.ts index ba5ded15ff7..d3a5386e462 100644 --- a/src/platformPackager.ts +++ b/src/platformPackager.ts @@ -3,7 +3,7 @@ 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" @@ -11,7 +11,7 @@ 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" @@ -184,9 +184,10 @@ export abstract class PlatformPackager 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) @@ -206,10 +207,10 @@ export abstract class PlatformPackager const deprecatedIgnore = (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) } diff --git a/src/util/filter.ts b/src/util/filter.ts index 0421788755e..2880dff6c4f 100644 --- a/src/util/filter.ts +++ b/src/util/filter.ts @@ -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 { @@ -55,45 +53,6 @@ export function createFilter(src: string, patterns: Array, ignoreFile } } -export function devDependencies(dir: string): Promise> { - 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): 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 { - 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, stat: Stats): boolean { let match = false diff --git a/src/util/util.ts b/src/util/util.ts index 2d5f4145f6d..293148a899d 100644 --- a/src/util/util.ts +++ b/src/util/util.ts @@ -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): Promise> { + 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, seen: Set, 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) + } + } +} \ No newline at end of file diff --git a/test/src/globTest.ts b/test/src/globTest.ts index 5b1efae2ed7..e5bbadd9e68 100644 --- a/test/src/globTest.ts +++ b/test/src/globTest.ts @@ -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) => {