From eec5b3261e61fb6aa9b07a04f8387e1a0371c9fe Mon Sep 17 00:00:00 2001 From: Matt McKegg Date: Sat, 5 Nov 2016 23:55:18 +1300 Subject: [PATCH] feat: add ability to specify additional npm rebuild args (#881) * feat: add ability to specify additional npm rebuild args --- docs/Options.md | 1 + src/metadata.ts | 5 +++++ src/packager.ts | 2 +- src/util/util.ts | 15 ++++++++++++--- 4 files changed, 19 insertions(+), 4 deletions(-) diff --git a/docs/Options.md b/docs/Options.md index 67d10d9ff07..ed7adcb13a5 100644 --- a/docs/Options.md +++ b/docs/Options.md @@ -84,6 +84,7 @@ Don't customize paths to background and icon, — just follow conventions. | afterPack | *programmatic API only* The function to be run after pack (but before pack into distributable format and sign). Promise must be returned. | npmRebuild | *two package.json structure only* Whether to [rebuild](https://docs.npmjs.com/cli/rebuild) native dependencies (`npm rebuild`) before starting to package the app. Defaults to `true`. | npmSkipBuildFromSource | *two package.json structure only* Whether to omit using [--build-from-source](https://github.com/mapbox/node-pre-gyp#options) flag when installing app native deps. Defaults to `false`. +| npmArgs | *two package.json structure only* Additional command line arguments to use when installing app native deps. Defaults to `null`. | nodeGypRebuild | Whether to execute `node-gyp rebuild` before starting to package the app. Defaults to `false`. | electronDist | The path to custom Electron build (e.g. `~/electron/out/R`). Only macOS supported, file issue if need for Linux or Windows. | publish | See [.build.publish](#PublishConfiguration). diff --git a/src/metadata.ts b/src/metadata.ts index 88db430449b..45d52366e1d 100755 --- a/src/metadata.ts +++ b/src/metadata.ts @@ -207,6 +207,11 @@ export interface BuildMetadata { */ readonly npmSkipBuildFromSource?: boolean + /* + *two package.json structure only* Additional command line arguments to use when installing app native deps. Defaults to `null`. + */ + readonly npmArgs?: Array | string | null + /* Whether to execute `node-gyp rebuild` before starting to package the app. Defaults to `false`. */ diff --git a/src/packager.ts b/src/packager.ts index a2297bd079f..bf015e34d0d 100644 --- a/src/packager.ts +++ b/src/packager.ts @@ -241,7 +241,7 @@ export class Packager implements BuildInfo { log("Skip app dependencies rebuild because platform is different") } else { - await installDependencies(this.appDir, this.electronVersion, Arch[arch], forceBuildFromSource, (await statOrNull(path.join(this.appDir, "node_modules"))) == null ? "install" : "rebuild") + await installDependencies(this.appDir, this.electronVersion, Arch[arch], forceBuildFromSource, (await statOrNull(path.join(this.appDir, "node_modules"))) == null ? "install" : "rebuild", this.devMetadata.build.npmArgs) } } } diff --git a/src/util/util.ts b/src/util/util.ts index 8473c452c10..21dfc219b80 100644 --- a/src/util/util.ts +++ b/src/util/util.ts @@ -16,8 +16,8 @@ export const debug7z: Debugger = _debug("electron-builder:7z") const DEFAULT_APP_DIR_NAMES = ["app", "www"] -export function installDependencies(appDir: string, electronVersion: string, arch: string = process.arch, forceBuildFromSource: boolean, command: string = "install"): Promise { - return task(`${(command === "install" ? "Installing" : "Rebuilding")} app dependencies for arch ${arch} to ${appDir}`, spawnNpmProduction(command, appDir, forceBuildFromSource, getGypEnv(electronVersion, arch))) +export function installDependencies(appDir: string, electronVersion: string, arch: string = process.arch, forceBuildFromSource: boolean, command: string = "install", additionalArgs?: any): Promise { + return task(`${(command === "install" ? "Installing" : "Rebuilding")} app dependencies for arch ${arch} to ${appDir}`, spawnNpmProduction(command, appDir, forceBuildFromSource, getGypEnv(electronVersion, arch), additionalArgs)) } export function getGypEnv(electronVersion: string, arch: string): any { @@ -32,7 +32,7 @@ export function getGypEnv(electronVersion: string, arch: string): any { }) } -export function spawnNpmProduction(command: string, appDir: string, forceBuildFromSource: boolean, env?: any): Promise { +export function spawnNpmProduction(command: string, appDir: string, forceBuildFromSource: boolean, env?: any, additionalArgs?: any): Promise { let npmExecPath = process.env.npm_execpath || process.env.NPM_CLI_JS const npmExecArgs = [command, "--production"] @@ -54,6 +54,15 @@ export function spawnNpmProduction(command: string, appDir: string, forceBuildFr npmExecArgs.push("--build-from-source") } + if (additionalArgs) { + if (Array.isArray(additionalArgs)) { + additionalArgs.push(...additionalArgs) + } + else { + additionalArgs.push(additionalArgs) + } + } + return spawn(npmExecPath, npmExecArgs, { cwd: appDir, env: env || process.env