UNPKG

16.3 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.WinPackager = void 0;
7
8function _bluebirdLst() {
9 const data = _interopRequireDefault(require("bluebird-lst"));
10
11 _bluebirdLst = function () {
12 return data;
13 };
14
15 return data;
16}
17
18function _builderUtil() {
19 const data = require("builder-util");
20
21 _builderUtil = function () {
22 return data;
23 };
24
25 return data;
26}
27
28function _builderUtilRuntime() {
29 const data = require("builder-util-runtime");
30
31 _builderUtilRuntime = function () {
32 return data;
33 };
34
35 return data;
36}
37
38function _fs() {
39 const data = require("builder-util/out/fs");
40
41 _fs = function () {
42 return data;
43 };
44
45 return data;
46}
47
48function _crypto() {
49 const data = require("crypto");
50
51 _crypto = function () {
52 return data;
53 };
54
55 return data;
56}
57
58function _fsExtra() {
59 const data = require("fs-extra");
60
61 _fsExtra = function () {
62 return data;
63 };
64
65 return data;
66}
67
68function _isCi() {
69 const data = _interopRequireDefault(require("is-ci"));
70
71 _isCi = function () {
72 return data;
73 };
74
75 return data;
76}
77
78function _lazyVal() {
79 const data = require("lazy-val");
80
81 _lazyVal = function () {
82 return data;
83 };
84
85 return data;
86}
87
88var path = _interopRequireWildcard(require("path"));
89
90function _codesign() {
91 const data = require("./codeSign/codesign");
92
93 _codesign = function () {
94 return data;
95 };
96
97 return data;
98}
99
100function _windowsCodeSign() {
101 const data = require("./codeSign/windowsCodeSign");
102
103 _windowsCodeSign = function () {
104 return data;
105 };
106
107 return data;
108}
109
110function _core() {
111 const data = require("./core");
112
113 _core = function () {
114 return data;
115 };
116
117 return data;
118}
119
120function _platformPackager() {
121 const data = require("./platformPackager");
122
123 _platformPackager = function () {
124 return data;
125 };
126
127 return data;
128}
129
130function _NsisTarget() {
131 const data = require("./targets/nsis/NsisTarget");
132
133 _NsisTarget = function () {
134 return data;
135 };
136
137 return data;
138}
139
140function _nsisUtil() {
141 const data = require("./targets/nsis/nsisUtil");
142
143 _nsisUtil = function () {
144 return data;
145 };
146
147 return data;
148}
149
150function _WebInstallerTarget() {
151 const data = require("./targets/nsis/WebInstallerTarget");
152
153 _WebInstallerTarget = function () {
154 return data;
155 };
156
157 return data;
158}
159
160function _targetFactory() {
161 const data = require("./targets/targetFactory");
162
163 _targetFactory = function () {
164 return data;
165 };
166
167 return data;
168}
169
170function _cacheManager() {
171 const data = require("./util/cacheManager");
172
173 _cacheManager = function () {
174 return data;
175 };
176
177 return data;
178}
179
180function _flags() {
181 const data = require("./util/flags");
182
183 _flags = function () {
184 return data;
185 };
186
187 return data;
188}
189
190function _timer() {
191 const data = require("./util/timer");
192
193 _timer = function () {
194 return data;
195 };
196
197 return data;
198}
199
200function _vm() {
201 const data = require("./vm/vm");
202
203 _vm = function () {
204 return data;
205 };
206
207 return data;
208}
209
210function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
211
212function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
213
214function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
215
216class WinPackager extends _platformPackager().PlatformPackager {
217 constructor(info) {
218 super(info, _core().Platform.WINDOWS);
219 this.cscInfo = new (_lazyVal().Lazy)(() => {
220 const platformSpecificBuildOptions = this.platformSpecificBuildOptions;
221
222 if (platformSpecificBuildOptions.certificateSubjectName != null || platformSpecificBuildOptions.certificateSha1 != null) {
223 return this.vm.value.then(vm => (0, _windowsCodeSign().getCertificateFromStoreInfo)(platformSpecificBuildOptions, vm)).catch(e => {
224 // https://github.com/electron-userland/electron-builder/pull/2397
225 if (platformSpecificBuildOptions.sign == null) {
226 throw e;
227 } else {
228 _builderUtil().log.debug({
229 error: e
230 }, "getCertificateFromStoreInfo error");
231
232 return null;
233 }
234 });
235 }
236
237 const certificateFile = platformSpecificBuildOptions.certificateFile;
238
239 if (certificateFile != null) {
240 const certificatePassword = this.getCscPassword();
241 return Promise.resolve({
242 file: certificateFile,
243 password: certificatePassword == null ? null : certificatePassword.trim()
244 });
245 }
246
247 const cscLink = this.getCscLink("WIN_CSC_LINK");
248
249 if (cscLink == null) {
250 return Promise.resolve(null);
251 }
252
253 return (0, _codesign().downloadCertificate)(cscLink, this.info.tempDirManager, this.projectDir) // before then
254 .catch(e => {
255 if (e instanceof _builderUtil().InvalidConfigurationError) {
256 throw new (_builderUtil().InvalidConfigurationError)(`Env WIN_CSC_LINK is not correct, cannot resolve: ${e.message}`);
257 } else {
258 throw e;
259 }
260 }).then(path => {
261 return {
262 file: path,
263 password: this.getCscPassword()
264 };
265 });
266 });
267 this._iconPath = new (_lazyVal().Lazy)(() => this.getOrConvertIcon("ico"));
268 this.vm = new (_lazyVal().Lazy)(() => process.platform === "win32" ? Promise.resolve(new (_vm().VmManager)()) : (0, _vm().getWindowsVm)(this.debugLogger));
269 this.computedPublisherName = new (_lazyVal().Lazy)(async () => {
270 const publisherName = this.platformSpecificBuildOptions.publisherName;
271
272 if (publisherName === null) {
273 return null;
274 } else if (publisherName != null) {
275 return (0, _builderUtil().asArray)(publisherName);
276 }
277
278 const certInfo = await this.lazyCertInfo.value;
279 return certInfo == null ? null : [certInfo.commonName];
280 });
281 this.lazyCertInfo = new (_lazyVal().Lazy)(async () => {
282 const cscInfo = await this.cscInfo.value;
283
284 if (cscInfo == null) {
285 return null;
286 }
287
288 if ("subject" in cscInfo) {
289 const bloodyMicrosoftSubjectDn = cscInfo.subject;
290 return {
291 commonName: (0, _builderUtilRuntime().parseDn)(bloodyMicrosoftSubjectDn).get("CN"),
292 bloodyMicrosoftSubjectDn
293 };
294 }
295
296 const cscFile = cscInfo.file;
297
298 if (cscFile == null) {
299 return null;
300 }
301
302 return await (0, _windowsCodeSign().getCertInfo)(cscFile, cscInfo.password || "");
303 });
304 }
305
306 get isForceCodeSigningVerification() {
307 return this.platformSpecificBuildOptions.verifyUpdateCodeSignature !== false;
308 }
309
310 get defaultTarget() {
311 return ["nsis"];
312 }
313
314 doGetCscPassword() {
315 return (0, _platformPackager().chooseNotNull)((0, _platformPackager().chooseNotNull)(this.platformSpecificBuildOptions.certificatePassword, process.env.WIN_CSC_KEY_PASSWORD), super.doGetCscPassword());
316 }
317
318 createTargets(targets, mapper) {
319 let copyElevateHelper;
320
321 const getCopyElevateHelper = () => {
322 if (copyElevateHelper == null) {
323 copyElevateHelper = new (_nsisUtil().CopyElevateHelper)();
324 }
325
326 return copyElevateHelper;
327 };
328
329 let helper;
330
331 const getHelper = () => {
332 if (helper == null) {
333 helper = new (_nsisUtil().AppPackageHelper)(getCopyElevateHelper());
334 }
335
336 return helper;
337 };
338
339 for (const name of targets) {
340 if (name === _core().DIR_TARGET) {
341 continue;
342 }
343
344 if (name === "nsis" || name === "portable") {
345 mapper(name, outDir => new (_NsisTarget().NsisTarget)(this, outDir, name, getHelper()));
346 } else if (name === "nsis-web") {
347 // package file format differs from nsis target
348 mapper(name, outDir => new (_WebInstallerTarget().WebInstallerTarget)(this, path.join(outDir, name), name, new (_nsisUtil().AppPackageHelper)(getCopyElevateHelper())));
349 } else {
350 const targetClass = (() => {
351 switch (name) {
352 case "squirrel":
353 try {
354 return require("electron-builder-squirrel-windows").default;
355 } catch (e) {
356 throw new (_builderUtil().InvalidConfigurationError)(`Module electron-builder-squirrel-windows must be installed in addition to build Squirrel.Windows: ${e.stack || e}`);
357 }
358
359 case "appx":
360 return require("./targets/AppxTarget").default;
361
362 case "msi":
363 return require("./targets/MsiTarget").default;
364
365 default:
366 return null;
367 }
368 })();
369
370 mapper(name, outDir => targetClass === null ? (0, _targetFactory().createCommonTarget)(name, outDir, this) : new targetClass(this, outDir, name));
371 }
372 }
373 }
374
375 getIconPath() {
376 return this._iconPath.value;
377 }
378
379 async sign(file, logMessagePrefix) {
380 const signOptions = {
381 path: file,
382 name: this.appInfo.productName,
383 site: await this.appInfo.computePackageUrl(),
384 options: this.platformSpecificBuildOptions
385 };
386 const cscInfo = await this.cscInfo.value;
387
388 if (cscInfo == null) {
389 if (this.platformSpecificBuildOptions.sign != null) {
390 await (0, _windowsCodeSign().sign)(signOptions, this);
391 } else if (this.forceCodeSigning) {
392 throw new (_builderUtil().InvalidConfigurationError)(`App is not signed and "forceCodeSigning" is set to true, please ensure that code signing configuration is correct, please see https://electron.build/code-signing`);
393 }
394
395 return;
396 }
397
398 if (logMessagePrefix == null) {
399 logMessagePrefix = "signing";
400 }
401
402 if ("file" in cscInfo) {
403 _builderUtil().log.info({
404 file: _builderUtil().log.filePath(file),
405 certificateFile: cscInfo.file
406 }, logMessagePrefix);
407 } else {
408 const info = cscInfo;
409
410 _builderUtil().log.info({
411 file: _builderUtil().log.filePath(file),
412 subject: info.subject,
413 thumbprint: info.thumbprint,
414 store: info.store,
415 user: info.isLocalMachineStore ? "local machine" : "current user"
416 }, logMessagePrefix);
417 }
418
419 await this.doSign({ ...signOptions,
420 cscInfo,
421 options: { ...this.platformSpecificBuildOptions
422 }
423 });
424 }
425
426 async doSign(options) {
427 for (let i = 0; i < 3; i++) {
428 try {
429 await (0, _windowsCodeSign().sign)(options, this);
430 break;
431 } catch (e) {
432 // https://github.com/electron-userland/electron-builder/issues/1414
433 const message = e.message;
434
435 if (message != null && message.includes("Couldn't resolve host name")) {
436 _builderUtil().log.warn({
437 error: message,
438 attempt: i + 1
439 }, `cannot sign`);
440
441 continue;
442 }
443
444 throw e;
445 }
446 }
447 }
448
449 async signAndEditResources(file, arch, outDir, internalName, requestedExecutionLevel) {
450 const appInfo = this.appInfo;
451 const files = [];
452 const args = [file, "--set-version-string", "FileDescription", appInfo.productName, "--set-version-string", "ProductName", appInfo.productName, "--set-version-string", "LegalCopyright", appInfo.copyright, "--set-file-version", appInfo.shortVersion || appInfo.buildVersion, "--set-product-version", appInfo.shortVersionWindows || appInfo.getVersionInWeirdWindowsForm()];
453
454 if (internalName != null) {
455 args.push("--set-version-string", "InternalName", internalName, "--set-version-string", "OriginalFilename", "");
456 }
457
458 if (requestedExecutionLevel != null && requestedExecutionLevel !== "asInvoker") {
459 args.push("--set-requested-execution-level", requestedExecutionLevel);
460 }
461
462 (0, _builderUtil().use)(appInfo.companyName, it => args.push("--set-version-string", "CompanyName", it));
463 (0, _builderUtil().use)(this.platformSpecificBuildOptions.legalTrademarks, it => args.push("--set-version-string", "LegalTrademarks", it));
464 const iconPath = await this.getIconPath();
465 (0, _builderUtil().use)(iconPath, it => {
466 files.push(it);
467 args.push("--set-icon", it);
468 });
469 const config = this.config;
470 const cscInfoForCacheDigest = !(0, _flags().isBuildCacheEnabled)() || _isCi().default || config.electronDist != null ? null : await this.cscInfo.value;
471 let buildCacheManager = null; // resources editing doesn't change executable for the same input and executed quickly - no need to complicate
472
473 if (cscInfoForCacheDigest != null) {
474 const cscFile = cscInfoForCacheDigest.file;
475
476 if (cscFile != null) {
477 files.push(cscFile);
478 }
479
480 const timer = (0, _timer().time)("executable cache");
481 const hash = (0, _crypto().createHash)("sha512");
482 hash.update(config.electronVersion || "no electronVersion");
483 hash.update(JSON.stringify(this.platformSpecificBuildOptions));
484 hash.update(JSON.stringify(args));
485 hash.update(this.platformSpecificBuildOptions.certificateSha1 || "no certificateSha1");
486 hash.update(this.platformSpecificBuildOptions.certificateSubjectName || "no subjectName");
487 buildCacheManager = new (_cacheManager().BuildCacheManager)(outDir, file, arch);
488
489 if (await buildCacheManager.copyIfValid(await (0, _cacheManager().digest)(hash, files))) {
490 timer.end();
491 return;
492 }
493
494 timer.end();
495 }
496
497 const timer = (0, _timer().time)("wine&sign"); // rcedit crashed of executed using wine, resourcehacker works
498
499 if (process.platform === "win32" || this.info.framework.name === "electron") {
500 await (0, _builderUtil().executeAppBuilder)(["rcedit", "--args", JSON.stringify(args)], undefined
501 /* child-process */
502 , {}, 3
503 /* retry five times */
504 );
505 }
506
507 await this.sign(file);
508 timer.end();
509
510 if (buildCacheManager != null) {
511 await buildCacheManager.save();
512 }
513 }
514
515 isSignDlls() {
516 return this.platformSpecificBuildOptions.signDlls === true;
517 }
518
519 createTransformerForExtraFiles(packContext) {
520 if (this.platformSpecificBuildOptions.signAndEditExecutable === false) {
521 return null;
522 }
523
524 return file => {
525 if (file.endsWith(".exe") || this.isSignDlls() && file.endsWith(".dll")) {
526 const parentDir = path.dirname(file);
527
528 if (parentDir !== packContext.appOutDir) {
529 return new (_fs().CopyFileTransformer)(file => this.sign(file));
530 }
531 }
532
533 return null;
534 };
535 }
536
537 async signApp(packContext, isAsar) {
538 const exeFileName = `${this.appInfo.productFilename}.exe`;
539
540 if (this.platformSpecificBuildOptions.signAndEditExecutable === false) {
541 return;
542 }
543
544 await _bluebirdLst().default.map((0, _fsExtra().readdir)(packContext.appOutDir), file => {
545 if (file === exeFileName) {
546 return this.signAndEditResources(path.join(packContext.appOutDir, exeFileName), packContext.arch, packContext.outDir, path.basename(exeFileName, ".exe"), this.platformSpecificBuildOptions.requestedExecutionLevel);
547 } else if (file.endsWith(".exe") || this.isSignDlls() && file.endsWith(".dll")) {
548 return this.sign(path.join(packContext.appOutDir, file));
549 }
550
551 return null;
552 });
553
554 if (!isAsar) {
555 return;
556 }
557
558 const outResourcesDir = path.join(packContext.appOutDir, "resources", "app.asar.unpacked"); // noinspection JSUnusedLocalSymbols
559
560 const fileToSign = await (0, _fs().walk)(outResourcesDir, (file, stat) => stat.isDirectory() || file.endsWith(".exe") || this.isSignDlls() && file.endsWith(".dll"));
561 await _bluebirdLst().default.map(fileToSign, file => this.sign(file), {
562 concurrency: 4
563 });
564 }
565
566} exports.WinPackager = WinPackager;
567// __ts-babel@6.0.4
568//# sourceMappingURL=winPackager.js.map
\No newline at end of file