UNPKG

6.18 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.rebuild = exports.nodeGypRebuild = exports.getGypEnv = exports.installOrRebuild = void 0;
4const builder_util_1 = require("builder-util");
5const fs_extra_1 = require("fs-extra");
6const os_1 = require("os");
7const path = require("path");
8const appBuilder_1 = require("./appBuilder");
9async function installOrRebuild(config, appDir, options, forceInstall = false) {
10 const effectiveOptions = {
11 buildFromSource: config.buildDependenciesFromSource === true,
12 additionalArgs: builder_util_1.asArray(config.npmArgs),
13 ...options,
14 };
15 let isDependenciesInstalled = false;
16 for (const fileOrDir of ["node_modules", ".pnp.js"]) {
17 if (await fs_extra_1.pathExists(path.join(appDir, fileOrDir))) {
18 isDependenciesInstalled = true;
19 break;
20 }
21 }
22 if (forceInstall || !isDependenciesInstalled) {
23 await installDependencies(appDir, effectiveOptions);
24 }
25 else {
26 await rebuild(appDir, effectiveOptions);
27 }
28}
29exports.installOrRebuild = installOrRebuild;
30function getElectronGypCacheDir() {
31 return path.join(os_1.homedir(), ".electron-gyp");
32}
33function getGypEnv(frameworkInfo, platform, arch, buildFromSource) {
34 const npmConfigArch = arch === "armv7l" ? "arm" : arch;
35 const common = {
36 ...process.env,
37 npm_config_arch: npmConfigArch,
38 npm_config_target_arch: npmConfigArch,
39 npm_config_platform: platform,
40 npm_config_build_from_source: buildFromSource,
41 // required for node-pre-gyp
42 npm_config_target_platform: platform,
43 npm_config_update_binary: true,
44 npm_config_fallback_to_build: true,
45 };
46 if (platform !== process.platform) {
47 common.npm_config_force = "true";
48 }
49 if (platform === "win32" || platform === "darwin") {
50 common.npm_config_target_libc = "unknown";
51 }
52 if (!frameworkInfo.useCustomDist) {
53 return common;
54 }
55 // https://github.com/nodejs/node-gyp/issues/21
56 return {
57 ...common,
58 npm_config_disturl: "https://electronjs.org/headers",
59 npm_config_target: frameworkInfo.version,
60 npm_config_runtime: "electron",
61 npm_config_devdir: getElectronGypCacheDir(),
62 };
63}
64exports.getGypEnv = getGypEnv;
65function checkYarnBerry() {
66 var _a;
67 const npmUserAgent = process.env["npm_config_user_agent"] || "";
68 const regex = /yarn\/(\d+)\./gm;
69 const yarnVersionMatch = regex.exec(npmUserAgent);
70 const yarnMajorVersion = Number((_a = yarnVersionMatch === null || yarnVersionMatch === void 0 ? void 0 : yarnVersionMatch[1]) !== null && _a !== void 0 ? _a : 0);
71 return yarnMajorVersion >= 2;
72}
73function installDependencies(appDir, options) {
74 const platform = options.platform || process.platform;
75 const arch = options.arch || process.arch;
76 const additionalArgs = options.additionalArgs;
77 builder_util_1.log.info({ platform, arch, appDir }, `installing production dependencies`);
78 let execPath = process.env.npm_execpath || process.env.NPM_CLI_JS;
79 const execArgs = ["install"];
80 const isYarnBerry = checkYarnBerry();
81 if (!isYarnBerry) {
82 if (process.env.NPM_NO_BIN_LINKS === "true") {
83 execArgs.push("--no-bin-links");
84 }
85 execArgs.push("--production");
86 }
87 if (!isRunningYarn(execPath)) {
88 execArgs.push("--prefer-offline");
89 }
90 if (execPath == null) {
91 execPath = getPackageToolPath();
92 }
93 else if (!isYarnBerry) {
94 execArgs.unshift(execPath);
95 execPath = process.env.npm_node_execpath || process.env.NODE_EXE || "node";
96 }
97 if (additionalArgs != null) {
98 execArgs.push(...additionalArgs);
99 }
100 return builder_util_1.spawn(execPath, execArgs, {
101 cwd: appDir,
102 env: getGypEnv(options.frameworkInfo, platform, arch, options.buildFromSource === true),
103 });
104}
105async function nodeGypRebuild(platform, arch, frameworkInfo) {
106 builder_util_1.log.info({ platform, arch }, "executing node-gyp rebuild");
107 // this script must be used only for electron
108 const nodeGyp = `node-gyp${process.platform === "win32" ? ".cmd" : ""}`;
109 const args = ["rebuild"];
110 // headers of old Electron versions do not have a valid config.gypi file
111 // and --force-process-config must be passed to node-gyp >= 8.4.0 to
112 // correctly build modules for them.
113 // see also https://github.com/nodejs/node-gyp/pull/2497
114 const [major, minor] = frameworkInfo.version.split('.').slice(0, 2).map(n => parseInt(n, 10));
115 if ((major <= 13) ||
116 (major == 14 && minor <= 1) ||
117 (major == 15 && minor <= 2)) {
118 args.push('--force-process-config');
119 }
120 await builder_util_1.spawn(nodeGyp, args, { env: getGypEnv(frameworkInfo, platform, arch, true) });
121}
122exports.nodeGypRebuild = nodeGypRebuild;
123function getPackageToolPath() {
124 if (process.env.FORCE_YARN === "true") {
125 return process.platform === "win32" ? "yarn.cmd" : "yarn";
126 }
127 else {
128 return process.platform === "win32" ? "npm.cmd" : "npm";
129 }
130}
131function isRunningYarn(execPath) {
132 const userAgent = process.env.npm_config_user_agent;
133 return process.env.FORCE_YARN === "true" || (execPath != null && path.basename(execPath).startsWith("yarn")) || (userAgent != null && /\byarn\b/.test(userAgent));
134}
135/** @internal */
136async function rebuild(appDir, options) {
137 const configuration = {
138 dependencies: await options.productionDeps.value,
139 nodeExecPath: process.execPath,
140 platform: options.platform || process.platform,
141 arch: options.arch || process.arch,
142 additionalArgs: options.additionalArgs,
143 execPath: process.env.npm_execpath || process.env.NPM_CLI_JS,
144 buildFromSource: options.buildFromSource === true,
145 };
146 const env = getGypEnv(options.frameworkInfo, configuration.platform, configuration.arch, options.buildFromSource === true);
147 await appBuilder_1.executeAppBuilderAndWriteJson(["rebuild-node-modules"], configuration, { env, cwd: appDir });
148}
149exports.rebuild = rebuild;
150//# sourceMappingURL=yarn.js.map
\No newline at end of file