Skip to content
Open
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
8 changes: 4 additions & 4 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ or app state; callers pass in what it needs.

Prismic-domain logic shared by multiple commands. Still application-unaware.

### `src/lib/prismic/clients/`
### `src/lib/prismic/client.ts`

One file per backend service. Network calls only: build the request, validate
the response, return typed data. No orchestration, no business decisions, no
console output.
Every request to a Prismic backend service, in one file grouped by service.
Network calls only: build the request, validate the response, return typed
data. No orchestration, no business decisions, no console output.

## `src/adapters/`

Expand Down
11 changes: 3 additions & 8 deletions src/adapters/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,7 @@ import {
} from "../lib/file";
import { stringify } from "../lib/json";
import { getInstalledMajor, readPackageJson } from "../lib/packageJson";
import {
addPreview,
getPreviews,
getSimulatorUrl,
setSimulatorUrl,
} from "../lib/prismic/clients/core";
import { addPreview, getPreviews, getSimulatorUrl, setSimulatorUrl } from "../lib/prismic/client";
import {
canonicalizeCustomType,
canonicalizeSlice,
Expand Down Expand Up @@ -172,10 +167,10 @@ export abstract class Adapter {
if (setup) await this.setupProject();

const config = { repo: await getRepositoryName(), ...(await getCredentials()) };
if (!(await getSimulatorUrl(config))) {
if (!(await getSimulatorUrl(config)).simulator_url) {
await setSimulatorUrl(this.localSimulatorUrl, config);
}
if ((await getPreviews(config)).length === 0) {
if ((await getPreviews(config)).results.length === 0) {
await addPreview(this.localPreviewConfig, config);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { CREDENTIALS_PATH } from "./config";
import { DEFAULT_PRISMIC_HOST, env } from "./env";
import { exists, readJsonFile, writeFileRecursive } from "./lib/file";
import { stringify } from "./lib/json";
import { refreshToken as baseRefreshToken } from "./lib/prismic/clients/auth";
import { refreshToken as baseRefreshToken } from "./lib/prismic/client";
import { appendTrailingSlash } from "./lib/url";
import { forgetTrackedUser } from "./tracking";

Expand Down
7 changes: 3 additions & 4 deletions src/commands/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ import { openBrowser } from "../lib/browser";
import { createCommand, type CommandConfig, CommandError, exclusiveOptions } from "../lib/command";
import { readJsonFile, watchFiles, writeFileRecursive } from "../lib/file";
import { stringify } from "../lib/json";
import { createRelease, deleteRelease } from "../lib/prismic/clients/core";
import type { CustomTypesConfig } from "../lib/prismic/clients/custom-types";
import { createRelease, type CustomTypesConfig, deleteRelease } from "../lib/prismic/client";
import {
canonicalizeCustomType,
canonicalizeSlice,
Expand Down Expand Up @@ -175,13 +174,13 @@ async function saveSession(sessionPath: URL, session: Session): Promise<void> {
}

async function createSession({ repo, token, host }: CustomTypesConfig): Promise<Session> {
const releaseId = await createRelease(
const release = await createRelease(
{ label: "prismic dev", hidden: true },
{ repo, token, host },
).catch((error) => {
throw explainRequestError(error);
});
return { repo, releaseId, pid: process.pid, synced: {} };
return { repo, releaseId: release.id, pid: process.pid, synced: {} };
}

async function endSession({ releaseId, ...config }: Release, sessionPath: URL): Promise<void> {
Expand Down
2 changes: 1 addition & 1 deletion src/commands/docs-list.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { env } from "../env";
import { createCommand, type CommandConfig } from "../lib/command";
import { stringify } from "../lib/json";
import { getDocsIndex, getDocsPageIndex } from "../lib/prismic/clients/docs";
import { getDocsIndex, getDocsPageIndex } from "../lib/prismic/client";
import { formatTable } from "../lib/string";

const config = {
Expand Down
2 changes: 1 addition & 1 deletion src/commands/docs-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import GithubSlugger from "github-slugger";
import { env } from "../env";
import { CommandError, createCommand, type CommandConfig } from "../lib/command";
import { stringify } from "../lib/json";
import { getDocsPageContent } from "../lib/prismic/clients/docs";
import { getDocsPageContent } from "../lib/prismic/client";
import { getAnalyticsHeaders } from "../tracking";

const config = {
Expand Down
18 changes: 8 additions & 10 deletions src/commands/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,14 @@ import {
} from "../lib/packageJson";
import {
addPreview,
getPreviews,
removePreview,
setSimulatorUrl,
} from "../lib/prismic/clients/core";
import {
deletePreview,
enableTypeBuilder,
getPreviews,
getRepository,
getUserProfile,
type Repository,
} from "../lib/prismic/clients/repository";
import { getProfile } from "../lib/prismic/clients/user";
setSimulatorUrl,
} from "../lib/prismic/client";
import { diffModels, getLegacySlices, getRemoteModels } from "../lib/prismic/models";
import { completeOnboardingSteps } from "../lib/prismic/onboarding";
import { ForbiddenRequestError, UnauthorizedRequestError } from "../lib/request";
Expand Down Expand Up @@ -121,7 +119,7 @@ export default createCommand(config, async ({ values }) => {
const { host, token: initialToken } = await getCredentials();
let token = initialToken;
try {
await getProfile({ token, host });
await getUserProfile({ token, host });
} catch (error) {
if (error instanceof UnauthorizedRequestError || error instanceof ForbiddenRequestError) {
if (env.PRISMIC_TOKEN) throw error;
Expand Down Expand Up @@ -355,11 +353,11 @@ async function completeStarterHandoff(
config: { repo: string; token: string | undefined; host: string },
): Promise<void> {
try {
const previews = await getPreviews(config);
const { results: previews } = await getPreviews(config);
await Promise.all(
previews
.filter((preview) => preview.label === "Starter Preview")
.map((preview) => removePreview(preview.id, config)),
.map((preview) => deletePreview(preview.id, config)),
);
const hasDevelopmentPreview = previews.some(
(preview) => preview.url === adapter.localPreviewUrl,
Expand Down
2 changes: 1 addition & 1 deletion src/commands/locale-add.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getActiveRepositoryName } from "../adapters";
import { getCredentials } from "../auth";
import { createCommand, type CommandConfig } from "../lib/command";
import { upsertLocale } from "../lib/prismic/clients/locale";
import { upsertLocale } from "../lib/prismic/client";

const config = {
name: "prismic locale add",
Expand Down
4 changes: 2 additions & 2 deletions src/commands/locale-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { getActiveRepositoryName } from "../adapters";
import { getCredentials } from "../auth";
import { createCommand, type CommandConfig } from "../lib/command";
import { stringify } from "../lib/json";
import { getLocales } from "../lib/prismic/clients/locale";
import { getLocales } from "../lib/prismic/client";
import { formatTable } from "../lib/string";

const config = {
Expand Down Expand Up @@ -30,7 +30,7 @@ export default createCommand(config, async ({ values }) => {

const { token, host } = await getCredentials();

const locales = await getLocales({ repo, token, host });
const { results: locales } = await getLocales({ repo, token, host });

if (json) {
console.info(stringify(locales));
Expand Down
4 changes: 2 additions & 2 deletions src/commands/locale-remove.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getActiveRepositoryName } from "../adapters";
import { getCredentials } from "../auth";
import { createCommand, type CommandConfig } from "../lib/command";
import { removeLocale } from "../lib/prismic/clients/locale";
import { deleteLocale } from "../lib/prismic/client";

const config = {
name: "prismic locale remove",
Expand Down Expand Up @@ -31,7 +31,7 @@ export default createCommand(config, async ({ positionals, values }) => {

const { token, host } = await getCredentials();

await removeLocale(code, { repo, token, host });
await deleteLocale(code, { repo, token, host });

console.info(`Locale removed: ${code}`);
});
4 changes: 2 additions & 2 deletions src/commands/locale-set-master.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getActiveRepositoryName } from "../adapters";
import { getCredentials } from "../auth";
import { CommandError, createCommand, type CommandConfig } from "../lib/command";
import { getLocales, upsertLocale } from "../lib/prismic/clients/locale";
import { getLocales, upsertLocale } from "../lib/prismic/client";

const config = {
name: "prismic locale set-master",
Expand Down Expand Up @@ -31,7 +31,7 @@ export default createCommand(config, async ({ positionals, values }) => {

const { token, host } = await getCredentials();

const locales = await getLocales({ repo, token, host });
const { results: locales } = await getLocales({ repo, token, host });

const locale = locales.find((l) => l.id === code);

Expand Down
2 changes: 1 addition & 1 deletion src/commands/preview-add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { getActiveRepositoryName, getAdapter, NoSupportedFrameworkError } from "
import { getCredentials } from "../auth";
import { CommandError, createCommand, type CommandConfig } from "../lib/command";
import { MissingPackageJson } from "../lib/packageJson";
import { addPreview } from "../lib/prismic/clients/core";
import { addPreview } from "../lib/prismic/client";

const config = {
name: "prismic preview add",
Expand Down
4 changes: 2 additions & 2 deletions src/commands/preview-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { getActiveRepositoryName } from "../adapters";
import { getCredentials } from "../auth";
import { createCommand, type CommandConfig } from "../lib/command";
import { stringify } from "../lib/json";
import { getPreviews, getSimulatorUrl } from "../lib/prismic/clients/core";
import { getPreviews, getSimulatorUrl } from "../lib/prismic/client";
import { formatTable } from "../lib/string";

const config = {
Expand Down Expand Up @@ -30,7 +30,7 @@ export default createCommand(config, async ({ values }) => {

const { token, host } = await getCredentials();

const [previews, simulatorUrl] = await Promise.all([
const [{ results: previews }, { simulator_url: simulatorUrl }] = await Promise.all([
getPreviews({ repo, token, host }),
getSimulatorUrl({ repo, token, host }),
]);
Expand Down
6 changes: 3 additions & 3 deletions src/commands/preview-remove.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getActiveRepositoryName } from "../adapters";
import { getCredentials } from "../auth";
import { CommandError, createCommand, type CommandConfig } from "../lib/command";
import { getPreviews, removePreview } from "../lib/prismic/clients/core";
import { deletePreview, getPreviews } from "../lib/prismic/client";

const config = {
name: "prismic preview remove",
Expand Down Expand Up @@ -31,14 +31,14 @@ export default createCommand(config, async ({ positionals, values }) => {

const { token, host } = await getCredentials();

const previews = await getPreviews({ repo, token, host });
const { results: previews } = await getPreviews({ repo, token, host });

const preview = previews.find((p) => p.url === previewUrl);
if (!preview) {
throw new CommandError(`Preview not found: ${previewUrl}`);
}

await removePreview(preview.id, { repo, token, host });
await deletePreview(preview.id, { repo, token, host });

console.info(`Preview removed: ${previewUrl}`);
});
2 changes: 1 addition & 1 deletion src/commands/preview-set-simulator.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getActiveRepositoryName } from "../adapters";
import { getCredentials } from "../auth";
import { CommandError, createCommand, type CommandConfig } from "../lib/command";
import { setSimulatorUrl } from "../lib/prismic/clients/core";
import { setSimulatorUrl } from "../lib/prismic/client";

const config = {
name: "prismic preview set-simulator",
Expand Down
7 changes: 3 additions & 4 deletions src/commands/push.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import { getAdapter } from "../adapters";
import { getCredentials } from "../auth";
import { CommandError, createCommand, type CommandConfig } from "../lib/command";
import { getDirtyPaths, getGitRoot } from "../lib/git";
import { getDocumentTotalByCustomTypes } from "../lib/prismic/clients/core";
import { deleteScreenshots } from "../lib/prismic/clients/custom-types";
import { deleteSliceScreenshots, getDocumentTotalByCustomType } from "../lib/prismic/client";
import { diffModels, getRemoteModels, writeRemoteModels } from "../lib/prismic/models";
import { completeOnboardingSteps, type OnboardingStep } from "../lib/prismic/onboarding";
import { ForbiddenRequestError } from "../lib/request";
Expand Down Expand Up @@ -123,7 +122,7 @@ export default createCommand(config, async ({ values }) => {
throw await explainExistingDocuments(error, diff.customTypes.delete, { repo, token, host });
}
for (const { id } of diff.slices.delete) {
await deleteScreenshots(id, { repo, token, host }).catch((error) => {
await deleteSliceScreenshots(id, { repo, token, host }).catch((error) => {
const message = error instanceof Error ? error.message : String(error);
console.warn(
`Failed to delete screenshots for slice "${id}"${message ? `: ${message}` : "."}`,
Expand Down Expand Up @@ -173,7 +172,7 @@ async function explainExistingDocuments(

let documentCount: number;
try {
documentCount = await getDocumentTotalByCustomTypes(id, config);
documentCount = (await getDocumentTotalByCustomType(id, config)).total;
} catch {
return new CommandError(
`Could not check whether type "${id}" has associated pages. ` +
Expand Down
3 changes: 1 addition & 2 deletions src/commands/repo-create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import { FRAMEWORKS, getAdapter } from "../adapters";
import { getCredentials } from "../auth";
import { detectAgent } from "../lib/ai";
import { CommandError, createCommand, type CommandConfig } from "../lib/command";
import { upsertLocale } from "../lib/prismic/clients/locale";
import { checkIsDomainAvailable, createRepository } from "../lib/prismic/clients/wroom";
import { checkIsDomainAvailable, createRepository, upsertLocale } from "../lib/prismic/client";
import { completeOnboardingSteps } from "../lib/prismic/onboarding";

const MAX_DOMAIN_TRIES = 5;
Expand Down
4 changes: 2 additions & 2 deletions src/commands/repo-list.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getCredentials } from "../auth";
import { createCommand, type CommandConfig } from "../lib/command";
import { stringify } from "../lib/json";
import { getRepositories } from "../lib/prismic/clients/user";
import { getUserRepositories } from "../lib/prismic/client";
import { formatTable } from "../lib/string";

const config = {
Expand All @@ -17,7 +17,7 @@ export default createCommand(config, async ({ values }) => {

const { token, host } = await getCredentials();

const repos = await getRepositories({ token, host });
const { repositories: repos } = await getUserRepositories({ token, host });

if (json) {
console.info(
Expand Down
8 changes: 4 additions & 4 deletions src/commands/repo-set-api-access.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { getCredentials } from "../auth";
import { CommandError, createCommand, type CommandConfig } from "../lib/command";
import { type RepositoryAccessLevel, setRepositoryAccess } from "../lib/prismic/clients/wroom";
import { type RepositoryApiAccessLevel, setRepositoryApiAccess } from "../lib/prismic/client";
import { getRepositoryName } from "../project";

const VALID_LEVELS: RepositoryAccessLevel[] = ["private", "public", "open"];
const VALID_LEVELS: RepositoryApiAccessLevel[] = ["private", "public", "open"];

const config = {
name: "prismic repo set-api-access",
Expand All @@ -27,15 +27,15 @@ export default createCommand(config, async ({ positionals, values }) => {
const [level] = positionals;
const { repo = await getRepositoryName() } = values;

if (!VALID_LEVELS.includes(level as RepositoryAccessLevel)) {
if (!VALID_LEVELS.includes(level as RepositoryApiAccessLevel)) {
throw new CommandError(
`Invalid access level: ${level}. Must be one of: ${VALID_LEVELS.join(", ")}`,
);
}

const { token, host } = await getCredentials();

await setRepositoryAccess(level as RepositoryAccessLevel, { repo, token, host });
await setRepositoryApiAccess(level as RepositoryApiAccessLevel, { repo, token, host });

console.info(`Repository access set to: ${level}`);
});
6 changes: 3 additions & 3 deletions src/commands/repo-set-name.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getCredentials } from "../auth";
import { createCommand, type CommandConfig } from "../lib/command";
import { setRepositoryName } from "../lib/prismic/clients/wroom";
import { setRepositoryName } from "../lib/prismic/client";
import { getRepositoryName } from "../project";

const config = {
Expand All @@ -25,7 +25,7 @@ export default createCommand(config, async ({ positionals, values }) => {

const { token, host } = await getCredentials();

const confirmedName = await setRepositoryName(displayName, { repo, token, host });
const { repository } = await setRepositoryName(displayName, { repo, token, host });

console.info(`Repository name set to: ${confirmedName}`);
console.info(`Repository name set to: ${repository.name}`);
});
Loading
Loading