Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement docs job await #38

Merged
merged 4 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand Down
46 changes: 38 additions & 8 deletions src/commands/publish-documentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
}),
}

// --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
Expand All @@ -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
Expand All @@ -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
)
}
3 changes: 3 additions & 0 deletions src/utils/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export async function sleep(ms: number): Promise<void> {
return new Promise(resolve => setTimeout(resolve, ms))
}
Loading