UNPKG

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