11import { Publisher , PublishOptions } from "./publisher"
22import { Promise as BluebirdPromise } from "bluebird"
3- import { bintrayRequest , HttpError , doApiRequest , uploadFile } from "./gitHubRequest"
3+ import { HttpError , doApiRequest } from "./restApiRequest"
4+ import { uploadFile } from "./uploader"
45import { log } from "../util/log"
56import { debug } from "../util/util"
67import { basename } from "path"
78import { stat } from "fs-extra-p"
9+ import { BintrayClient , Version } from "./bintray"
810
911//noinspection JSUnusedLocalSymbols
1012const __awaiter = require ( "../util/awaiter" )
1113
12- //noinspection ReservedWordAsName
13- interface Version {
14- readonly name : string
15- readonly package : string
16- }
17-
1814export class BintrayPublisher implements Publisher {
1915 private _versionPromise : BluebirdPromise < Version >
20- private readonly auth : string
2116
22- private basePath : string
17+ private readonly client : BintrayClient
2318
2419 constructor ( private user : string , apiKey : string , private version : string , private packageName : string , private repo : string = "generic" , private options : PublishOptions = { } ) {
25- this . auth = `Basic ${ new Buffer ( `${ user } :${ apiKey } ` ) . toString ( "base64" ) } `
26- this . basePath = `/packages/${ this . user } /${ this . repo } /${ this . packageName } `
20+ this . client = new BintrayClient ( user , packageName , repo , apiKey )
2721 this . _versionPromise = < BluebirdPromise < Version > > this . init ( )
2822 }
2923
3024 private async init ( ) : Promise < Version | null > {
3125 try {
32- return await bintrayRequest < Version > ( ` ${ this . basePath } /versions/ ${ this . version } ` , this . auth )
26+ return await this . client . getVersion ( this . version )
3327 }
3428 catch ( e ) {
3529 if ( e instanceof HttpError && e . response . statusCode === 404 ) {
3630 if ( this . options . publish !== "onTagOrDraft" ) {
3731 log ( `Version ${ this . version } doesn't exist, creating one` )
38- return this . createVersion ( )
32+ return this . client . createVersion ( this . version )
3933 }
4034 else {
4135 log ( `Version ${ this . version } doesn't exist, artifacts will be not published` )
@@ -46,12 +40,6 @@ export class BintrayPublisher implements Publisher {
4640 }
4741 }
4842
49- private createVersion ( ) {
50- return bintrayRequest < Version > ( `${ this . basePath } /versions` , this . auth , {
51- name : this . version ,
52- } )
53- }
54-
5543 async upload ( file : string , artifactName ?: string ) : Promise < any > {
5644 const fileName = artifactName || basename ( file )
5745 const version = await this . _versionPromise
@@ -74,7 +62,7 @@ export class BintrayPublisher implements Publisher {
7462 "X-Bintray-Override" : "1" ,
7563 "X-Bintray-Publish" : "1" ,
7664 }
77- } , this . auth , uploadFile . bind ( this , file , fileStat , fileName ) )
65+ } , this . client . auth , uploadFile . bind ( this , file , fileStat , fileName ) )
7866 }
7967 catch ( e ) {
8068 if ( e instanceof HttpError && e . response . statusCode === 502 && badGatewayCount ++ < 3 ) {
@@ -93,10 +81,6 @@ export class BintrayPublisher implements Publisher {
9381 }
9482
9583 const version = this . _versionPromise . value ( )
96- if ( version == null ) {
97- return BluebirdPromise . resolve ( )
98- }
99-
100- return bintrayRequest < Version > ( `/packages/${ this . user } /${ this . repo } /${ this . packageName } /versions/${ version . name } ` , this . auth , null , "DELETE" )
84+ return version == null ? BluebirdPromise . resolve ( ) : this . client . deleteVersion ( version . name )
10185 }
10286}
0 commit comments