@@ -4,32 +4,16 @@ import { PublishOptions, Publisher } from "./publish/publisher"
4
4
import { GitHubPublisher } from "./publish/gitHubPublisher"
5
5
import { executeFinally } from "./util/promise"
6
6
import { Promise as BluebirdPromise } from "bluebird"
7
- import { isEmptyOrSpaces , isCi } from "./util/util"
7
+ import { isEmptyOrSpaces , isCi , asArray , debug } from "./util/util"
8
8
import { log , warn } from "./util/log"
9
9
import { Platform , Arch , archFromString } from "./metadata"
10
10
import { getRepositoryInfo } from "./repositoryInfo"
11
11
import { DIR_TARGET } from "./targets/targetFactory"
12
+ import { BintrayPublisher , BintrayConfiguration } from "./publish/BintrayPublisher"
12
13
13
14
//noinspection JSUnusedLocalSymbols
14
15
const __awaiter = require ( "./util/awaiter" )
15
16
16
- export async function createPublisher ( packager : Packager , options : PublishOptions , isPublishOptionGuessed : boolean = false ) : Promise < Publisher | null > {
17
- const info = await getRepositoryInfo ( packager . metadata , packager . devMetadata )
18
- if ( info == null ) {
19
- if ( isPublishOptionGuessed ) {
20
- return null
21
- }
22
-
23
- warn ( "Cannot detect repository by .git/config" )
24
- throw new Error ( `Please specify 'repository' in the dev package.json ('${ packager . devPackageFile } ')` )
25
- }
26
- else {
27
- const version = packager . metadata . version !
28
- log ( `Creating Github Publisher — user: ${ info . user } , project: ${ info . project } , version: ${ version } ` )
29
- return new GitHubPublisher ( info . user , info . project , version , options , isPublishOptionGuessed )
30
- }
31
- }
32
-
33
17
export interface BuildOptions extends PackagerOptions , PublishOptions {
34
18
}
35
19
@@ -210,6 +194,9 @@ export async function build(rawOptions?: CliOptions): Promise<void> {
210
194
if ( options . githubToken === undefined && ! isEmptyOrSpaces ( process . env . GH_TOKEN ) ) {
211
195
options . githubToken = process . env . GH_TOKEN
212
196
}
197
+ if ( options . bintrayToken === undefined && ! isEmptyOrSpaces ( process . env . BT_TOKEN ) ) {
198
+ options . bintrayToken = process . env . BT_TOKEN
199
+ }
213
200
214
201
if ( options . draft === undefined && ! isEmptyOrSpaces ( process . env . EP_DRAFT ) ) {
215
202
options . draft = process . env . EP_DRAFT . toLowerCase ( ) === "true"
@@ -238,24 +225,16 @@ export async function build(rawOptions?: CliOptions): Promise<void> {
238
225
}
239
226
}
240
227
241
- if ( options . publish !== "never" && options . githubToken == null && isCi ( ) ) {
242
- log ( `CI detected, publish is set to ${ options . publish } , but GH_TOKEN is not set, so artifacts will be not published` )
243
- }
244
-
245
- const publishTasks : Array < BluebirdPromise < any > > = [ ]
246
228
const packager = new Packager ( options )
247
- if ( options . publish != null && options . publish !== "never" ) {
248
- let publisher : Promise < Publisher > | null = null
249
- packager . artifactCreated ( event => {
250
- if ( publisher == null ) {
251
- publisher = createPublisher ( packager , options , isPublishOptionGuessed )
252
- }
229
+ const publishTasks : Array < BluebirdPromise < any > > = [ ]
253
230
254
- if ( publisher ) {
255
- publisher
256
- . then ( it => publishTasks . push ( < BluebirdPromise < any > > it . upload ( event . file , event . artifactName ) ) )
257
- }
258
- } )
231
+ if ( options . publish != null && options . publish !== "never" ) {
232
+ if ( options . githubToken != null || options . bintrayToken != null ) {
233
+ publishManager ( packager , publishTasks , options , isPublishOptionGuessed )
234
+ }
235
+ else if ( isCi ( ) ) {
236
+ log ( `CI detected, publish is set to ${ options . publish } , but neither GH_TOKEN nor BT_TOKEN is not set, so artifacts will be not published` )
237
+ }
259
238
}
260
239
261
240
await executeFinally ( packager . build ( ) , errorOccurred => {
@@ -270,3 +249,69 @@ export async function build(rawOptions?: CliOptions): Promise<void> {
270
249
}
271
250
} )
272
251
}
252
+
253
+ function publishManager ( packager : Packager , publishTasks : Array < BluebirdPromise < any > > , options : BuildOptions , isPublishOptionGuessed : boolean ) {
254
+ const nameToPublisher = new Map < string , Promise < Publisher > > ( )
255
+ packager . artifactCreated ( event => {
256
+ let publishers = event . packager . platformSpecificBuildOptions . publish
257
+ // if explicitly set to null - do not publish
258
+ if ( publishers === null ) {
259
+ debug ( `${ event . file } is not published: publish set to null` )
260
+ return
261
+ }
262
+
263
+ if ( publishers == null ) {
264
+ publishers = event . packager . info . devMetadata . build . publish
265
+ if ( publishers === null ) {
266
+ debug ( `${ event . file } is not published: publish set to null in the "build"` )
267
+ return
268
+ }
269
+
270
+ if ( publishers == null && options . githubToken != null ) {
271
+ publishers = [ "github" ]
272
+ }
273
+ // if both tokens are set — still publish to github (because default publisher is github)
274
+ if ( publishers == null && options . bintrayToken != null ) {
275
+ publishers = [ "bintray" ]
276
+ }
277
+ }
278
+
279
+ for ( let publisherName of asArray ( publishers ) ) {
280
+ let publisher = nameToPublisher . get ( publisherName )
281
+ if ( publisher == null ) {
282
+ publisher = createPublisher ( packager , options , publisherName , isPublishOptionGuessed )
283
+ nameToPublisher . set ( publisherName , publisher )
284
+ }
285
+
286
+ if ( publisher != null ) {
287
+ publisher
288
+ . then ( it => it == null ? null : publishTasks . push ( < BluebirdPromise < any > > it . upload ( event . file , event . artifactName ) ) )
289
+ }
290
+ }
291
+ } )
292
+ }
293
+
294
+ export async function createPublisher ( packager : Packager , options : PublishOptions , publisherName : string , isPublishOptionGuessed : boolean = false ) : Promise < Publisher | null > {
295
+ const info = await getRepositoryInfo ( packager . metadata , packager . devMetadata )
296
+ if ( info == null ) {
297
+ if ( isPublishOptionGuessed ) {
298
+ return null
299
+ }
300
+
301
+ warn ( "Cannot detect repository by .git/config" )
302
+ throw new Error ( `Please specify 'repository' in the dev package.json ('${ packager . devPackageFile } ')` )
303
+ }
304
+
305
+ if ( publisherName === "github" ) {
306
+ const version = packager . metadata . version !
307
+ log ( `Creating Github Publisher — user: ${ info . user } , project: ${ info . project } , version: ${ version } ` )
308
+ return new GitHubPublisher ( info . user , info . project , version , options , isPublishOptionGuessed )
309
+ }
310
+ if ( publisherName === "bintray" ) {
311
+ const version = packager . metadata . version !
312
+ const bintrayInfo : BintrayConfiguration = { user : info . user , packageName : info . project , repo : "generic" }
313
+ log ( `Creating Bintray Publisher — user: ${ bintrayInfo . user } , package: ${ bintrayInfo . packageName } , repository: ${ bintrayInfo . repo } , version: ${ version } ` )
314
+ return new BintrayPublisher ( bintrayInfo , version , options )
315
+ }
316
+ return null
317
+ }
0 commit comments