Skip to content
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
56 changes: 56 additions & 0 deletions src/clients/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,62 @@ export async function getRepository(config: {
}
}

export type OnboardingStep =
| "createPrismicProject"
| "createPageType"
| "createSlice"
| "connectPrismic";
Comment thread
jomifepe marked this conversation as resolved.

const OnboardingStateSchema = z.object({
completedSteps: z.array(z.string()),
});
export type OnboardingState = z.infer<typeof OnboardingStateSchema>;

type OnboardingConfig = {
repo: string;
token: string | undefined;
host: string;
};

export async function getOnboardingState(config: OnboardingConfig): Promise<OnboardingState> {
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<void> {
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,
});
}
}
Comment thread
jomifepe marked this conversation as resolved.

export async function completeOnboardingStepsSilently(
config: OnboardingConfig & { stepIds: OnboardingStep[] },
): Promise<void> {
try {
await completeOnboardingSteps(config);
} catch {
// Ignore errors
}
}

function getRepositoryServiceUrl(host: string): URL {
return new URL(`https://api.internal.${host}/repository/`);
}
8 changes: 8 additions & 0 deletions src/commands/pull.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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;
Expand Down
20 changes: 20 additions & 0 deletions src/commands/push.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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;
Expand Down
8 changes: 8 additions & 0 deletions src/commands/repo-create.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -50,6 +51,13 @@ export async function createRepo(config: {
throw error;
}

await completeOnboardingStepsSilently({
repo: domain,
token,
host,
stepIds: ["createPrismicProject"],
});

return domain;
}

Expand Down
7 changes: 7 additions & 0 deletions src/commands/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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();
Expand Down