mc-publish/src/index.ts

53 lines
2.5 KiB
TypeScript
Raw Normal View History

import { getRequiredFiles, gradleOutputSelector } from "./utils/file-utils";
2021-09-25 02:37:10 -04:00
import PublisherFactory from "./publishing/publisher-factory";
import PublisherTarget from "./publishing/publisher-target";
import { getInputAsObject, mapNumberInput } from "./utils/input-utils";
2021-09-25 02:37:10 -04:00
import { getDefaultLogger } from "./utils/logger-utils";
import { retry } from "./utils/function-utils";
2022-07-05 11:02:45 -04:00
import LoggingStopwatch from "./utils/logging-stopwatch";
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>();
for (const target of PublisherTarget.getValues()) {
const targetName = PublisherTarget.toString(target);
const publisherOptions = commonOptions[targetName.toLowerCase()];
if (!publisherOptions?.token || typeof publisherOptions.token !== "string") {
2021-09-25 02:37:10 -04:00
continue;
}
const options = { ...commonOptions, ...publisherOptions };
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 getRequiredFiles(fileSelector);
const retryAttempts = mapNumberInput(options.retryAttempts);
const retryDelay = mapNumberInput(options.retryDelay);
const publisher = publisherFactory.create(target, logger);
2022-07-05 11:02:45 -04:00
const stopwatch = LoggingStopwatch.startNew(logger, `Publishing assets to ${targetName}...`, ms => `Successfully published assets to ${targetName} (in ${ms} ms)`);
await retry({
func: () => publisher.publish(files, options),
maxAttempts: retryAttempts,
delay: retryDelay,
errorCallback: e => {
logger.error(`${e}`);
logger.info(`Retrying to publish assets to ${targetName} in ${retryDelay} ms...`);
}
});
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(", ")}`);
} else {
logger.warn("You didn't specify any targets, your assets have not been published");
}
}
main().catch(error => getDefaultLogger().fatal(error instanceof Error ? `${error}` : `Something went horribly wrong: ${error}`));