Skip to content

Commit

Permalink
feat: add ability to specify additional npm rebuild args (#881)
Browse files Browse the repository at this point in the history
* feat: add ability to specify additional npm rebuild args
  • Loading branch information
mmckegg authored and develar committed Nov 5, 2016
1 parent 3bc99e1 commit eec5b32
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
1 change: 1 addition & 0 deletions docs/Options.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ Don't customize paths to background and icon, — just follow conventions.
| afterPack | <a name="BuildMetadata-afterPack"></a>*programmatic API only* The function to be run after pack (but before pack into distributable format and sign). Promise must be returned.
| npmRebuild | <a name="BuildMetadata-npmRebuild"></a>*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 | <a name="BuildMetadata-npmSkipBuildFromSource"></a>*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 | <a name="BuildMetadata-npmArgs"></a>*two package.json structure only* Additional command line arguments to use when installing app native deps. Defaults to `null`.
| nodeGypRebuild | <a name="BuildMetadata-nodeGypRebuild"></a>Whether to execute `node-gyp rebuild` before starting to package the app. Defaults to `false`.
| electronDist | <a name="BuildMetadata-electronDist"></a>The path to custom Electron build (e.g. `~/electron/out/R`). Only macOS supported, file issue if need for Linux or Windows.
| publish | <a name="BuildMetadata-publish"></a>See [.build.publish](#PublishConfiguration).
Expand Down
5 changes: 5 additions & 0 deletions src/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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> | string | null

/*
Whether to execute `node-gyp rebuild` before starting to package the app. Defaults to `false`.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/packager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Expand Down
15 changes: 12 additions & 3 deletions src/util/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any> {
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<any> {
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 {
Expand All @@ -32,7 +32,7 @@ export function getGypEnv(electronVersion: string, arch: string): any {
})
}

export function spawnNpmProduction(command: string, appDir: string, forceBuildFromSource: boolean, env?: any): Promise<any> {
export function spawnNpmProduction(command: string, appDir: string, forceBuildFromSource: boolean, env?: any, additionalArgs?: any): Promise<any> {
let npmExecPath = process.env.npm_execpath || process.env.NPM_CLI_JS
const npmExecArgs = [command, "--production"]

Expand All @@ -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
Expand Down

0 comments on commit eec5b32

Please sign in to comment.