2022-07-05 13:44:29 -04:00
import File , { gradleOutputSelector } from "./utils/io/file" ;
2021-09-25 02:37:10 -04:00
import PublisherFactory from "./publishing/publisher-factory" ;
import PublisherTarget from "./publishing/publisher-target" ;
2022-07-05 13:44:29 -04:00
import { getInputAsObject , mapEnumInput , mapNumberInput } from "./utils/actions/input" ;
import { getDefaultLogger } from "./utils/logging/logger" ;
import retry from "./utils/retry" ;
import LoggingStopwatch from "./utils/logging/logging-stopwatch" ;
2022-07-05 11:08:11 -04:00
import AggregateError from "aggregate-error" ;
enum FailMode {
Fail ,
Warn ,
Skip ,
}
2021-09-25 02:37:10 -04:00
async function main() {
const commonOptions = getInputAsObject ( ) ;
const publisherFactory = new PublisherFactory ( ) ;
const logger = getDefaultLogger ( ) ;
const publishedTo = new Array < string > ( ) ;
2022-07-05 11:08:11 -04:00
const errors = new Array < Error > ( ) ;
2021-09-25 02:37:10 -04:00
for ( const target of PublisherTarget . getValues ( ) ) {
const targetName = PublisherTarget . toString ( target ) ;
const publisherOptions = commonOptions [ targetName . toLowerCase ( ) ] ;
2021-12-10 09:00:43 -05:00
if ( ! publisherOptions ? . token || typeof publisherOptions . token !== "string" ) {
2021-09-25 02:37:10 -04:00
continue ;
}
2021-09-30 10:01:53 -04:00
const options = { . . . commonOptions , . . . publisherOptions } ;
2022-07-05 13:44:29 -04:00
const fileSelector = typeof options . filesPrimary === "string" ? { primary : options.filesPrimary , secondary : typeof options . filesSecondary === "string" ? options.filesSecondary : gradleOutputSelector.secondary } : typeof options . files === "string" ? options.files : gradleOutputSelector ;
const files = await File . getRequiredFiles ( fileSelector ) ;
2022-07-05 10:07:56 -04:00
const retryAttempts = mapNumberInput ( options . retryAttempts ) ;
const retryDelay = mapNumberInput ( options . retryDelay ) ;
2022-07-05 11:08:11 -04:00
const failMode = mapEnumInput ( options . failMode , FailMode , FailMode . Fail as FailMode ) ;
2021-09-30 10:01:53 -04:00
const publisher = publisherFactory . create ( target , logger ) ;
2022-07-05 11:08:11 -04:00
const func = {
2022-01-12 10:16:19 -05:00
func : ( ) = > publisher . publish ( files , options ) ,
maxAttempts : retryAttempts ,
delay : retryDelay ,
2022-07-05 11:08:11 -04:00
errorCallback : ( e : Error ) = > {
logger . error ( e ) ;
2022-01-12 10:16:19 -05:00
logger . info ( ` Retrying to publish assets to ${ targetName } in ${ retryDelay } ms... ` ) ;
}
2022-07-05 11:08:11 -04:00
} ;
2022-01-12 10:16:19 -05:00
2022-07-05 11:08:11 -04:00
const stopwatch = LoggingStopwatch . startNew ( logger , ` Publishing assets to ${ targetName } ... ` , ms = > ` Successfully published assets to ${ targetName } (in ${ ms } ms) ` ) ;
try {
await retry ( func ) ;
} catch ( e : any ) {
switch ( failMode ) {
case FailMode . Warn :
logger . warn ( e ) ;
continue ;
case FailMode . Skip :
logger . warn ( ` An error occurred while uploading assets to ${ targetName } ` ) ;
errors . push ( e ) ;
continue ;
default :
throw e ;
}
}
2022-07-05 11:02:45 -04:00
stopwatch . stop ( ) ;
2021-09-25 02:37:10 -04:00
publishedTo . push ( targetName ) ;
}
if ( publishedTo . length ) {
logger . info ( ` Your assets have been successfully published to: ${ publishedTo . join ( ", " ) } ` ) ;
2022-07-05 11:08:11 -04:00
} else if ( ! errors . length ) {
2021-09-25 02:37:10 -04:00
logger . warn ( "You didn't specify any targets, your assets have not been published" ) ;
}
2022-07-05 11:08:11 -04:00
if ( errors . length ) {
throw new AggregateError ( errors ) ;
}
2021-09-25 02:37:10 -04:00
}
2022-07-05 11:08:11 -04:00
main ( ) . catch ( error = > getDefaultLogger ( ) . fatal ( error instanceof Error ? error : ` Something went horribly wrong: ${ error } ` ) ) ;