Skip to content

Commit 9249fcd

Browse files
committed
feat: Set platform npm environment variable
Closes #1027
1 parent 569dd8b commit 9249fcd

File tree

6 files changed

+49
-35
lines changed

6 files changed

+49
-35
lines changed

src/cli/install-app-deps.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,15 @@ import { installOrRebuild } from "../yarn"
1010

1111
async function main() {
1212
const args: any = yargs
13+
.option("platform", {
14+
choices: ["linux", "darwin", "win32"],
15+
default: process.platform,
16+
})
1317
.option("arch", {
1418
choices: ["ia32", "x64", "all"],
15-
}).argv
19+
default: process.arch,
20+
})
21+
.argv
1622

1723
const projectDir = process.cwd()
1824
const devPackageFile = path.join(projectDir, "package.json")
@@ -24,7 +30,7 @@ async function main() {
2430
])
2531

2632
// if two package.json — force full install (user wants to install/update app deps in addition to dev)
27-
await installOrRebuild(devMetadata.build, results[0], results[1], args.arch, results[0] !== projectDir)
33+
await installOrRebuild(devMetadata.build, results[0], results[1], args.platform, args.arch, results[0] !== projectDir)
2834
}
2935

3036
main()

src/cli/node-gyp-rebuild.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,22 @@ import { log } from "../util/log"
88
import { getGypEnv } from "../yarn"
99

1010
const args: any = yargs
11+
.option("platform", {
12+
choices: ["linux", "darwin", "win32"],
13+
default: process.platform,
14+
})
1115
.option("arch", {
1216
choices: ["ia32", "x64", "armv7l"],
17+
default: process.arch,
1318
}).argv
1419

1520
const projectDir = process.cwd()
1621
const devPackageFile = path.join(projectDir, "package.json")
1722

1823
async function main() {
19-
const arch = args.arch || process.arch
20-
log(`Execute node-gyp rebuild for arch ${arch}`)
24+
log(`Execute node-gyp rebuild for ${args.platform}:${args.arch}`)
2125
await exec(process.platform === "win32" ? "node-gyp.cmd" : "node-gyp", ["rebuild"], {
22-
env: getGypEnv(await getElectronVersion(await readPackageJson(devPackageFile), devPackageFile), arch, true),
26+
env: getGypEnv(await getElectronVersion(await readPackageJson(devPackageFile), devPackageFile), args.platform, args.arch, true),
2327
})
2428
}
2529

src/packager.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ export class Packager implements BuildInfo {
245245
if (options.nodeGypRebuild === true) {
246246
log(`Executing node-gyp rebuild for arch ${Arch[arch]}`)
247247
await exec(process.platform === "win32" ? "node-gyp.cmd" : "node-gyp", ["rebuild"], {
248-
env: getGypEnv(this.electronVersion, Arch[arch], true),
248+
env: getGypEnv(this.electronVersion, platform.nodeName, Arch[arch], true),
249249
})
250250
}
251251

@@ -258,7 +258,7 @@ export class Packager implements BuildInfo {
258258
log("Skip app dependencies rebuild because platform is different")
259259
}
260260
else {
261-
await installOrRebuild(options, this.appDir, this.electronVersion, Arch[arch])
261+
await installOrRebuild(options, this.appDir, this.electronVersion, platform.nodeName, Arch[arch])
262262
}
263263
}
264264
}

src/yarn.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,32 @@ import { spawn, asArray } from "./util/util"
66
import { BuildMetadata } from "./metadata"
77
import { exists } from "./util/fs"
88

9-
export async function installOrRebuild(options: BuildMetadata, appDir: string, electronVersion: string, arch: string, forceInstall: boolean = false) {
9+
export async function installOrRebuild(options: BuildMetadata, appDir: string, electronVersion: string, platform: string, arch: string, forceInstall: boolean = false) {
1010
const args = asArray(options.npmArgs)
1111
if (forceInstall || !(await exists(path.join(appDir, "node_modules")))) {
12-
await installDependencies(appDir, electronVersion, arch, args, !options.npmSkipBuildFromSource)
12+
await installDependencies(appDir, electronVersion, platform, arch, args, !options.npmSkipBuildFromSource)
1313
}
1414
else {
15-
await rebuild(appDir, electronVersion, arch, args, !options.npmSkipBuildFromSource)
15+
await rebuild(appDir, electronVersion, arch, platform, args, !options.npmSkipBuildFromSource)
1616
}
1717
}
1818

19-
export function getGypEnv(electronVersion: string, arch: string, buildFromSource: boolean) {
19+
export function getGypEnv(electronVersion: string, platform: string, arch: string, buildFromSource: boolean) {
2020
const gypHome = path.join(homedir(), ".electron-gyp")
2121
return Object.assign({}, process.env, {
2222
npm_config_disturl: "https://atom.io/download/electron",
2323
npm_config_target: electronVersion,
2424
npm_config_runtime: "electron",
2525
npm_config_arch: arch,
2626
npm_config_target_arch: arch,
27+
npm_config_platform: platform,
2728
npm_config_build_from_source: buildFromSource,
2829
HOME: gypHome,
2930
USERPROFILE: gypHome,
3031
})
3132
}
3233

33-
export function installDependencies(appDir: string, electronVersion: string, arch: string = process.arch, additionalArgs: Array<string>, buildFromSource: boolean): Promise<any> {
34+
function installDependencies(appDir: string, electronVersion: string, platform: string = process.platform, arch: string = process.arch, additionalArgs: Array<string>, buildFromSource: boolean): Promise<any> {
3435
log(`Installing app dependencies for arch ${arch} to ${appDir}`)
3536
let execPath = process.env.npm_execpath || process.env.NPM_CLI_JS
3637
const execArgs = ["install", "--production"]
@@ -54,7 +55,7 @@ export function installDependencies(appDir: string, electronVersion: string, arc
5455
execArgs.push(...additionalArgs)
5556
return spawn(execPath, execArgs, {
5657
cwd: appDir,
57-
env: getGypEnv(electronVersion, arch, buildFromSource),
58+
env: getGypEnv(electronVersion, arch, platform, buildFromSource),
5859
})
5960
}
6061

@@ -109,16 +110,17 @@ function isYarnPath(execPath: string | null) {
109110
return execPath != null && path.basename(execPath).startsWith("yarn")
110111
}
111112

112-
export async function rebuild(appDir: string, electronVersion: string, arch: string = process.arch, additionalArgs: Array<string>, buildFromSource: boolean) {
113+
export async function rebuild(appDir: string, electronVersion: string, arch: string = process.arch, platform: string = process.platform, additionalArgs: Array<string>, buildFromSource: boolean) {
113114
const deps = new Set<string>()
114115
await dependencies(appDir, false, deps)
115116
const nativeDeps = await BluebirdPromise.filter(deps, it => exists(path.join(it, "binding.gyp")), {concurrency: 8})
116117

117118
if (nativeDeps.length === 0) {
119+
log(`No native production dependencies`)
118120
return
119121
}
120122

121-
log(`Rebuilding native production dependencies for arch ${arch}`)
123+
log(`Rebuilding native production dependencies for ${platform}:${arch}`)
122124

123125
let execPath = process.env.npm_execpath || process.env.NPM_CLI_JS
124126
const isYarn = isYarnPath(execPath)
@@ -131,7 +133,7 @@ export async function rebuild(appDir: string, electronVersion: string, arch: str
131133
execPath = process.env.npm_node_execpath || process.env.NODE_EXE || "node"
132134
}
133135

134-
const env = getGypEnv(electronVersion, arch, buildFromSource)
136+
const env = getGypEnv(electronVersion, arch, platform, buildFromSource)
135137
if (isYarn) {
136138
execArgs.push("run", "install", "--")
137139
execArgs.push(...additionalArgs)

test/yarn.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33610,7 +33610,7 @@ function isDate (d) {
3361033610
module.exports = {
3361133611
"name": "yarn",
3361233612
"installationMethod": "unknown",
33613-
"version": "0.18.0",
33613+
"version": "0.18.1",
3361433614
"license": "BSD-2-Clause",
3361533615
"preferGlobal": true,
3361633616
"dependencies": {
@@ -68041,7 +68041,7 @@ let historyCounter = 0;
6804168041

6804268042
class HoistManifest {
6804368043
constructor(key, parts, pkg, loc, isIgnored) {
68044-
this.ignore = isIgnored;
68044+
this.isIgnored = isIgnored;
6804568045
this.loc = loc;
6804668046
this.pkg = pkg;
6804768047

@@ -68155,13 +68155,17 @@ class PackageHoister {
6815568155

6815668156
//
6815768157
let parentParts = [];
68158-
let isIgnored = ref.ignore;
68158+
let isIgnored = () => ref.ignore;
6815968159

6816068160
if (parent) {
6816168161
if (!this.tree.get(parent.key)) {
6816268162
return null;
6816368163
}
68164-
isIgnored = isIgnored || parent.ignore;
68164+
// non ignored dependencies inherit parent's ignored status
68165+
// parent may transition from ignored to non ignored when hoisted if it is used in another non ignored branch
68166+
if (!isIgnored() && parent.isIgnored()) {
68167+
isIgnored = () => !!parent && parent.isIgnored();
68168+
}
6816568169
parentParts = parent.parts;
6816668170
}
6816768171

@@ -68203,9 +68207,9 @@ class PackageHoister {
6820368207
const existing = this.tree.get(checkKey);
6820468208
if (existing) {
6820568209
if (existing.loc === info.loc) {
68206-
// deduping an unignored reference to an ignored one
68207-
if (existing.ignore && !info.ignore) {
68208-
existing.ignore = false;
68210+
// switch to non ignored if earlier deduped version was ignored
68211+
if (existing.isIgnored() && !info.isIgnored()) {
68212+
existing.isIgnored = info.isIgnored;
6820968213
}
6821068214

6821168215
existing.addHistory(`Deduped ${ fullKey } to this item`);
@@ -68301,8 +68305,6 @@ class PackageHoister {
6830168305

6830268306
this.tree.delete(key);
6830368307

68304-
//
68305-
6830668308
var _getNewParts = this.getNewParts(key, info, rawParts.slice());
6830768309

6830868310
const parts = _getNewParts.parts;
@@ -68423,7 +68425,7 @@ class PackageHoister {
6842368425
const ref = info.pkg._reference;
6842468426
invariant(ref, 'expected reference');
6842568427

68426-
if (info.ignore) {
68428+
if (info.isIgnored()) {
6842768429
info.addHistory('Deleted as this module was ignored');
6842868430
} else {
6842968431
visibleFlatTree.push([loc, info]);

yarn.lock

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -786,12 +786,12 @@ concat-map@0.0.1:
786786
resolved "https://registry.yarnpkg.com/concat-map/-/concat-map-0.0.1.tgz#d8a96bd77fd68df7793a73036a3ba0d5405d477b"
787787

788788
concat-stream@^1.5.2:
789-
version "1.5.2"
790-
resolved "https://registry.yarnpkg.com/concat-stream/-/concat-stream-1.5.2.tgz#708978624d856af41a5a741defdd261da752c266"
789+
version "1.6.0"
790+
resolved "https://registry.yarnpkg.com/concat-stream/-/concat-stream-1.6.0.tgz#0aac662fd52be78964d5532f694784e70110acf7"
791791
dependencies:
792-
inherits "~2.0.1"
793-
readable-stream "~2.0.0"
794-
typedarray "~0.0.5"
792+
inherits "^2.0.3"
793+
readable-stream "^2.2.2"
794+
typedarray "^0.0.6"
795795

796796
configstore@^2.0.0:
797797
version "2.1.0"
@@ -1657,7 +1657,7 @@ inflight@^1.0.4:
16571657
once "^1.3.0"
16581658
wrappy "1"
16591659

1660-
inherits@2, inherits@~2.0.1:
1660+
inherits@2, inherits@^2.0.3, inherits@~2.0.1:
16611661
version "2.0.3"
16621662
resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.3.tgz#633c2c83e3da42a502f52466022480f4208261de"
16631663

@@ -2960,7 +2960,7 @@ readable-stream@^1.1.8, readable-stream@~1.1.9:
29602960
isarray "0.0.1"
29612961
string_decoder "~0.10.x"
29622962

2963-
readable-stream@^2.0.0, "readable-stream@^2.0.0 || ^1.1.13", readable-stream@^2.0.2, readable-stream@^2.0.5, readable-stream@^2.1.5:
2963+
readable-stream@^2.0.0, "readable-stream@^2.0.0 || ^1.1.13", readable-stream@^2.0.2, readable-stream@^2.0.5, readable-stream@^2.1.5, readable-stream@^2.2.2:
29642964
version "2.2.2"
29652965
resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-2.2.2.tgz#a9e6fec3c7dda85f8bb1b3ba7028604556fc825e"
29662966
dependencies:
@@ -2972,7 +2972,7 @@ readable-stream@^2.0.0, "readable-stream@^2.0.0 || ^1.1.13", readable-stream@^2.
29722972
string_decoder "~0.10.x"
29732973
util-deprecate "~1.0.1"
29742974

2975-
readable-stream@~2.0.0, readable-stream@~2.0.5:
2975+
readable-stream@~2.0.5:
29762976
version "2.0.6"
29772977
resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-2.0.6.tgz#8f90341e68a53ccc928788dacfcd11b36eb9b78e"
29782978
dependencies:
@@ -3487,7 +3487,7 @@ type-check@~0.3.2:
34873487
dependencies:
34883488
prelude-ls "~1.1.2"
34893489

3490-
typedarray@~0.0.5:
3490+
typedarray@^0.0.6:
34913491
version "0.0.6"
34923492
resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777"
34933493

0 commit comments

Comments
 (0)