1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.rebuild = exports.nodeGypRebuild = exports.getGypEnv = exports.installOrRebuild = void 0;
|
4 | const builder_util_1 = require("builder-util");
|
5 | const fs_extra_1 = require("fs-extra");
|
6 | const os_1 = require("os");
|
7 | const path = require("path");
|
8 | const appBuilder_1 = require("./appBuilder");
|
9 | async 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 | }
|
29 | exports.installOrRebuild = installOrRebuild;
|
30 | function getElectronGypCacheDir() {
|
31 | return path.join(os_1.homedir(), ".electron-gyp");
|
32 | }
|
33 | function 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 |
|
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 |
|
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 | }
|
64 | exports.getGypEnv = getGypEnv;
|
65 | function 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 | }
|
73 | function 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 | }
|
105 | async function nodeGypRebuild(platform, arch, frameworkInfo) {
|
106 | builder_util_1.log.info({ platform, arch }, "executing node-gyp rebuild");
|
107 |
|
108 | const nodeGyp = `node-gyp${process.platform === "win32" ? ".cmd" : ""}`;
|
109 | const args = ["rebuild"];
|
110 |
|
111 |
|
112 |
|
113 |
|
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 | }
|
122 | exports.nodeGypRebuild = nodeGypRebuild;
|
123 | function 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 | }
|
131 | function 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 |
|
136 | async 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 | }
|
149 | exports.rebuild = rebuild;
|
150 |
|
\ | No newline at end of file |