1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.retry = exports.executeAppBuilder = exports.InvalidConfigurationError = exports.isEnvTrue = exports.isPullRequest = exports.getPlatformIconFileName = exports.replaceDefault = exports.addValue = exports.isTokenCharValid = exports.isEmptyOrSpaces = exports.use = exports.ExecError = exports.spawn = exports.spawnAndWrite = exports.doSpawn = exports.exec = exports.removePassword = exports.serializeToYaml = exports.debug7z = exports.deepAssign = exports.asArray = exports.exists = exports.copyFile = exports.DebugLogger = exports.AsyncTaskManager = exports.defaultArchFromString = exports.archFromString = exports.getArchSuffix = exports.toLinuxArchString = exports.getArchCliNames = exports.Arch = exports.debug = exports.log = exports.TmpDir = exports.safeStringifyJson = void 0;
|
4 | const _7zip_bin_1 = require("7zip-bin");
|
5 | const app_builder_bin_1 = require("app-builder-bin");
|
6 | const builder_util_runtime_1 = require("builder-util-runtime");
|
7 | const chalk = require("chalk");
|
8 | const child_process_1 = require("child_process");
|
9 | const cross_spawn_1 = require("cross-spawn");
|
10 | const crypto_1 = require("crypto");
|
11 | const debug_1 = require("debug");
|
12 | const js_yaml_1 = require("js-yaml");
|
13 | const path = require("path");
|
14 | const log_1 = require("./log");
|
15 | const source_map_support_1 = require("source-map-support");
|
16 | if (process.env.JEST_WORKER_ID == null) {
|
17 | source_map_support_1.install();
|
18 | }
|
19 | var builder_util_runtime_2 = require("builder-util-runtime");
|
20 | Object.defineProperty(exports, "safeStringifyJson", { enumerable: true, get: function () { return builder_util_runtime_2.safeStringifyJson; } });
|
21 | var temp_file_1 = require("temp-file");
|
22 | Object.defineProperty(exports, "TmpDir", { enumerable: true, get: function () { return temp_file_1.TmpDir; } });
|
23 | var log_2 = require("./log");
|
24 | Object.defineProperty(exports, "log", { enumerable: true, get: function () { return log_2.log; } });
|
25 | Object.defineProperty(exports, "debug", { enumerable: true, get: function () { return log_2.debug; } });
|
26 | var arch_1 = require("./arch");
|
27 | Object.defineProperty(exports, "Arch", { enumerable: true, get: function () { return arch_1.Arch; } });
|
28 | Object.defineProperty(exports, "getArchCliNames", { enumerable: true, get: function () { return arch_1.getArchCliNames; } });
|
29 | Object.defineProperty(exports, "toLinuxArchString", { enumerable: true, get: function () { return arch_1.toLinuxArchString; } });
|
30 | Object.defineProperty(exports, "getArchSuffix", { enumerable: true, get: function () { return arch_1.getArchSuffix; } });
|
31 | Object.defineProperty(exports, "archFromString", { enumerable: true, get: function () { return arch_1.archFromString; } });
|
32 | Object.defineProperty(exports, "defaultArchFromString", { enumerable: true, get: function () { return arch_1.defaultArchFromString; } });
|
33 | var asyncTaskManager_1 = require("./asyncTaskManager");
|
34 | Object.defineProperty(exports, "AsyncTaskManager", { enumerable: true, get: function () { return asyncTaskManager_1.AsyncTaskManager; } });
|
35 | var DebugLogger_1 = require("./DebugLogger");
|
36 | Object.defineProperty(exports, "DebugLogger", { enumerable: true, get: function () { return DebugLogger_1.DebugLogger; } });
|
37 | var fs_1 = require("./fs");
|
38 | Object.defineProperty(exports, "copyFile", { enumerable: true, get: function () { return fs_1.copyFile; } });
|
39 | Object.defineProperty(exports, "exists", { enumerable: true, get: function () { return fs_1.exists; } });
|
40 | var builder_util_runtime_3 = require("builder-util-runtime");
|
41 | Object.defineProperty(exports, "asArray", { enumerable: true, get: function () { return builder_util_runtime_3.asArray; } });
|
42 | var deepAssign_1 = require("./deepAssign");
|
43 | Object.defineProperty(exports, "deepAssign", { enumerable: true, get: function () { return deepAssign_1.deepAssign; } });
|
44 | exports.debug7z = debug_1.default("electron-builder:7z");
|
45 | function serializeToYaml(object, skipInvalid = false, noRefs = false) {
|
46 | return js_yaml_1.dump(object, {
|
47 | lineWidth: 8000,
|
48 | skipInvalid,
|
49 | noRefs,
|
50 | });
|
51 | }
|
52 | exports.serializeToYaml = serializeToYaml;
|
53 | function removePassword(input) {
|
54 | return input.replace(/(-String |-P |pass:| \/p |-pass |--secretKey |--accessKey |-p )([^ ]+)/g, (match, p1, p2) => {
|
55 | if (p1.trim() === "/p" && p2.startsWith("\\\\Mac\\Host\\\\")) {
|
56 |
|
57 | return `${p1}${p2}`;
|
58 | }
|
59 | return `${p1}${crypto_1.createHash("sha256").update(p2).digest("hex")} (sha256 hash)`;
|
60 | });
|
61 | }
|
62 | exports.removePassword = removePassword;
|
63 | function getProcessEnv(env) {
|
64 | if (process.platform === "win32") {
|
65 | return env == null ? undefined : env;
|
66 | }
|
67 | const finalEnv = {
|
68 | ...(env || process.env),
|
69 | };
|
70 |
|
71 |
|
72 | const locale = process.platform === "linux" ? process.env.LANG || "C.UTF-8" : "en_US.UTF-8";
|
73 | finalEnv.LANG = locale;
|
74 | finalEnv.LC_CTYPE = locale;
|
75 | finalEnv.LC_ALL = locale;
|
76 | return finalEnv;
|
77 | }
|
78 | function exec(file, args, options, isLogOutIfDebug = true) {
|
79 | if (log_1.log.isDebugEnabled) {
|
80 | const logFields = {
|
81 | file,
|
82 | args: args == null ? "" : removePassword(args.join(" ")),
|
83 | };
|
84 | if (options != null) {
|
85 | if (options.cwd != null) {
|
86 | logFields.cwd = options.cwd;
|
87 | }
|
88 | if (options.env != null) {
|
89 | const diffEnv = { ...options.env };
|
90 | for (const name of Object.keys(process.env)) {
|
91 | if (process.env[name] === options.env[name]) {
|
92 | delete diffEnv[name];
|
93 | }
|
94 | }
|
95 | logFields.env = builder_util_runtime_1.safeStringifyJson(diffEnv);
|
96 | }
|
97 | }
|
98 | log_1.log.debug(logFields, "executing");
|
99 | }
|
100 | return new Promise((resolve, reject) => {
|
101 | child_process_1.execFile(file, args, {
|
102 | ...options,
|
103 | maxBuffer: 1000 * 1024 * 1024,
|
104 | env: getProcessEnv(options == null ? null : options.env),
|
105 | }, (error, stdout, stderr) => {
|
106 | if (error == null) {
|
107 | if (isLogOutIfDebug && log_1.log.isDebugEnabled) {
|
108 | const logFields = {
|
109 | file,
|
110 | };
|
111 | if (stdout.length > 0) {
|
112 | logFields.stdout = stdout;
|
113 | }
|
114 | if (stderr.length > 0) {
|
115 | logFields.stderr = stderr;
|
116 | }
|
117 | log_1.log.debug(logFields, "executed");
|
118 | }
|
119 | resolve(stdout.toString());
|
120 | }
|
121 | else {
|
122 | let message = chalk.red(removePassword(`Exit code: ${error.code}. ${error.message}`));
|
123 | if (stdout.length !== 0) {
|
124 | if (file.endsWith("wine")) {
|
125 | stdout = stdout.toString();
|
126 | }
|
127 | message += `\n${chalk.yellow(stdout.toString())}`;
|
128 | }
|
129 | if (stderr.length !== 0) {
|
130 | if (file.endsWith("wine")) {
|
131 | stderr = stderr.toString();
|
132 | }
|
133 | message += `\n${chalk.red(stderr.toString())}`;
|
134 | }
|
135 | reject(new Error(message));
|
136 | }
|
137 | });
|
138 | });
|
139 | }
|
140 | exports.exec = exec;
|
141 | function logSpawn(command, args, options) {
|
142 |
|
143 | if (!log_1.log.isDebugEnabled) {
|
144 | return;
|
145 | }
|
146 | const argsString = removePassword(args.join(" "));
|
147 | const logFields = {
|
148 | command: command + " " + (command === "docker" ? argsString : removePassword(argsString)),
|
149 | };
|
150 | if (options != null && options.cwd != null) {
|
151 | logFields.cwd = options.cwd;
|
152 | }
|
153 | log_1.log.debug(logFields, "spawning");
|
154 | }
|
155 | function doSpawn(command, args, options, extraOptions) {
|
156 | if (options == null) {
|
157 | options = {};
|
158 | }
|
159 | options.env = getProcessEnv(options.env);
|
160 | if (options.stdio == null) {
|
161 | const isDebugEnabled = log_1.debug.enabled;
|
162 |
|
163 | options.stdio = [extraOptions != null && extraOptions.isPipeInput ? "pipe" : "ignore", isDebugEnabled ? "inherit" : "pipe", isDebugEnabled ? "inherit" : "pipe"];
|
164 | }
|
165 | logSpawn(command, args, options);
|
166 | try {
|
167 | return cross_spawn_1.spawn(command, args, options);
|
168 | }
|
169 | catch (e) {
|
170 | throw new Error(`Cannot spawn ${command}: ${e.stack || e}`);
|
171 | }
|
172 | }
|
173 | exports.doSpawn = doSpawn;
|
174 | function spawnAndWrite(command, args, data, options) {
|
175 | const childProcess = doSpawn(command, args, options, { isPipeInput: true });
|
176 | const timeout = setTimeout(() => childProcess.kill(), 4 * 60 * 1000);
|
177 | return new Promise((resolve, reject) => {
|
178 | handleProcess("close", childProcess, command, () => {
|
179 | try {
|
180 | clearTimeout(timeout);
|
181 | }
|
182 | finally {
|
183 | resolve(undefined);
|
184 | }
|
185 | }, error => {
|
186 | try {
|
187 | clearTimeout(timeout);
|
188 | }
|
189 | finally {
|
190 | reject(error);
|
191 | }
|
192 | });
|
193 | childProcess.stdin.end(data);
|
194 | });
|
195 | }
|
196 | exports.spawnAndWrite = spawnAndWrite;
|
197 | function spawn(command, args, options, extraOptions) {
|
198 | return new Promise((resolve, reject) => {
|
199 | handleProcess("close", doSpawn(command, args || [], options, extraOptions), command, resolve, reject);
|
200 | });
|
201 | }
|
202 | exports.spawn = spawn;
|
203 | function handleProcess(event, childProcess, command, resolve, reject) {
|
204 | childProcess.on("error", reject);
|
205 | let out = "";
|
206 | if (childProcess.stdout != null) {
|
207 | childProcess.stdout.on("data", (data) => {
|
208 | out += data;
|
209 | });
|
210 | }
|
211 | let errorOut = "";
|
212 | if (childProcess.stderr != null) {
|
213 | childProcess.stderr.on("data", (data) => {
|
214 | errorOut += data;
|
215 | });
|
216 | }
|
217 | childProcess.once(event, (code) => {
|
218 | if (log_1.log.isDebugEnabled) {
|
219 | const fields = {
|
220 | command: path.basename(command),
|
221 | code,
|
222 | pid: childProcess.pid,
|
223 | };
|
224 | if (out.length > 0) {
|
225 | fields.out = out;
|
226 | }
|
227 | log_1.log.debug(fields, "exited");
|
228 | }
|
229 | if (code === 0) {
|
230 | if (resolve != null) {
|
231 | resolve(out);
|
232 | }
|
233 | }
|
234 | else {
|
235 | reject(new ExecError(command, code, out, errorOut));
|
236 | }
|
237 | });
|
238 | }
|
239 | function formatOut(text, title) {
|
240 | return text.length === 0 ? "" : `\n${title}:\n${text}`;
|
241 | }
|
242 | class ExecError extends Error {
|
243 | constructor(command, exitCode, out, errorOut, code = "ERR_ELECTRON_BUILDER_CANNOT_EXECUTE") {
|
244 | super(`${command} exited with code ${code}${formatOut(out, "Output")}${formatOut(errorOut, "Error output")}`);
|
245 | this.exitCode = exitCode;
|
246 | this.alreadyLogged = false;
|
247 | this.code = code;
|
248 | }
|
249 | }
|
250 | exports.ExecError = ExecError;
|
251 | function use(value, task) {
|
252 | return value == null ? null : task(value);
|
253 | }
|
254 | exports.use = use;
|
255 | function isEmptyOrSpaces(s) {
|
256 | return s == null || s.trim().length === 0;
|
257 | }
|
258 | exports.isEmptyOrSpaces = isEmptyOrSpaces;
|
259 | function isTokenCharValid(token) {
|
260 | return /^[.\w/=+-]+$/.test(token);
|
261 | }
|
262 | exports.isTokenCharValid = isTokenCharValid;
|
263 | function addValue(map, key, value) {
|
264 | const list = map.get(key);
|
265 | if (list == null) {
|
266 | map.set(key, [value]);
|
267 | }
|
268 | else if (!list.includes(value)) {
|
269 | list.push(value);
|
270 | }
|
271 | }
|
272 | exports.addValue = addValue;
|
273 | function replaceDefault(inList, defaultList) {
|
274 | if (inList == null || (inList.length === 1 && inList[0] === "default")) {
|
275 | return defaultList;
|
276 | }
|
277 | const index = inList.indexOf("default");
|
278 | if (index >= 0) {
|
279 | const list = inList.slice(0, index);
|
280 | list.push(...defaultList);
|
281 | if (index !== inList.length - 1) {
|
282 | list.push(...inList.slice(index + 1));
|
283 | }
|
284 | inList = list;
|
285 | }
|
286 | return inList;
|
287 | }
|
288 | exports.replaceDefault = replaceDefault;
|
289 | function getPlatformIconFileName(value, isMac) {
|
290 | if (value === undefined) {
|
291 | return undefined;
|
292 | }
|
293 | if (value === null) {
|
294 | return null;
|
295 | }
|
296 | if (!value.includes(".")) {
|
297 | return `${value}.${isMac ? "icns" : "ico"}`;
|
298 | }
|
299 | return value.replace(isMac ? ".ico" : ".icns", isMac ? ".icns" : ".ico");
|
300 | }
|
301 | exports.getPlatformIconFileName = getPlatformIconFileName;
|
302 | function isPullRequest() {
|
303 |
|
304 | function isSet(value) {
|
305 |
|
306 | return value && value !== "false";
|
307 | }
|
308 | return (isSet(process.env.TRAVIS_PULL_REQUEST) || isSet(process.env.CIRCLE_PULL_REQUEST) || isSet(process.env.BITRISE_PULL_REQUEST) || isSet(process.env.APPVEYOR_PULL_REQUEST_NUMBER));
|
309 | }
|
310 | exports.isPullRequest = isPullRequest;
|
311 | function isEnvTrue(value) {
|
312 | if (value != null) {
|
313 | value = value.trim();
|
314 | }
|
315 | return value === "true" || value === "" || value === "1";
|
316 | }
|
317 | exports.isEnvTrue = isEnvTrue;
|
318 | class InvalidConfigurationError extends Error {
|
319 | constructor(message, code = "ERR_ELECTRON_BUILDER_INVALID_CONFIGURATION") {
|
320 | super(message);
|
321 | this.code = code;
|
322 | }
|
323 | }
|
324 | exports.InvalidConfigurationError = InvalidConfigurationError;
|
325 | function executeAppBuilder(args, childProcessConsumer, extraOptions = {}, maxRetries = 0) {
|
326 | const command = app_builder_bin_1.appBuilderPath;
|
327 | const env = {
|
328 | ...process.env,
|
329 | SZA_PATH: _7zip_bin_1.path7za,
|
330 | FORCE_COLOR: chalk.level === 0 ? "0" : "1",
|
331 | };
|
332 | const cacheEnv = process.env.ELECTRON_BUILDER_CACHE;
|
333 | if (cacheEnv != null && cacheEnv.length > 0) {
|
334 | env.ELECTRON_BUILDER_CACHE = path.resolve(cacheEnv);
|
335 | }
|
336 | if (extraOptions.env != null) {
|
337 | Object.assign(env, extraOptions.env);
|
338 | }
|
339 | function runCommand() {
|
340 | return new Promise((resolve, reject) => {
|
341 | const childProcess = doSpawn(command, args, {
|
342 | stdio: ["ignore", "pipe", process.stdout],
|
343 | ...extraOptions,
|
344 | env,
|
345 | });
|
346 | if (childProcessConsumer != null) {
|
347 | childProcessConsumer(childProcess);
|
348 | }
|
349 | handleProcess("close", childProcess, command, resolve, error => {
|
350 | if (error instanceof ExecError && error.exitCode === 2) {
|
351 | error.alreadyLogged = true;
|
352 | }
|
353 | reject(error);
|
354 | });
|
355 | });
|
356 | }
|
357 | if (maxRetries === 0) {
|
358 | return runCommand();
|
359 | }
|
360 | else {
|
361 | return retry(runCommand, maxRetries, 1000);
|
362 | }
|
363 | }
|
364 | exports.executeAppBuilder = executeAppBuilder;
|
365 | async function retry(task, retriesLeft, interval) {
|
366 | try {
|
367 | return await task();
|
368 | }
|
369 | catch (error) {
|
370 | log_1.log.info(`Above command failed, retrying ${retriesLeft} more times`);
|
371 | if (retriesLeft > 0) {
|
372 | await new Promise(resolve => setTimeout(resolve, interval));
|
373 | return await retry(task, retriesLeft - 1, interval);
|
374 | }
|
375 | else {
|
376 | throw error;
|
377 | }
|
378 | }
|
379 | }
|
380 | exports.retry = retry;
|
381 |
|
\ | No newline at end of file |