diff --git a/src/clients/repository.ts b/src/clients/repository.ts index 351f465..86902bc 100644 --- a/src/clients/repository.ts +++ b/src/clients/repository.ts @@ -37,6 +37,62 @@ export async function getRepository(config: { } } +export type OnboardingStep = + | "createPrismicProject" + | "createPageType" + | "createSlice" + | "connectPrismic"; + +const OnboardingStateSchema = z.object({ + completedSteps: z.array(z.string()), +}); +export type OnboardingState = z.infer; + +type OnboardingConfig = { + repo: string; + token: string | undefined; + host: string; +}; + +export async function getOnboardingState(config: OnboardingConfig): Promise { + const { repo, token, host } = config; + const url = new URL("./onboarding", getRepositoryServiceUrl(host)); + url.searchParams.set("repository", repo); + return request(url, { + credentials: { "prismic-auth": token }, + schema: OnboardingStateSchema, + }); +} + +export async function completeOnboardingSteps( + config: OnboardingConfig & { stepIds: OnboardingStep[] }, +): Promise { + const { repo, token, host, stepIds } = config; + const { completedSteps } = await getOnboardingState({ repo, token, host }); + const missing = stepIds.filter((id) => !completedSteps.includes(id)); + + // API does not accept multiple steps; toggle each missing step sequentially. + for (const stepId of missing) { + const url = new URL(`./onboarding/${stepId}/toggle`, getRepositoryServiceUrl(host)); + url.searchParams.set("repository", repo); + await request(url, { + method: "PATCH", + credentials: { "prismic-auth": token }, + schema: OnboardingStateSchema, + }); + } +} + +export async function completeOnboardingStepsSilently( + config: OnboardingConfig & { stepIds: OnboardingStep[] }, +): Promise { + try { + await completeOnboardingSteps(config); + } catch { + // Ignore errors + } +} + function getRepositoryServiceUrl(host: string): URL { return new URL(`https://api.internal.${host}/repository/`); } diff --git a/src/commands/pull.ts b/src/commands/pull.ts index f0d576f..866609b 100644 --- a/src/commands/pull.ts +++ b/src/commands/pull.ts @@ -1,6 +1,7 @@ import { getAdapter } from "../adapters"; import { getHost, getToken } from "../auth"; import { getCustomTypes, getSlices } from "../clients/custom-types"; +import { completeOnboardingStepsSilently } from "../clients/repository"; import { resolveEnvironment } from "../environments"; import { CommandError, createCommand, type CommandConfig } from "../lib/command"; import { diffArrays } from "../lib/diff"; @@ -138,6 +139,13 @@ export default createCommand(config, async ({ values }) => { await adapter.generateTypes(); + await completeOnboardingStepsSilently({ + repo: parentRepo, + token, + host, + stepIds: ["connectPrismic"], + }); + const totalTypes = customTypeOps.insert.length + customTypeOps.update.length; const totalSlices = sliceOps.insert.length + sliceOps.update.length; const totalDeletes = customTypeOps.delete.length + sliceOps.delete.length; diff --git a/src/commands/push.ts b/src/commands/push.ts index 3182a28..485b84a 100644 --- a/src/commands/push.ts +++ b/src/commands/push.ts @@ -12,6 +12,10 @@ import { updateCustomType, updateSlice, } from "../clients/custom-types"; +import { + completeOnboardingStepsSilently, + type OnboardingStep, +} from "../clients/repository"; import { resolveEnvironment } from "../environments"; import { CommandError, createCommand, type CommandConfig } from "../lib/command"; import { diffArrays } from "../lib/diff"; @@ -143,6 +147,22 @@ export default createCommand(config, async ({ values }) => { await removeSlice(id, { repo, token, host }); } + const onboardingSteps: OnboardingStep[] = []; + if (sliceOps.insert.length > 0) { + onboardingSteps.push("createSlice"); + } + if (customTypeOps.insert.some((model) => model.format === "page")) { + onboardingSteps.push("createPageType"); + } + if (onboardingSteps.length > 0) { + await completeOnboardingStepsSilently({ + repo: parentRepo, + token, + host, + stepIds: onboardingSteps, + }); + } + const totalTypes = customTypeOps.insert.length + customTypeOps.update.length; const totalSlices = sliceOps.insert.length + sliceOps.update.length; const totalDeletes = customTypeOps.delete.length + sliceOps.delete.length; diff --git a/src/commands/repo-create.ts b/src/commands/repo-create.ts index 3e66ef7..9296c2f 100644 --- a/src/commands/repo-create.ts +++ b/src/commands/repo-create.ts @@ -1,5 +1,6 @@ import { getAdapter } from "../adapters"; import { getHost, getToken } from "../auth"; +import { completeOnboardingStepsSilently } from "../clients/repository"; import { checkIsDomainAvailable, createRepository } from "../clients/wroom"; import { CommandError, createCommand, type CommandConfig } from "../lib/command"; import { UnknownRequestError } from "../lib/request"; @@ -50,6 +51,13 @@ export async function createRepo(config: { throw error; } + await completeOnboardingStepsSilently({ + repo: domain, + token, + host, + stepIds: ["createPrismicProject"], + }); + return domain; } diff --git a/src/commands/sync.ts b/src/commands/sync.ts index 45876d5..1849269 100644 --- a/src/commands/sync.ts +++ b/src/commands/sync.ts @@ -4,6 +4,7 @@ import { setTimeout } from "node:timers/promises"; import { getAdapter } from "../adapters"; import { getHost, getToken } from "../auth"; import { getCustomTypes, getSlices } from "../clients/custom-types"; +import { completeOnboardingStepsSilently } from "../clients/repository"; import { env } from "../env"; import { resolveEnvironment } from "../environments"; import { createCommand, type CommandConfig } from "../lib/command"; @@ -116,6 +117,12 @@ export default createCommand(config, async ({ values }) => { lastHash = nextHash; if (isInitial) { + await completeOnboardingStepsSilently({ + repo: parentRepo, + token, + host, + stepIds: ["connectPrismic"], + }); console.info("Initial sync complete."); } else { const timestamp = new Date().toLocaleTimeString();