UNPKG

15.6 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.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;
4const _7zip_bin_1 = require("7zip-bin");
5const app_builder_bin_1 = require("app-builder-bin");
6const builder_util_runtime_1 = require("builder-util-runtime");
7const chalk = require("chalk");
8const child_process_1 = require("child_process");
9const cross_spawn_1 = require("cross-spawn");
10const crypto_1 = require("crypto");
11const debug_1 = require("debug");
12const js_yaml_1 = require("js-yaml");
13const path = require("path");
14const log_1 = require("./log");
15const source_map_support_1 = require("source-map-support");
16if (process.env.JEST_WORKER_ID == null) {
17 source_map_support_1.install();
18}
19var builder_util_runtime_2 = require("builder-util-runtime");
20Object.defineProperty(exports, "safeStringifyJson", { enumerable: true, get: function () { return builder_util_runtime_2.safeStringifyJson; } });
21var temp_file_1 = require("temp-file");
22Object.defineProperty(exports, "TmpDir", { enumerable: true, get: function () { return temp_file_1.TmpDir; } });
23var log_2 = require("./log");
24Object.defineProperty(exports, "log", { enumerable: true, get: function () { return log_2.log; } });
25Object.defineProperty(exports, "debug", { enumerable: true, get: function () { return log_2.debug; } });
26var arch_1 = require("./arch");
27Object.defineProperty(exports, "Arch", { enumerable: true, get: function () { return arch_1.Arch; } });
28Object.defineProperty(exports, "getArchCliNames", { enumerable: true, get: function () { return arch_1.getArchCliNames; } });
29Object.defineProperty(exports, "toLinuxArchString", { enumerable: true, get: function () { return arch_1.toLinuxArchString; } });
30Object.defineProperty(exports, "getArchSuffix", { enumerable: true, get: function () { return arch_1.getArchSuffix; } });
31Object.defineProperty(exports, "archFromString", { enumerable: true, get: function () { return arch_1.archFromString; } });
32Object.defineProperty(exports, "defaultArchFromString", { enumerable: true, get: function () { return arch_1.defaultArchFromString; } });
33var asyncTaskManager_1 = require("./asyncTaskManager");
34Object.defineProperty(exports, "AsyncTaskManager", { enumerable: true, get: function () { return asyncTaskManager_1.AsyncTaskManager; } });
35var DebugLogger_1 = require("./DebugLogger");
36Object.defineProperty(exports, "DebugLogger", { enumerable: true, get: function () { return DebugLogger_1.DebugLogger; } });
37var fs_1 = require("./fs");
38Object.defineProperty(exports, "copyFile", { enumerable: true, get: function () { return fs_1.copyFile; } });
39Object.defineProperty(exports, "exists", { enumerable: true, get: function () { return fs_1.exists; } });
40var builder_util_runtime_3 = require("builder-util-runtime");
41Object.defineProperty(exports, "asArray", { enumerable: true, get: function () { return builder_util_runtime_3.asArray; } });
42var deepAssign_1 = require("./deepAssign");
43Object.defineProperty(exports, "deepAssign", { enumerable: true, get: function () { return deepAssign_1.deepAssign; } });
44exports.debug7z = debug_1.default("electron-builder:7z");
45function serializeToYaml(object, skipInvalid = false, noRefs = false) {
46 return js_yaml_1.dump(object, {
47 lineWidth: 8000,
48 skipInvalid,
49 noRefs,
50 });
51}
52exports.serializeToYaml = serializeToYaml;
53function 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 // appx /p
57 return `${p1}${p2}`;
58 }
59 return `${p1}${crypto_1.createHash("sha256").update(p2).digest("hex")} (sha256 hash)`;
60 });
61}
62exports.removePassword = removePassword;
63function getProcessEnv(env) {
64 if (process.platform === "win32") {
65 return env == null ? undefined : env;
66 }
67 const finalEnv = {
68 ...(env || process.env),
69 };
70 // without LC_CTYPE dpkg can returns encoded unicode symbols
71 // set LC_CTYPE to avoid crash https://github.com/electron-userland/electron-builder/issues/503 Even "en_DE.UTF-8" leads to error.
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}
78function 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}
140exports.exec = exec;
141function logSpawn(command, args, options) {
142 // use general debug.enabled to log spawn, because it doesn't produce a lot of output (the only line), but important in any case
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}
155function 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 // do not ignore stdout/stderr if not debug, because in this case we will read into buffer and print on error
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}
173exports.doSpawn = doSpawn;
174function 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}
196exports.spawnAndWrite = spawnAndWrite;
197function spawn(command, args, options, extraOptions) {
198 return new Promise((resolve, reject) => {
199 handleProcess("close", doSpawn(command, args || [], options, extraOptions), command, resolve, reject);
200 });
201}
202exports.spawn = spawn;
203function 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}
239function formatOut(text, title) {
240 return text.length === 0 ? "" : `\n${title}:\n${text}`;
241}
242class 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}
250exports.ExecError = ExecError;
251function use(value, task) {
252 return value == null ? null : task(value);
253}
254exports.use = use;
255function isEmptyOrSpaces(s) {
256 return s == null || s.trim().length === 0;
257}
258exports.isEmptyOrSpaces = isEmptyOrSpaces;
259function isTokenCharValid(token) {
260 return /^[.\w/=+-]+$/.test(token);
261}
262exports.isTokenCharValid = isTokenCharValid;
263function 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}
272exports.addValue = addValue;
273function 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}
288exports.replaceDefault = replaceDefault;
289function 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}
301exports.getPlatformIconFileName = getPlatformIconFileName;
302function isPullRequest() {
303 // TRAVIS_PULL_REQUEST is set to the pull request number if the current job is a pull request build, or false if it’s not.
304 function isSet(value) {
305 // value can be or null, or empty string
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}
310exports.isPullRequest = isPullRequest;
311function isEnvTrue(value) {
312 if (value != null) {
313 value = value.trim();
314 }
315 return value === "true" || value === "" || value === "1";
316}
317exports.isEnvTrue = isEnvTrue;
318class InvalidConfigurationError extends Error {
319 constructor(message, code = "ERR_ELECTRON_BUILDER_INVALID_CONFIGURATION") {
320 super(message);
321 this.code = code;
322 }
323}
324exports.InvalidConfigurationError = InvalidConfigurationError;
325function 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}
364exports.executeAppBuilder = executeAppBuilder;
365async 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}
380exports.retry = retry;
381//# sourceMappingURL=util.js.map
\No newline at end of file