Skip to content
Closed
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
3 changes: 3 additions & 0 deletions src/commands/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,9 @@ export default createCommand(config, async ({ values }) => {
console.info("Run `prismic pull` to pull models from Prismic.");
}

// Progress reporting must not turn a successful project setup into a failure.
await completeOnboardingSteps(["connectPrismic"], { repo, token, host }).catch(sentryCaptureError);

// Printed last so it is the step the reader is left with.
const setupInstructions = await adapter.getSetupInstructions();
if (setupInstructions) console.info(`\n${setupInstructions}`);
Expand Down
14 changes: 14 additions & 0 deletions src/lib/prismic/clients/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,20 @@ export async function toggleOnboardingStep(
});
}

export async function completeOnboardingStep(
stepId: string,
config: RepositoryConfig,
): Promise<OnboardingState> {
const url = new URL(
`onboarding/${encodeURIComponent(stepId)}/complete`,
getRepositoryServiceUrl(config.host),
);
return onboardingServiceRequest(url, config, {
method: "PATCH",
schema: OnboardingStateSchema,
});
}

function repositoryServiceRequest<T>(
url: URL,
config: RepositoryConfig,
Expand Down
11 changes: 3 additions & 8 deletions src/lib/prismic/onboarding.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getOnboardingState, toggleOnboardingStep } from "./clients/repository";
import { completeOnboardingStep } from "./clients/repository";

type OnboardingConfig = {
repo: string;
Expand All @@ -17,12 +17,7 @@ export async function completeOnboardingSteps(
stepIds: OnboardingStep[],
config: OnboardingConfig,
): Promise<void> {
const { repo, token, host } = 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) {
await toggleOnboardingStep(stepId, { repo, token, host });
for (const stepId of new Set(stepIds)) {
await completeOnboardingStep(stepId, config);
}
}
10 changes: 10 additions & 0 deletions test/init.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ it("supports --help", async ({ expect, prismic }) => {
expect(stdout).toContain("prismic docs view cli#set-up-a-type-builder-project");
});

it("completes app connection after repeated successful init", async ({ expect, prismic, repo, token, host }) => {
for (let attempt = 0; attempt < 2; attempt++) {
const { exitCode, stderr } = await prismic("init", ["--repo", repo, "--no-setup"]);
expect(exitCode, stderr).toBe(0);
}
const completedSteps = await getOnboardingCompletedSteps({ repo, token, host });
expect(completedSteps.filter((step) => step === "connectPrismic")).toHaveLength(1);
});

it("fails if prismic.config.json already exists without --repo", async ({
expect,
prismic,
Expand Down Expand Up @@ -439,6 +448,7 @@ it("completes the handoff for a starter project", async ({

const completedSteps = await getOnboardingCompletedSteps({ repo, token, host });
expect(completedSteps).toContain("instantStart_continueBuildingLocally");
expect(completedSteps).toContain("connectPrismic");
} finally {
await deleteRepository(repo, { token, password, host });
}
Expand Down
Loading