Skip to content

Commit 20026f1

Browse files
committed
feat: more performant and reliable npm rebuild
1 parent 8a8ccad commit 20026f1

File tree

4 files changed

+43
-35
lines changed

4 files changed

+43
-35
lines changed

src/yarn.ts

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export async function installOrRebuild(options: BuildMetadata, appDir: string, e
1515
}
1616
}
1717

18-
export function getGypEnv(electronVersion: string, arch: string): any {
18+
export function getGypEnv(electronVersion: string, arch: string) {
1919
const gypHome = path.join(homedir(), ".electron-gyp")
2020
return Object.assign({}, process.env, {
2121
npm_config_disturl: "https://atom.io/download/electron",
@@ -37,23 +37,23 @@ function computeExtraArgs(options: BuildMetadata) {
3737

3838
export function installDependencies(appDir: string, electronVersion: string, arch: string = process.arch, additionalArgs: Array<string>): Promise<any> {
3939
log(`Installing app dependencies for arch ${arch} to ${appDir}`)
40-
let npmExecPath = process.env.npm_execpath || process.env.NPM_CLI_JS
40+
let execPath = process.env.npm_execpath || process.env.NPM_CLI_JS
4141
const npmExecArgs = ["install", "--production"]
4242

43-
const isYarn = npmExecPath != null && npmExecPath.includes("yarn")
43+
const isYarn = isYarnPath(execPath)
4444
if (!isYarn) {
4545
if (process.env.NPM_NO_BIN_LINKS === "true") {
4646
npmExecArgs.push("--no-bin-links")
4747
}
4848
npmExecArgs.push("--cache-min", "999999999")
4949
}
5050

51-
if (npmExecPath == null) {
52-
npmExecPath = getPackageToolPath()
51+
if (execPath == null) {
52+
execPath = getPackageToolPath()
5353
}
5454
else {
55-
npmExecArgs.unshift(npmExecPath)
56-
npmExecPath = process.env.npm_node_execpath || process.env.NODE_EXE || "node"
55+
npmExecArgs.unshift(execPath)
56+
execPath = process.env.npm_node_execpath || process.env.NODE_EXE || "node"
5757
}
5858

5959
for (let a of additionalArgs) {
@@ -62,7 +62,7 @@ export function installDependencies(appDir: string, electronVersion: string, arc
6262
}
6363
}
6464

65-
return spawn(npmExecPath, npmExecArgs, {
65+
return spawn(execPath, npmExecArgs, {
6666
cwd: appDir,
6767
env: getGypEnv(electronVersion, arch),
6868
})
@@ -115,6 +115,10 @@ function getPackageToolPath() {
115115
}
116116
}
117117

118+
function isYarnPath(execPath: string | null) {
119+
return execPath != null && path.basename(execPath).startsWith("yarn")
120+
}
121+
118122
export async function rebuild(appDir: string, electronVersion: string, arch: string = process.arch, additionalArgs: Array<string>) {
119123
const deps = new Set<string>()
120124
await dependencies(appDir, false, deps)
@@ -127,27 +131,28 @@ export async function rebuild(appDir: string, electronVersion: string, arch: str
127131
log(`Rebuilding native production dependencies for arch ${arch}`)
128132

129133
let execPath = process.env.npm_execpath || process.env.NPM_CLI_JS
130-
const execArgs = ["run", "install", "--"]
131-
134+
const execArgs: Array<string> = []
132135
if (execPath == null) {
133136
execPath = getPackageToolPath()
134137
}
135138
else {
136-
execArgs.unshift(execPath)
139+
execArgs.push(execPath)
137140
execPath = process.env.npm_node_execpath || process.env.NODE_EXE || "node"
138141
}
139142

140-
const gypHome = path.join(homedir(), ".electron-gyp")
141-
const env = Object.assign({}, process.env, {
142-
HOME: gypHome,
143-
USERPROFILE: gypHome,
144-
})
145-
146-
execArgs.push("--disturl=https://atom.io/download/electron")
147-
execArgs.push(`--target=${electronVersion}`)
148-
execArgs.push("--runtime=electron")
149-
execArgs.push(`--arch=${arch}`)
150-
execArgs.push(...additionalArgs)
151-
152-
await BluebirdPromise.each(nativeDeps, it => spawn(execPath, execArgs, {cwd: it, env: env}))
143+
const env = getGypEnv(electronVersion, arch)
144+
if (isYarnPath(execPath)) {
145+
execArgs.push("run", "install", "--")
146+
execArgs.push("--disturl=https://atom.io/download/electron")
147+
execArgs.push(`--target=${electronVersion}`)
148+
execArgs.push("--runtime=electron")
149+
execArgs.push(`--arch=${arch}`)
150+
execArgs.push(...additionalArgs)
151+
await BluebirdPromise.each(nativeDeps, it => spawn(execPath, execArgs, {cwd: it, env: env}))
152+
}
153+
else {
154+
execArgs.push("rebuild")
155+
execArgs.push(...nativeDeps.map(it => path.basename(it)))
156+
await spawn(execPath, execArgs, {cwd: appDir, env: env})
157+
}
153158
}

test/src/globTest.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ test("extraResources - one-package", async () => {
253253
//noinspection SpellCheckingInspection
254254
await assertPack("test-app-one", {
255255
// to check NuGet package
256-
targets: platform.createTarget(platform === Platform.WINDOWS ? null : DIR_TARGET),
256+
targets: platform.createTarget(platform === Platform.WINDOWS ? "squirrel" : DIR_TARGET),
257257
devMetadata: {
258258
build: {
259259
asar: true,

test/src/helpers/packTester.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ export async function assertPack(fixtureName: string, packagerOptions: PackagerO
9292
if (projectDirCreated != null) {
9393
await projectDirCreated(projectDir)
9494
if (checkOptions.npmInstallBefore) {
95-
await spawn(process.platform === "win32" ? "yarn.cmd" : "yarn", ["install", "--production"], {
95+
await spawn(process.platform === "win32" ? "npm.cmd" : "npm", ["install", "--production", "--cache-min", "999999999", "--no-bin-links"], {
9696
cwd: projectDir
9797
})
9898
}

yarn.lock

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
22
# yarn lockfile v1
3-
4-
53
"@develar/semantic-release@^6.3.21":
64
version "6.3.23"
75
resolved "https://registry.yarnpkg.com/@develar/semantic-release/-/semantic-release-6.3.23.tgz#80c8ecd7369cf4a5c442d59d24bd614c051dd004"
@@ -2447,8 +2445,8 @@ js-tokens@^2.0.0:
24472445
resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-2.0.0.tgz#79903f5563ee778cc1162e6dcf1a0027c97f9cb5"
24482446

24492447
js-yaml@^3.4.2, js-yaml@^3.6.1:
2450-
version "3.6.1"
2451-
resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-3.6.1.tgz#6e5fe67d8b205ce4d22fad05b7781e8dadcc4b30"
2448+
version "3.7.0"
2449+
resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-3.7.0.tgz#5c967ddd837a9bfdca5f2de84253abe8a1c03b80"
24522450
dependencies:
24532451
argparse "^1.0.7"
24542452
esprima "^2.6.0"
@@ -2820,7 +2818,11 @@ minimist@^1.1.0, minimist@^1.1.3, minimist@^1.2.0:
28202818
version "1.2.0"
28212819
resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.0.tgz#a35008b20f41383eec1fb914f4cd5df79a264284"
28222820

2823-
minimist@~0.0.1, minimist@0.0.8:
2821+
minimist@~0.0.1:
2822+
version "0.0.10"
2823+
resolved "https://registry.yarnpkg.com/minimist/-/minimist-0.0.10.tgz#de3f98543dbf96082be48ad1a0c7cda836301dcf"
2824+
2825+
minimist@0.0.8:
28242826
version "0.0.8"
28252827
resolved "https://registry.yarnpkg.com/minimist/-/minimist-0.0.8.tgz#857fcabfc3397d2625b8228262e86aa7a011b05d"
28262828

@@ -3453,8 +3455,8 @@ redent@^1.0.0:
34533455
strip-indent "^1.0.1"
34543456

34553457
regenerate@^1.2.1:
3456-
version "1.3.1"
3457-
resolved "https://registry.yarnpkg.com/regenerate/-/regenerate-1.3.1.tgz#0300203a5d2fdcf89116dce84275d011f5903f33"
3458+
version "1.3.2"
3459+
resolved "https://registry.yarnpkg.com/regenerate/-/regenerate-1.3.2.tgz#d1941c67bad437e1be76433add5b385f95b19260"
34583460

34593461
regenerator-runtime@^0.9.5:
34603462
version "0.9.6"
@@ -4111,8 +4113,8 @@ which-module@^1.0.0:
41114113
resolved "https://registry.yarnpkg.com/which-module/-/which-module-1.0.0.tgz#bba63ca861948994ff307736089e3b96026c2a4f"
41124114

41134115
which@^1.2.9:
4114-
version "1.2.11"
4115-
resolved "https://registry.yarnpkg.com/which/-/which-1.2.11.tgz#c8b2eeea6b8c1659fa7c1dd4fdaabe9533dc5e8b"
4116+
version "1.2.12"
4117+
resolved "https://registry.yarnpkg.com/which/-/which-1.2.12.tgz#de67b5e450269f194909ef23ece4ebe416fa1192"
41164118
dependencies:
41174119
isexe "^1.1.1"
41184120

@@ -4288,3 +4290,4 @@ zip-stream@^1.1.0:
42884290
compress-commons "^1.1.0"
42894291
lodash "^4.8.0"
42904292
readable-stream "^2.0.0"
4293+

0 commit comments

Comments
 (0)