-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
PublishManager.ts
515 lines (445 loc) · 17.1 KB
/
PublishManager.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
import BluebirdPromise from "bluebird-lst"
import { createHash } from "crypto"
import { Platform, PlatformSpecificBuildOptions, Target } from "electron-builder-core"
import { CancellationToken } from "electron-builder-http/out/CancellationToken"
import { BintrayOptions, GenericServerOptions, GithubOptions, githubUrl, PublishConfiguration, PublishProvider, S3Options, s3Url, UpdateInfo, VersionInfo } from "electron-builder-http/out/publishOptions"
import { asArray, debug, isEmptyOrSpaces, isPullRequest } from "electron-builder-util"
import { log, warn } from "electron-builder-util/out/log"
import { throwError } from "electron-builder-util/out/promise"
import { HttpPublisher, PublishContext, Publisher, PublishOptions } from "electron-publish"
import { BintrayPublisher } from "electron-publish/out/BintrayPublisher"
import { GitHubPublisher } from "electron-publish/out/gitHubPublisher"
import { MultiProgress } from "electron-publish/out/multiProgress"
import { createReadStream, ensureDir, outputJson, writeFile } from "fs-extra-p"
import isCi from "is-ci"
import { safeDump } from "js-yaml"
import * as path from "path"
import * as url from "url"
import { Packager } from "../packager"
import { ArtifactCreated, BuildInfo } from "../packagerApi"
import { PlatformPackager } from "../platformPackager"
import { WinPackager } from "../winPackager"
export class PublishManager implements PublishContext {
private readonly nameToPublisher = new Map<string, Publisher | null>()
readonly publishTasks: Array<Promise<any>> = []
private readonly errors: Array<Error> = []
private isPublish = false
readonly progress = (<NodeJS.WritableStream>process.stdout).isTTY ? new MultiProgress() : null
constructor(packager: Packager, private readonly publishOptions: PublishOptions, readonly cancellationToken: CancellationToken) {
if (!isPullRequest()) {
if (publishOptions.publish === undefined) {
if (process.env.npm_lifecycle_event === "release") {
publishOptions.publish = "always"
}
else {
const tag = getCiTag()
if (tag != null) {
log(`Tag ${tag} is defined, so artifacts will be published`)
publishOptions.publish = "onTag"
}
else if (isCi) {
log("CI detected, so artifacts will be published if draft release exists")
publishOptions.publish = "onTagOrDraft"
}
}
}
if (publishOptions.publish != null && publishOptions.publish !== "never") {
this.isPublish = publishOptions.publish !== "onTag" || getCiTag() != null
}
}
else if (publishOptions.publish !== "never") {
log("Current build is a part of pull request, publishing will be skipped")
}
packager.addAfterPackHandler(async event => {
const packager = event.packager
if (event.electronPlatformName === "darwin") {
if (!event.targets.some(it => it.name === "zip")) {
return
}
}
else if (packager.platform === Platform.WINDOWS) {
if (!event.targets.some(it => isSuitableWindowsTarget(it))) {
return
}
}
else {
return
}
const publishConfigs = await getPublishConfigsForUpdateInfo(packager, await getPublishConfigs(packager, null))
if (publishConfigs == null || publishConfigs.length === 0) {
return
}
let publishConfig = publishConfigs[0]
if (packager.platform === Platform.WINDOWS) {
const publisherName = await (<WinPackager>packager).computedPublisherName.value
if (publisherName != null) {
publishConfig = Object.assign({publisherName: publisherName}, publishConfig)
}
}
const providerClass = requireProviderClass(publishConfig.provider)
if (providerClass != null && providerClass.modifyPublishConfig != null) {
publishConfig = await providerClass.modifyPublishConfig(publishConfig)
}
await writeFile(path.join(packager.getResourcesDir(event.appOutDir), "app-update.yml"), safeDump(publishConfig))
})
packager.artifactCreated(event => this.addTask(this.artifactCreated(event)))
}
private async artifactCreated(event: ArtifactCreated) {
const packager = event.packager
const target = event.target
const publishConfigs = event.publishConfig == null ? await getPublishConfigs(packager, target == null ? null : target.options) : [event.publishConfig]
const eventFile = event.file
if (publishConfigs == null) {
if (this.isPublish) {
debug(`${eventFile} is not published: no publish configs`)
}
return
}
if (this.isPublish) {
for (const publishConfig of publishConfigs) {
if (this.cancellationToken.cancelled) {
break
}
const publisher = this.getOrCreatePublisher(publishConfig, packager.info)
if (publisher != null) {
if (eventFile == null) {
this.addTask((<HttpPublisher>publisher).uploadData(event.data!, event.safeArtifactName!))
}
else {
this.addTask(publisher.upload(eventFile!, event.safeArtifactName))
}
}
}
}
if (target != null && eventFile != null && !this.cancellationToken.cancelled) {
if ((packager.platform === Platform.MAC && target.name === "zip") ||
(packager.platform === Platform.WINDOWS && isSuitableWindowsTarget(target) && eventFile.endsWith(".exe"))) {
this.addTask(writeUpdateInfo(event, publishConfigs))
}
}
}
private addTask(promise: Promise<any>) {
if (this.cancellationToken.cancelled) {
return
}
this.publishTasks.push(promise
.catch(it => this.errors.push(it)))
}
getOrCreatePublisher(publishConfig: PublishConfiguration, buildInfo: BuildInfo): Publisher | null {
let publisher = this.nameToPublisher.get(publishConfig.provider)
if (publisher == null) {
publisher = createPublisher(this, buildInfo.metadata.version!, publishConfig, this.publishOptions)
this.nameToPublisher.set(publishConfig.provider, publisher)
log(`Publishing to ${publisher}`)
}
return publisher
}
cancelTasks() {
for (const task of this.publishTasks) {
if ("cancel" in task) {
(<any>task).cancel()
}
}
this.publishTasks.length = 0
this.nameToPublisher.clear()
}
async awaitTasks() {
if (this.errors.length > 0) {
this.cancelTasks()
throwError(this.errors)
return
}
const publishTasks = this.publishTasks
let list = publishTasks.slice()
publishTasks.length = 0
while (list.length > 0) {
await BluebirdPromise.all(list)
if (publishTasks.length === 0) {
break
}
else {
list = publishTasks.slice()
publishTasks.length = 0
}
}
}
}
export async function getPublishConfigsForUpdateInfo(packager: PlatformPackager<any>, publishConfigs: Array<PublishConfiguration> | null): Promise<Array<PublishConfiguration> | null> {
if (publishConfigs === null) {
return null
}
if (publishConfigs.length === 0) {
debug("No publishConfigs, detect using repository info")
// https://github.com/electron-userland/electron-builder/issues/925#issuecomment-261732378
// default publish config is github, file should be generated regardless of publish state (user can test installer locally or manage the release process manually)
const repositoryInfo = await packager.info.repositoryInfo
if (repositoryInfo != null && repositoryInfo.type === "github") {
const resolvedPublishConfig = await getResolvedPublishConfig(packager.info, {provider: repositoryInfo.type}, false)
if (resolvedPublishConfig != null) {
return [resolvedPublishConfig]
}
}
}
return publishConfigs
}
async function writeUpdateInfo(event: ArtifactCreated, _publishConfigs: Array<PublishConfiguration>) {
const packager = event.packager
const publishConfigs = await getPublishConfigsForUpdateInfo(packager, _publishConfigs)
if (publishConfigs == null || publishConfigs.length === 0) {
return
}
const target = event.target!
let outDir = target.outDir
if (target.name.startsWith("nsis-")) {
outDir = path.join(outDir, target.name)
await ensureDir(outDir)
}
for (const publishConfig of publishConfigs) {
const isGitHub = publishConfig.provider === "github"
if (!(publishConfig.provider === "generic" || publishConfig.provider === "s3" || isGitHub)) {
continue
}
const version = packager.appInfo.version
const channel = (<GenericServerOptions>publishConfig).channel || "latest"
if (packager.platform === Platform.MAC) {
const updateInfoFile = isGitHub ? path.join(outDir, "github", `${channel}-mac.json`) : path.join(outDir, `${channel}-mac.json`)
await (<any>outputJson)(updateInfoFile, <VersionInfo>{
version: version,
releaseDate: new Date().toISOString(),
url: computeDownloadUrl(publishConfig, packager.generateName2("zip", "mac", isGitHub), packager),
}, {spaces: 2})
packager.info.dispatchArtifactCreated({
file: updateInfoFile,
packager: packager,
target: null,
publishConfig: publishConfig,
})
}
else {
await writeWindowsUpdateInfo(event, version, outDir, channel, publishConfigs)
break
}
}
}
async function writeWindowsUpdateInfo(event: ArtifactCreated, version: string, outDir: any, channel: string, publishConfigs: Array<PublishConfiguration>): Promise<void> {
const packager = event.packager
const sha2 = await sha256(event.file!)
const updateInfoFile = path.join(outDir, `${channel}.yml`)
await writeFile(updateInfoFile, safeDump(<UpdateInfo>{
version: version,
releaseDate: new Date().toISOString(),
githubArtifactName: event.safeArtifactName,
path: path.basename(event.file!),
sha2: sha2,
}))
const githubPublishConfig = publishConfigs.find(it => it.provider === "github")
if (githubPublishConfig != null) {
// to preserve compatibility with old electron-updater (< 0.10.0), we upload file with path specific for GitHub
packager.info.dispatchArtifactCreated({
data: new Buffer(safeDump(<UpdateInfo>{
version: version,
path: event.safeArtifactName,
sha2: sha2,
})),
safeArtifactName: `${channel}.yml`,
packager: packager,
target: null,
publishConfig: githubPublishConfig,
})
}
const genericPublishConfig = publishConfigs.find(it => it.provider === "generic" || it.provider === "s3")
if (genericPublishConfig != null) {
packager.info.dispatchArtifactCreated({
file: updateInfoFile,
packager: packager,
target: null,
publishConfig: genericPublishConfig,
})
}
}
export function createPublisher(context: PublishContext, version: string, publishConfig: PublishConfiguration, options: PublishOptions): Publisher | null {
const provider = publishConfig.provider
switch (provider) {
case "github":
return new GitHubPublisher(context, publishConfig, version, options)
case "bintray":
return new BintrayPublisher(context, publishConfig, version, options)
case "generic":
return null
default:
const clazz = requireProviderClass(provider)
return clazz == null ? null : new clazz(context, publishConfig)
}
}
function requireProviderClass(provider: string): any | null {
switch (provider) {
case "github":
return GitHubPublisher
case "bintray":
return BintrayPublisher
case "generic":
return null
default:
return require(`electron-publisher-${provider}`).default
}
}
export function computeDownloadUrl(publishConfig: PublishConfiguration, fileName: string | null, packager: PlatformPackager<any>) {
if (publishConfig.provider === "generic") {
const baseUrlString = (<GenericServerOptions>publishConfig).url
if (fileName == null) {
return baseUrlString
}
const baseUrl = url.parse(baseUrlString)
return url.format(Object.assign({}, baseUrl, {pathname: path.posix.resolve(baseUrl.pathname || "/", encodeURI(fileName))}))
}
let baseUrl
if (publishConfig.provider === "s3") {
baseUrl = s3Url((<S3Options>publishConfig))
}
else {
const gh = <GithubOptions>publishConfig
baseUrl = `${githubUrl(gh)}/${gh.owner}/${gh.repo}/releases/download/v${packager.appInfo.version}`
}
if (fileName == null) {
return baseUrl
}
return `${baseUrl}/${encodeURI(fileName)}`
}
export async function getPublishConfigs(packager: PlatformPackager<any>, targetSpecificOptions: PlatformSpecificBuildOptions | null | undefined): Promise<Array<PublishConfiguration> | null> {
let publishers
// check build.nsis (target)
if (targetSpecificOptions != null) {
publishers = targetSpecificOptions.publish
// if explicitly set to null - do not publish
if (publishers === null) {
return null
}
}
// check build.win (platform)
if (publishers == null) {
publishers = packager.platformSpecificBuildOptions.publish
if (publishers === null) {
return null
}
}
if (publishers == null) {
publishers = packager.config.publish
if (publishers === null) {
return null
}
}
if (publishers == null) {
let serviceName: PublishProvider | null = null
if (!isEmptyOrSpaces(process.env.GH_TOKEN)) {
serviceName = "github"
}
else if (!isEmptyOrSpaces(process.env.BT_TOKEN)) {
serviceName = "bintray"
}
if (serviceName != null) {
debug(`Detect ${serviceName} as publish provider`)
return [(await getResolvedPublishConfig(packager.info, {provider: serviceName}))!]
}
}
if (publishers == null) {
return []
}
debug(`Explicit publish provider: ${JSON.stringify(publishers, null, 2)}`)
return await (<Promise<Array<PublishConfiguration>>>BluebirdPromise.map(asArray(publishers), it => getResolvedPublishConfig(packager.info, typeof it === "string" ? {provider: it} : it)))
.then(publishConfigs => expandPublishConfigs(packager, publishConfigs))
}
function expandPublishConfigs(packager: PlatformPackager<any>, publishConfigs: Array<PublishConfiguration>) {
return publishConfigs.map(publishConfig => expandPublishConfig(packager, publishConfig))
}
function expandPublishConfig(packager: PlatformPackager<any>, publishConfig: any): PublishConfiguration {
return <PublishConfiguration>Object.keys(publishConfig).reduce((expandedPublishConfig: {[key: string]: string}, key) => {
const option = publishConfig[key]
if (option != null) {
expandedPublishConfig[key] = packager.expandMacro(option, null)
}
return expandedPublishConfig
}, {})
}
function sha256(file: string) {
return new BluebirdPromise<string>((resolve, reject) => {
const hash = createHash("sha256")
hash
.on("error", reject)
.setEncoding("hex")
createReadStream(file)
.on("error", reject)
.on("end", () => {
hash.end()
resolve(<string>hash.read())
})
.pipe(hash, {end: false})
})
}
function isSuitableWindowsTarget(target: Target) {
return target.name === "nsis" || target.name.startsWith("nsis-")
}
function getCiTag() {
const tag = process.env.TRAVIS_TAG || process.env.APPVEYOR_REPO_TAG_NAME || process.env.CIRCLE_TAG || process.env.CI_BUILD_TAG
return tag != null && tag.length > 0 ? tag : null
}
async function getResolvedPublishConfig(packager: BuildInfo, options: PublishConfiguration, errorIfCannot: boolean = true): Promise<PublishConfiguration | null> {
const provider = options.provider
if (provider === "generic") {
if ((<GenericServerOptions>options).url == null) {
throw new Error(`Please specify "url" for "generic" update server`)
}
return options
}
const providerClass = requireProviderClass(options.provider)
if (providerClass != null && providerClass.checkPublishConfig != null) {
providerClass.checkPublishConfig(options)
return options
}
const isGithub = provider === "github"
if (!isGithub && provider !== "bintray") {
return options
}
let owner = options.owner
let project = isGithub ? (<GithubOptions>options).repo : (<BintrayOptions>options).package
if (isGithub && owner == null && project != null) {
const index = project.indexOf("/")
if (index > 0) {
const repo = project
project = repo.substring(0, index)
owner = repo.substring(index + 1)
}
}
async function getInfo() {
const info = await packager.repositoryInfo
if (info != null) {
return info
}
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://github.com/electron-userland/electron-builder/wiki/Publishing-Artifacts`
if (errorIfCannot) {
throw new Error(message)
}
else {
warn(message)
return null
}
}
if (!owner || !project) {
debug(`No owner or project for ${provider}, call getInfo: owner: ${owner}, project: ${project}`)
const info = await getInfo()
if (info == null) {
return null
}
if (!owner) {
owner = info.user
}
if (!project) {
project = info.project
}
}
if (isGithub) {
return Object.assign({owner, repo: project}, options)
}
else {
return Object.assign({owner, package: project}, options)
}
}