Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
52 changes: 52 additions & 0 deletions src/clients/onboarding.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import * as z from "zod/mini";

import { request } from "../lib/request";
import { getRepositoryServiceUrl } from "./repository";

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: string[] },
): 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,
});
}
}

export async function completeOnboardingStepsSilently(
config: OnboardingConfig & { stepIds: string[] },
): Promise<void> {
try {
await completeOnboardingSteps(config);
} catch {}
}
2 changes: 1 addition & 1 deletion src/clients/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ export async function getRepository(config: {
}
}

function getRepositoryServiceUrl(host: string): URL {
export function getRepositoryServiceUrl(host: string): URL {
return new URL(`https://api.internal.${host}/repository/`);
}
8 changes: 8 additions & 0 deletions src/commands/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { Profile } from "../clients/user";
import { getAdapter } from "../adapters";
import { createLoginSession, getHost, getToken } from "../auth";
import { getCustomTypes, getSlices } from "../clients/custom-types";
import { completeOnboardingStepsSilently } from "../clients/onboarding";
import { getProfile } from "../clients/user";
import { DEFAULT_PRISMIC_HOST } from "../env";
import { openBrowser } from "../lib/browser";
Expand Down Expand Up @@ -193,6 +194,13 @@ export default createCommand(config, async ({ values }) => {

await adapter.generateTypes();

await completeOnboardingStepsSilently({
repo,
token,
host,
stepIds: ["createProject", "chooseLocale"],
});
Comment thread
jomifepe marked this conversation as resolved.
Outdated

console.info(`\nInitialized Prismic for repository "${repo}".`);
console.info("Run `prismic type create <name>` to create a content type.");
console.info("Run `prismic sync` to sync models from Prismic.");
Expand Down
17 changes: 17 additions & 0 deletions src/commands/slice-create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import type { SharedSlice } from "@prismicio/types-internal/lib/customtypes";
import { snakeCase } from "change-case";

import { getAdapter } from "../adapters";
import { getHost, getToken } from "../auth";
import { completeOnboardingStepsSilently } from "../clients/onboarding";
import { createCommand, type CommandConfig } from "../lib/command";
import { readConfig } from "../project";

const config = {
name: "prismic slice create",
Expand Down Expand Up @@ -41,6 +44,20 @@ export default createCommand(config, async ({ positionals, values }) => {
await adapter.createSlice(model);
await adapter.generateTypes();

try {
const [{ repositoryName }, token, host] = await Promise.all([
readConfig(),
getToken(),
getHost(),
]);
await completeOnboardingStepsSilently({
repo: repositoryName,
token,
host,
stepIds: ["createSlice"],
});
} catch {}

console.info(`Created slice "${name}" (id: "${id}")`);
console.info(`Run \`prismic field add <type> --to-slice ${id}\` to add fields.`);
console.info(`Run \`prismic slice connect ${id} --to <type>\` to connect the slice to a type.`);
Expand Down
19 changes: 19 additions & 0 deletions src/commands/type-create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import type { CustomType } from "@prismicio/types-internal/lib/customtypes";
import { snakeCase } from "change-case";

import { getAdapter } from "../adapters";
import { getHost, getToken } from "../auth";
import { completeOnboardingStepsSilently } from "../clients/onboarding";
import { CommandError, createCommand, type CommandConfig } from "../lib/command";
import { readConfig } from "../project";

const config = {
name: "prismic type create",
Expand Down Expand Up @@ -99,6 +102,22 @@ export default createCommand(config, async ({ positionals, values }) => {
await adapter.createCustomType(model);
await adapter.generateTypes();

if (format === "page") {
try {
const [{ repositoryName }, token, host] = await Promise.all([
readConfig(),
getToken(),
getHost(),
]);
await completeOnboardingStepsSilently({
repo: repositoryName,
token,
host,
stepIds: ["createPageType"],
});
} catch {}
}

console.info(`Created type "${name}" (id: "${id}", format: "${format}")`);
console.info(`Run \`prismic field add <type> --to-type ${id}\` to add fields.`);
console.info(`Run \`prismic type view ${id}\` to view the type.`);
Expand Down
Loading