diff --git a/package.json b/package.json index 7247300..96d6c7e 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@supernovaio/cli", "description": "Supernova.io Command Line Interface", - "version": "1.1.1", + "version": "1.1.2", "author": "Supernova.io", "homepage": "https://supernova.io/", "keywords": [ diff --git a/src/commands/publish-documentation.ts b/src/commands/publish-documentation.ts index a2a049d..c3e5794 100644 --- a/src/commands/publish-documentation.ts +++ b/src/commands/publish-documentation.ts @@ -13,7 +13,8 @@ import { Command, Flags } from "@oclif/core" import { Environment, ErrorCode } from "../types/types" import { getWritableVersion } from "../utils/sdk" import "colors" -import { DocumentationEnvironment } from "@supernovaio/sdk" +import { DocumentationEnvironment, ExportBuildStatus } from "@supernovaio/sdk" +import { sleep } from "../utils/common" // --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- // MARK: - Definition @@ -60,6 +61,14 @@ export class PublishDocumentation extends Command { hidden: true, required: false, }), + awaitPublishJob: Flags.boolean({ + description: + "Whether to block the process until the publishing is done. " + + "Setting the flag to false will exit with success as long as documentation publish was successfully triggered, " + + "but before the publish is completed. Setting the flag to true will exit once the publish is complete and will " + + "throw if documentation publish is not successful.", + default: true, + }), } // --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- @@ -76,18 +85,33 @@ export class PublishDocumentation extends Command { } // Get workspace -> design system –> version - let { instance, id } = await getWritableVersion(flags) - let result = await instance.documentation.publishDrafts(id, environment, { + const { instance, id, designSystem } = await getWritableVersion(flags) + const publishJob = await instance.documentation.publishDrafts(id, environment, { pagePersistentIds: [], groupPersistentIds: [], }) - if (result.status === "Success") { + if (!(flags.awaitPublishJob ?? true)) { + this.log(`Publishing documentation in ${designSystem.name} has started, job await is disabled, exiting...`) + } else { + this.log(`Publishing documentation in ${designSystem.name}...`) + } + + // Timeout is roughly 30 minutes + for (let i = 0; i < 30 * 60; i++) { + await sleep(1000) + const updatedJob = await instance.documentation.getDocumentationBuild(designSystem.workspaceId, publishJob.id) + if (isJobStatusDone(updatedJob.status)) break + } + + if (publishJob.status === "Success") { this.log("\nDone: Documentation queued for publishing".green) - } else if (result.status === "InProgress") { - this.log("\n Done: Skipped documentation publish as another build is already in progress".green) - } else if (result.status === "Failed") { - throw new Error(`Documentation publish failed with unknown failure`) + } else if (publishJob.status === "Failed") { + throw new Error(`Documentation publish failed`) + } else if (publishJob.status === "Timeout") { + throw new Error(`Documentation publish timed out`) + } else { + throw new Error(`Error awaiting publish job`) } } catch (error) { // Catch general error @@ -109,3 +133,9 @@ function tryParseDocsEnvironment(targetArg: string) { return null } } + +function isJobStatusDone(status: ExportBuildStatus): boolean { + return ( + status === ExportBuildStatus.Failed || status === ExportBuildStatus.Success || status === ExportBuildStatus.Timeout + ) +} diff --git a/src/utils/common.ts b/src/utils/common.ts new file mode 100644 index 0000000..fcc1262 --- /dev/null +++ b/src/utils/common.ts @@ -0,0 +1,3 @@ +export async function sleep(ms: number): Promise { + return new Promise(resolve => setTimeout(resolve, ms)) +}