UNPKG

23.1 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.createPublisher = createPublisher;
7exports.computeDownloadUrl = computeDownloadUrl;
8exports.getPublishConfigs = exports.getPublishConfigsForUpdateInfo = exports.getAppUpdatePublishConfiguration = exports.PublishManager = void 0;
9
10function _bluebirdLst() {
11 const data = _interopRequireWildcard(require("bluebird-lst"));
12
13 _bluebirdLst = function () {
14 return data;
15 };
16
17 return data;
18}
19
20function _builderUtil() {
21 const data = require("builder-util");
22
23 _builderUtil = function () {
24 return data;
25 };
26
27 return data;
28}
29
30function _builderUtilRuntime() {
31 const data = require("builder-util-runtime");
32
33 _builderUtilRuntime = function () {
34 return data;
35 };
36
37 return data;
38}
39
40var _debug2 = _interopRequireDefault(require("debug"));
41
42function _electronPublish() {
43 const data = require("electron-publish");
44
45 _electronPublish = function () {
46 return data;
47 };
48
49 return data;
50}
51
52function _BintrayPublisher() {
53 const data = require("electron-publish/out/BintrayPublisher");
54
55 _BintrayPublisher = function () {
56 return data;
57 };
58
59 return data;
60}
61
62function _gitHubPublisher() {
63 const data = require("electron-publish/out/gitHubPublisher");
64
65 _gitHubPublisher = function () {
66 return data;
67 };
68
69 return data;
70}
71
72function _multiProgress() {
73 const data = require("electron-publish/out/multiProgress");
74
75 _multiProgress = function () {
76 return data;
77 };
78
79 return data;
80}
81
82function _fsExtraP() {
83 const data = require("fs-extra-p");
84
85 _fsExtraP = function () {
86 return data;
87 };
88
89 return data;
90}
91
92function _isCi() {
93 const data = _interopRequireDefault(require("is-ci"));
94
95 _isCi = function () {
96 return data;
97 };
98
99 return data;
100}
101
102var path = _interopRequireWildcard(require("path"));
103
104function url() {
105 const data = _interopRequireWildcard(require("url"));
106
107 url = function () {
108 return data;
109 };
110
111 return data;
112}
113
114function _s3Publisher() {
115 const data = _interopRequireDefault(require("electron-publish/out/s3/s3Publisher"));
116
117 _s3Publisher = function () {
118 return data;
119 };
120
121 return data;
122}
123
124function _spacesPublisher() {
125 const data = _interopRequireDefault(require("electron-publish/out/s3/spacesPublisher"));
126
127 _spacesPublisher = function () {
128 return data;
129 };
130
131 return data;
132}
133
134function _index() {
135 const data = require("../index");
136
137 _index = function () {
138 return data;
139 };
140
141 return data;
142}
143
144function _macroExpander() {
145 const data = require("../util/macroExpander");
146
147 _macroExpander = function () {
148 return data;
149 };
150
151 return data;
152}
153
154function _updateInfoBuilder() {
155 const data = require("./updateInfoBuilder");
156
157 _updateInfoBuilder = function () {
158 return data;
159 };
160
161 return data;
162}
163
164let resolvePublishConfigurations = (() => {
165 var _ref5 = (0, _bluebirdLst().coroutine)(function* (publishers, platformPackager, packager, arch, errorIfCannot) {
166 if (publishers == null) {
167 let serviceName = null;
168
169 if (!(0, _builderUtil().isEmptyOrSpaces)(process.env.GH_TOKEN) || !(0, _builderUtil().isEmptyOrSpaces)(process.env.GITHUB_TOKEN)) {
170 serviceName = "github";
171 } else if (!(0, _builderUtil().isEmptyOrSpaces)(process.env.BT_TOKEN)) {
172 serviceName = "bintray";
173 }
174
175 if (serviceName != null) {
176 _builderUtil().log.debug(null, `Detect ${serviceName} as publish provider`);
177
178 return [yield getResolvedPublishConfig(platformPackager, packager, {
179 provider: serviceName
180 }, arch, errorIfCannot)];
181 }
182 }
183
184 if (publishers == null) {
185 return [];
186 }
187
188 debug(`Explicit publish provider: ${(0, _builderUtil().safeStringifyJson)(publishers)}`);
189 return yield _bluebirdLst().default.map((0, _builderUtil().asArray)(publishers), it => getResolvedPublishConfig(platformPackager, packager, typeof it === "string" ? {
190 provider: it
191 } : it, arch, errorIfCannot));
192 });
193
194 return function resolvePublishConfigurations(_x12, _x13, _x14, _x15, _x16) {
195 return _ref5.apply(this, arguments);
196 };
197})();
198
199let getResolvedPublishConfig = (() => {
200 var _ref6 = (0, _bluebirdLst().coroutine)(function* (platformPackager, packager, options, arch, errorIfCannot) {
201 let getInfo = (() => {
202 var _ref7 = (0, _bluebirdLst().coroutine)(function* () {
203 const info = yield packager.repositoryInfo;
204
205 if (info != null) {
206 return info;
207 }
208
209 const message = `Cannot detect repository by .git/config. Please specify "repository" in the package.json (https://docs.npmjs.com/files/package.json#repository).\nPlease see https://electron.build/configuration/publish`;
210
211 if (errorIfCannot) {
212 throw new Error(message);
213 } else {
214 _builderUtil().log.warn(message);
215
216 return null;
217 }
218 });
219
220 return function getInfo() {
221 return _ref7.apply(this, arguments);
222 };
223 })();
224
225 options = Object.assign({}, options);
226 expandPublishConfig(options, platformPackager, packager, arch);
227 let channelFromAppVersion = null;
228
229 if (options.channel == null && isDetectUpdateChannel(platformPackager == null ? null : platformPackager.platformSpecificBuildOptions, packager.config)) {
230 channelFromAppVersion = packager.appInfo.channel;
231 }
232
233 const provider = options.provider;
234
235 if (provider === "generic") {
236 const o = options;
237
238 if (o.url == null) {
239 throw new (_builderUtil().InvalidConfigurationError)(`Please specify "url" for "generic" update server`);
240 }
241
242 if (channelFromAppVersion != null) {
243 o.channel = channelFromAppVersion;
244 }
245
246 return options;
247 }
248
249 const providerClass = requireProviderClass(options.provider);
250
251 if (providerClass != null && providerClass.checkAndResolveOptions != null) {
252 yield providerClass.checkAndResolveOptions(options, channelFromAppVersion, errorIfCannot);
253 return options;
254 }
255
256 const isGithub = provider === "github";
257
258 if (!isGithub && provider !== "bintray") {
259 return options;
260 }
261
262 let owner = isGithub ? options.owner : options.owner;
263 let project = isGithub ? options.repo : options.package;
264
265 if (isGithub && owner == null && project != null) {
266 const index = project.indexOf("/");
267
268 if (index > 0) {
269 const repo = project;
270 project = repo.substring(0, index);
271 owner = repo.substring(index + 1);
272 }
273 }
274
275 if (!owner || !project) {
276 _builderUtil().log.debug({
277 reason: "owner or project is not specified explicitly",
278 provider,
279 owner,
280 project
281 }, "calling getInfo");
282
283 const info = yield getInfo();
284
285 if (info == null) {
286 return null;
287 }
288
289 if (!owner) {
290 owner = info.user;
291 }
292
293 if (!project) {
294 project = info.project;
295 }
296 }
297
298 if (isGithub) {
299 if (options.token != null && !options.private) {
300 _builderUtil().log.warn('"token" specified in the github publish options. It should be used only for [setFeedURL](module:electron-updater/out/AppUpdater.AppUpdater+setFeedURL).');
301 } //tslint:disable-next-line:no-object-literal-type-assertion
302
303
304 return Object.assign({
305 owner,
306 repo: project
307 }, options);
308 } else {
309 //tslint:disable-next-line:no-object-literal-type-assertion
310 return Object.assign({
311 owner,
312 package: project
313 }, options);
314 }
315 });
316
317 return function getResolvedPublishConfig(_x17, _x18, _x19, _x20, _x21) {
318 return _ref6.apply(this, arguments);
319 };
320})(); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
321
322function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
323
324const publishForPrWarning = "There are serious security concerns with PUBLISH_FOR_PULL_REQUEST=true (see the CircleCI documentation (https://circleci.com/docs/1.0/fork-pr-builds/) for details)" + "\nIf you have SSH keys, sensitive env vars or AWS credentials stored in your project settings and untrusted forks can make pull requests against your repo, then this option isn't for you.";
325const debug = (0, _debug2.default)("electron-builder:publish");
326
327class PublishManager {
328 constructor(packager, publishOptions, cancellationToken = packager.cancellationToken) {
329 var _this = this;
330
331 this.packager = packager;
332 this.publishOptions = publishOptions;
333 this.cancellationToken = cancellationToken;
334 this.nameToPublisher = new Map();
335 this.isPublish = false;
336 this.progress = process.stdout.isTTY ? new (_multiProgress().MultiProgress)() : null;
337 this.updateFileWriteTask = [];
338 this.taskManager = new (_builderUtil().AsyncTaskManager)(cancellationToken);
339 const forcePublishForPr = process.env.PUBLISH_FOR_PULL_REQUEST === "true";
340
341 if (!(0, _builderUtil().isPullRequest)() || forcePublishForPr) {
342 if (publishOptions.publish === undefined) {
343 if (process.env.npm_lifecycle_event === "release") {
344 publishOptions.publish = "always";
345 } else {
346 const tag = (0, _electronPublish().getCiTag)();
347
348 if (tag != null) {
349 _builderUtil().log.info({
350 reason: "tag is defined",
351 tag
352 }, "artifacts will be published");
353
354 publishOptions.publish = "onTag";
355 } else if (_isCi().default) {
356 _builderUtil().log.info({
357 reason: "CI detected"
358 }, "artifacts will be published if draft release exists");
359
360 publishOptions.publish = "onTagOrDraft";
361 }
362 }
363 }
364
365 const publishPolicy = publishOptions.publish;
366 this.isPublish = publishPolicy != null && publishOptions.publish !== "never" && (publishPolicy !== "onTag" || (0, _electronPublish().getCiTag)() != null);
367
368 if (this.isPublish && forcePublishForPr) {
369 _builderUtil().log.warn(publishForPrWarning);
370 }
371 } else if (publishOptions.publish !== "never") {
372 _builderUtil().log.info({
373 reason: "current build is a part of pull request",
374 solution: `set env PUBLISH_FOR_PULL_REQUEST to true to force code signing\n${publishForPrWarning}`
375 }, "publishing will be skipped");
376 }
377
378 packager.addAfterPackHandler((() => {
379 var _ref = (0, _bluebirdLst().coroutine)(function* (event) {
380 const packager = event.packager;
381
382 if (event.electronPlatformName === "darwin") {
383 if (!event.targets.some(it => it.name === "dmg" || it.name === "zip")) {
384 return;
385 }
386 } else if (packager.platform === _index().Platform.WINDOWS) {
387 if (!event.targets.some(it => isSuitableWindowsTarget(it))) {
388 return;
389 }
390 } else {
391 return;
392 }
393
394 const publishConfig = yield getAppUpdatePublishConfiguration(packager, event.arch, _this.isPublish);
395
396 if (publishConfig != null) {
397 yield (0, _fsExtraP().writeFile)(path.join(packager.getResourcesDir(event.appOutDir), "app-update.yml"), (0, _builderUtil().serializeToYaml)(publishConfig));
398 }
399 });
400
401 return function (_x) {
402 return _ref.apply(this, arguments);
403 };
404 })());
405 packager.artifactCreated(event => {
406 const publishConfiguration = event.publishConfig;
407
408 if (publishConfiguration == null) {
409 this.taskManager.addTask(this.artifactCreatedWithoutExplicitPublishConfig(event));
410 } else if (this.isPublish) {
411 if (debug.enabled) {
412 debug(`artifactCreated (isPublish: ${this.isPublish}): ${(0, _builderUtil().safeStringifyJson)(event, new Set(["packager"]))},\n publishConfig: ${(0, _builderUtil().safeStringifyJson)(publishConfiguration)}`);
413 }
414
415 this.scheduleUpload(publishConfiguration, event);
416 }
417 });
418 }
419
420 getGlobalPublishConfigurations() {
421 var _this2 = this;
422
423 return (0, _bluebirdLst().coroutine)(function* () {
424 const publishers = _this2.packager.config.publish;
425 return yield resolvePublishConfigurations(publishers, null, _this2.packager, null, true);
426 })();
427 }
428 /** @internal */
429
430
431 scheduleUpload(publishConfig, event) {
432 if (publishConfig.provider === "generic") {
433 return;
434 }
435
436 const publisher = this.getOrCreatePublisher(publishConfig);
437
438 if (publisher == null) {
439 _builderUtil().log.debug({
440 file: event.file,
441 reason: "publisher is null",
442 publishConfig: (0, _builderUtil().safeStringifyJson)(publishConfig)
443 }, "not published");
444
445 return;
446 }
447
448 const providerName = publisher.providerName;
449
450 if (this.publishOptions.publish === "onTagOrDraft" && (0, _electronPublish().getCiTag)() == null && !(providerName === "GitHub" || providerName === "Bintray")) {
451 _builderUtil().log.info({
452 file: event.file,
453 reason: "current build is not for a git tag",
454 publishPolicy: "onTagOrDraft"
455 }, `not published to ${providerName}`);
456
457 return;
458 }
459
460 this.taskManager.addTask(publisher.upload(event));
461 }
462
463 artifactCreatedWithoutExplicitPublishConfig(event) {
464 var _this3 = this;
465
466 return (0, _bluebirdLst().coroutine)(function* () {
467 const platformPackager = event.packager;
468 const target = event.target;
469 const publishConfigs = yield getPublishConfigs(platformPackager, target == null ? null : target.options, event.arch, _this3.isPublish);
470
471 if (debug.enabled) {
472 debug(`artifactCreated (isPublish: ${_this3.isPublish}): ${(0, _builderUtil().safeStringifyJson)(event, new Set(["packager"]))},\n publishConfigs: ${(0, _builderUtil().safeStringifyJson)(publishConfigs)}`);
473 }
474
475 const eventFile = event.file;
476
477 if (publishConfigs == null) {
478 if (_this3.isPublish) {
479 _builderUtil().log.debug({
480 file: eventFile,
481 reason: "no publish configs"
482 }, "not published");
483 }
484
485 return;
486 }
487
488 if (_this3.isPublish) {
489 for (const publishConfig of publishConfigs) {
490 if (_this3.cancellationToken.cancelled) {
491 _builderUtil().log.debug({
492 file: event.file,
493 reason: "cancelled"
494 }, "not published");
495
496 break;
497 }
498
499 _this3.scheduleUpload(publishConfig, event);
500 }
501 }
502
503 if (event.isWriteUpdateInfo && target != null && eventFile != null && !_this3.cancellationToken.cancelled && (platformPackager.platform !== _index().Platform.WINDOWS || isSuitableWindowsTarget(target))) {
504 _this3.taskManager.addTask((0, _updateInfoBuilder().createUpdateInfoTasks)(event, publishConfigs).then(it => _this3.updateFileWriteTask.push(...it)));
505 }
506 })();
507 }
508
509 getOrCreatePublisher(publishConfig) {
510 // to not include token into cache key
511 const providerCacheKey = (0, _builderUtil().safeStringifyJson)(publishConfig);
512 let publisher = this.nameToPublisher.get(providerCacheKey);
513
514 if (publisher == null) {
515 publisher = createPublisher(this, this.packager.appInfo.version, publishConfig, this.publishOptions);
516 this.nameToPublisher.set(providerCacheKey, publisher);
517
518 _builderUtil().log.info({
519 publisher: publisher.toString()
520 }, "publishing");
521 }
522
523 return publisher;
524 } // noinspection JSUnusedGlobalSymbols
525
526
527 cancelTasks() {
528 this.taskManager.cancelTasks();
529 this.nameToPublisher.clear();
530 }
531
532 awaitTasks() {
533 var _this4 = this;
534
535 return (0, _bluebirdLst().coroutine)(function* () {
536 yield _this4.taskManager.awaitTasks();
537 const updateInfoFileTasks = _this4.updateFileWriteTask;
538
539 if (_this4.cancellationToken.cancelled || updateInfoFileTasks.length === 0) {
540 return;
541 }
542
543 yield (0, _updateInfoBuilder().writeUpdateInfoFiles)(updateInfoFileTasks, _this4.packager);
544 yield _this4.taskManager.awaitTasks();
545 })();
546 }
547
548}
549
550exports.PublishManager = PublishManager;
551
552let getAppUpdatePublishConfiguration = (() => {
553 var _ref2 = (0, _bluebirdLst().coroutine)(function* (packager, arch, errorIfCannot) {
554 const publishConfigs = yield getPublishConfigsForUpdateInfo(packager, (yield getPublishConfigs(packager, null, arch, errorIfCannot)), arch);
555
556 if (publishConfigs == null || publishConfigs.length === 0) {
557 return null;
558 }
559
560 let publishConfig = publishConfigs[0];
561
562 if (packager.platform === _index().Platform.WINDOWS && publishConfig.publisherName == null) {
563 const winPackager = packager;
564
565 if (winPackager.isForceCodeSigningVerification) {
566 const publisherName = yield winPackager.computedPublisherName.value;
567
568 if (publisherName != null) {
569 publishConfig = Object.assign({}, publishConfig, {
570 publisherName
571 });
572 }
573 }
574 }
575
576 return publishConfig;
577 });
578
579 return function getAppUpdatePublishConfiguration(_x2, _x3, _x4) {
580 return _ref2.apply(this, arguments);
581 };
582})();
583
584exports.getAppUpdatePublishConfiguration = getAppUpdatePublishConfiguration;
585
586let getPublishConfigsForUpdateInfo = (() => {
587 var _ref3 = (0, _bluebirdLst().coroutine)(function* (packager, publishConfigs, arch) {
588 if (publishConfigs === null) {
589 return null;
590 }
591
592 if (publishConfigs.length === 0) {
593 _builderUtil().log.debug(null, "getPublishConfigsForUpdateInfo: no publishConfigs, detect using repository info"); // https://github.com/electron-userland/electron-builder/issues/925#issuecomment-261732378
594 // default publish config is github, file should be generated regardless of publish state (user can test installer locally or manage the release process manually)
595
596
597 const repositoryInfo = yield packager.info.repositoryInfo;
598 debug(`getPublishConfigsForUpdateInfo: ${(0, _builderUtil().safeStringifyJson)(repositoryInfo)}`);
599
600 if (repositoryInfo != null && repositoryInfo.type === "github") {
601 const resolvedPublishConfig = yield getResolvedPublishConfig(packager, packager.info, {
602 provider: repositoryInfo.type
603 }, arch, false);
604
605 if (resolvedPublishConfig != null) {
606 debug(`getPublishConfigsForUpdateInfo: resolve to publish config ${(0, _builderUtil().safeStringifyJson)(resolvedPublishConfig)}`);
607 return [resolvedPublishConfig];
608 }
609 }
610 }
611
612 return publishConfigs;
613 });
614
615 return function getPublishConfigsForUpdateInfo(_x5, _x6, _x7) {
616 return _ref3.apply(this, arguments);
617 };
618})();
619
620exports.getPublishConfigsForUpdateInfo = getPublishConfigsForUpdateInfo;
621
622function createPublisher(context, version, publishConfig, options) {
623 if (debug.enabled) {
624 debug(`Create publisher: ${(0, _builderUtil().safeStringifyJson)(publishConfig)}`);
625 }
626
627 const provider = publishConfig.provider;
628
629 switch (provider) {
630 case "github":
631 return new (_gitHubPublisher().GitHubPublisher)(context, publishConfig, version, options);
632
633 case "bintray":
634 return new (_BintrayPublisher().BintrayPublisher)(context, publishConfig, version, options);
635
636 case "generic":
637 return null;
638
639 default:
640 const clazz = requireProviderClass(provider);
641 return clazz == null ? null : new clazz(context, publishConfig);
642 }
643}
644
645function requireProviderClass(provider) {
646 switch (provider) {
647 case "github":
648 return _gitHubPublisher().GitHubPublisher;
649
650 case "bintray":
651 return _BintrayPublisher().BintrayPublisher;
652
653 case "generic":
654 return null;
655
656 case "s3":
657 return _s3Publisher().default;
658
659 case "spaces":
660 return _spacesPublisher().default;
661
662 default:
663 return require(`electron-publisher-${provider}`).default;
664 }
665}
666
667function computeDownloadUrl(publishConfiguration, fileName, packager) {
668 if (publishConfiguration.provider === "generic") {
669 const baseUrlString = publishConfiguration.url;
670
671 if (fileName == null) {
672 return baseUrlString;
673 }
674
675 const baseUrl = url().parse(baseUrlString);
676 return url().format(Object.assign({}, baseUrl, {
677 pathname: path.posix.resolve(baseUrl.pathname || "/", encodeURI(fileName))
678 }));
679 }
680
681 let baseUrl;
682
683 if (publishConfiguration.provider === "github") {
684 const gh = publishConfiguration;
685 baseUrl = `${(0, _builderUtilRuntime().githubUrl)(gh)}/${gh.owner}/${gh.repo}/releases/download/${gh.vPrefixedTagName === false ? "" : "v"}${packager.appInfo.version}`;
686 } else {
687 baseUrl = (0, _builderUtilRuntime().getS3LikeProviderBaseUrl)(publishConfiguration);
688 }
689
690 if (fileName == null) {
691 return baseUrl;
692 }
693
694 return `${baseUrl}/${encodeURI(fileName)}`;
695}
696
697let getPublishConfigs = (() => {
698 var _ref4 = (0, _bluebirdLst().coroutine)(function* (platformPackager, targetSpecificOptions, arch, errorIfCannot) {
699 let publishers; // check build.nsis (target)
700
701 if (targetSpecificOptions != null) {
702 publishers = targetSpecificOptions.publish; // if explicitly set to null - do not publish
703
704 if (publishers === null) {
705 return null;
706 }
707 } // check build.win (platform)
708
709
710 if (publishers == null) {
711 publishers = platformPackager.platformSpecificBuildOptions.publish;
712
713 if (publishers === null) {
714 return null;
715 }
716 }
717
718 if (publishers == null) {
719 publishers = platformPackager.config.publish;
720
721 if (publishers === null) {
722 return null;
723 }
724 }
725
726 return yield resolvePublishConfigurations(publishers, platformPackager, platformPackager.info, arch, errorIfCannot);
727 });
728
729 return function getPublishConfigs(_x8, _x9, _x10, _x11) {
730 return _ref4.apply(this, arguments);
731 };
732})();
733
734exports.getPublishConfigs = getPublishConfigs;
735
736function isSuitableWindowsTarget(target) {
737 if (target.name === "appx" && target.options != null && target.options.electronUpdaterAware) {
738 return true;
739 }
740
741 return target.name === "nsis" || target.name.startsWith("nsis-");
742}
743
744function expandPublishConfig(options, platformPackager, packager, arch) {
745 for (const name of Object.keys(options)) {
746 const value = options[name];
747
748 if (typeof value === "string") {
749 const archValue = arch == null ? null : _builderUtil().Arch[arch];
750 const expanded = platformPackager == null ? (0, _macroExpander().expandMacro)(value, archValue, packager.appInfo) : platformPackager.expandMacro(value, archValue);
751
752 if (expanded !== value) {
753 options[name] = expanded;
754 }
755 }
756 }
757}
758
759function isDetectUpdateChannel(platformSpecificConfiguration, configuration) {
760 const value = platformSpecificConfiguration == null ? null : platformSpecificConfiguration.detectUpdateChannel;
761 return value == null ? configuration.detectUpdateChannel !== false : value;
762}
763//# sourceMappingURL=PublishManager.js.map
\No newline at end of file