Skip to content
Draft
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,6 @@
}
},
"engines": {
"node": ">=20"
"node": ">=20.12"
}
}
61 changes: 49 additions & 12 deletions src/adapters/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { glob } from "tinyglobby";
import { getCustomTypes, getSlices } from "../clients/custom-types";
import { readJsonFile, writeFileRecursive } from "../lib/file";
import { stringify } from "../lib/json";
import { log } from "../lib/logger";
import { readPackageJson } from "../lib/packageJson";
import { appendTrailingSlash } from "../lib/url";
import { addRoute, removeRoute, updateRoute } from "../project";
Expand Down Expand Up @@ -110,6 +111,7 @@ export abstract class Adapter {
const sliceDirectory = new URL(sliceDirectoryName, appendTrailingSlash(library));
const modelPath = new URL("model.json", appendTrailingSlash(sliceDirectory));
await writeFileRecursive(modelPath, stringify(model));
log({ type: "file-created", url: modelPath });
await this.createSliceIndexFile(library);
await this.onSliceCreated(model, library);
}
Expand All @@ -118,12 +120,14 @@ export abstract class Adapter {
const slice = await this.getSlice(model.id);
const modelPath = new URL("model.json", appendTrailingSlash(slice.directory));
await writeFileRecursive(modelPath, stringify(model));
log({ type: "file-updated", url: modelPath });
await this.onSliceUpdated(model);
}

async deleteSlice(id: string): Promise<void> {
const slice = await this.getSlice(id);
await rm(slice.directory, { recursive: true });
log({ type: "file-deleted", url: slice.directory });
await this.createSliceIndexFile(slice.library);
await this.onSliceDeleted(id);
}
Expand Down Expand Up @@ -162,6 +166,7 @@ export abstract class Adapter {
const customTypesDirectory = new URL("customtypes/", projectRoot);
const modelPath = new URL(`${model.id}/index.json`, customTypesDirectory);
await writeFileRecursive(modelPath, stringify(model));
log({ type: "file-created", url: modelPath });
if (model.format === "page") await addRoute(model);
await this.onCustomTypeCreated(model);
}
Expand All @@ -170,13 +175,15 @@ export abstract class Adapter {
const customType = await this.getCustomType(model.id);
const modelPath = new URL("index.json", appendTrailingSlash(customType.directory));
await writeFileRecursive(modelPath, stringify(model));
log({ type: "file-updated", url: modelPath });
await updateRoute(model);
await this.onCustomTypeUpdated(model);
}

async deleteCustomType(id: string): Promise<void> {
const customType = await this.getCustomType(id);
await rm(customType.directory, { recursive: true });
log({ type: "file-deleted", url: customType.directory });
await removeRoute(id);
await this.onCustomTypeDeleted(id);
}
Expand All @@ -187,53 +194,68 @@ export abstract class Adapter {
host: string;
}): Promise<void> {
const { repo, token, host } = config;
await Promise.all([
const [syncSlicesResult, syncCustomTypesResult] = await Promise.all([
this.syncSlices({ repo, token, host, generateTypes: false }),
this.syncCustomTypes({ repo, token, host, generateTypes: false }),
]);
await this.generateTypes();
if (syncSlicesResult.didSync || syncCustomTypesResult.didSync) await this.generateTypes();
}

async syncSlices(config: {
repo: string;
token: string | undefined;
host: string;
generateTypes?: boolean;
}): Promise<void> {
}): Promise<{ didSync: boolean }> {
const { repo, token, host, generateTypes = true } = config;

let didSync = false;

const remoteSlices = await getSlices({ repo, token, host });
const localSlices = await this.getSlices();

// Handle slices update
for (const remoteSlice of remoteSlices) {
const localSlice = localSlices.find((slice) => slice.model.id === remoteSlice.id);
if (localSlice) await this.updateSlice(remoteSlice);
if (localSlice && JSON.stringify(remoteSlice) !== JSON.stringify(localSlice.model)) {
await this.updateSlice(remoteSlice);
didSync = true;
}
}

// Handle slices deletion
for (const localSlice of localSlices) {
const existsRemotely = remoteSlices.some((slice) => slice.id === localSlice.model.id);
if (!existsRemotely) await this.deleteSlice(localSlice.model.id);
if (!existsRemotely) {
await this.deleteSlice(localSlice.model.id);
didSync = true;
}
}

// Handle slices creation
for (const remoteSlice of remoteSlices) {
const existsLocally = localSlices.some((slice) => slice.model.id === remoteSlice.id);
if (!existsLocally) await this.createSlice(remoteSlice);
if (!existsLocally) {
await this.createSlice(remoteSlice);
didSync = true;
}
}

if (generateTypes) await this.generateTypes();
if (didSync && generateTypes) await this.generateTypes();

return { didSync };
}

async syncCustomTypes(config: {
repo: string;
token: string | undefined;
host: string;
generateTypes?: boolean;
}): Promise<void> {
}): Promise<{ didSync: boolean }> {
const { repo, token, host, generateTypes = true } = config;

let didSync = false;

const remoteCustomTypes = await getCustomTypes({ repo, token, host });
const localCustomTypes = await this.getCustomTypes();

Expand All @@ -242,26 +264,40 @@ export abstract class Adapter {
const localCustomType = localCustomTypes.find(
(customType) => customType.model.id === remoteCustomType.id,
);
if (localCustomType) await this.updateCustomType(remoteCustomType);
if (
localCustomType &&
JSON.stringify(remoteCustomType) !== JSON.stringify(localCustomType.model)
) {
await this.updateCustomType(remoteCustomType);
didSync = true;
}
}

// Handle custom types deletion
for (const localCustomType of localCustomTypes) {
const existsRemotely = remoteCustomTypes.some(
(customType) => customType.id === localCustomType.model.id,
);
if (!existsRemotely) await this.deleteCustomType(localCustomType.model.id);
if (!existsRemotely) {
await this.deleteCustomType(localCustomType.model.id);
didSync = true;
}
}

// Handle custom types creation
for (const remoteCustomType of remoteCustomTypes) {
const existsLocally = localCustomTypes.some(
(customType) => customType.model.id === remoteCustomType.id,
);
if (!existsLocally) await this.createCustomType(remoteCustomType);
if (!existsLocally) {
await this.createCustomType(remoteCustomType);
didSync = true;
}
}

if (generateTypes) await this.generateTypes();
if (didSync && generateTypes) await this.generateTypes();

return { didSync };
}

async generateTypes(): Promise<URL> {
Expand All @@ -280,6 +316,7 @@ export abstract class Adapter {
typesProvider: "@prismicio/client",
});
await writeFileRecursive(output, types);
log({ type: "file-updated", url: output });
return output;
}
}
9 changes: 9 additions & 0 deletions src/adapters/nextjs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { fileURLToPath } from "node:url";

import { Adapter } from ".";
import { exists, writeFileRecursive } from "../lib/file";
import { log } from "../lib/logger";
import { addDependencies, findPackageJson, getNpmPackageVersion } from "../lib/packageJson";
import { dedent } from "../lib/string";
import { appendTrailingSlash } from "../lib/url";
Expand Down Expand Up @@ -52,6 +53,7 @@ export class NextJsAdapter extends Adapter {
typescript: await checkIsTypeScriptProject(),
});
await writeFileRecursive(componentPath, contents);
log({ type: "file-created", url: componentPath });
}

onSliceUpdated(): void {}
Expand Down Expand Up @@ -91,6 +93,7 @@ export class NextJsAdapter extends Adapter {
const filename = `index.${extension}`;
const indexPath = new URL(filename, library);
await writeFileRecursive(indexPath, contents);
log({ type: "file-updated", url: indexPath });
}

async getDefaultSliceLibrary(): Promise<URL> {
Expand All @@ -116,6 +119,7 @@ async function createRevalidateRoute(): Promise<void> {

const contents = revalidateRouteTemplate({ supportsCacheLife });
await writeFileRecursive(filePath, contents);
log({ type: "file-created", url: filePath });
}

async function createExitPreviewRoute(): Promise<void> {
Expand All @@ -132,6 +136,7 @@ async function createExitPreviewRoute(): Promise<void> {

const contents = exitPreviewRouteTemplate({ typescript, appRouter });
await writeFileRecursive(filePath, contents);
log({ type: "file-created", url: filePath });
}

async function createPreviewRoute(): Promise<void> {
Expand All @@ -148,6 +153,7 @@ async function createPreviewRoute(): Promise<void> {

const contents = previewRouteTemplate({ typescript, appRouter });
await writeFileRecursive(filePath, contents);
log({ type: "file-created", url: filePath });
}

async function createSliceSimulatorPage(): Promise<void> {
Expand All @@ -164,6 +170,7 @@ async function createSliceSimulatorPage(): Promise<void> {

const contents = sliceSimulatorPageTemplate({ typescript, appRouter });
await writeFileRecursive(filePath, contents);
log({ type: "file-created", url: filePath });
}

async function createPrismicIoFile(): Promise<void> {
Expand All @@ -182,6 +189,7 @@ async function createPrismicIoFile(): Promise<void> {
hasSrcDirectory,
});
await writeFileRecursive(filePath, contents);
log({ type: "file-created", url: filePath });
}

async function createPageFile(model: CustomType): Promise<void> {
Expand All @@ -206,6 +214,7 @@ async function createPageFile(model: CustomType): Promise<void> {
appRouter: usesAppRouter,
});
await writeFileRecursive(pageFilePath, contents);
log({ type: "file-created", url: pageFilePath });
}

async function checkUsesAppRouter() {
Expand Down
7 changes: 7 additions & 0 deletions src/adapters/nuxt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { fileURLToPath } from "node:url";

import { Adapter } from ".";
import { exists, writeFileRecursive } from "../lib/file";
import { log } from "../lib/logger";
import { addDependencies, getNpmPackageVersion } from "../lib/packageJson";
import { dedent } from "../lib/string";
import { appendTrailingSlash } from "../lib/url";
Expand Down Expand Up @@ -43,6 +44,7 @@ export class NuxtAdapter extends Adapter {
typescript: await checkIsTypeScriptProject(),
});
await writeFileRecursive(componentPath, contents);
log({ type: "file-created", url: componentPath });
}

onSliceUpdated(): void {}
Expand Down Expand Up @@ -78,6 +80,7 @@ export class NuxtAdapter extends Adapter {
const filename = `index.${extension}`;
const indexPath = new URL(filename, library);
await writeFileRecursive(indexPath, contents);
log({ type: "file-updated", url: indexPath });
}

async getDefaultSliceLibrary(): Promise<URL> {
Expand Down Expand Up @@ -174,6 +177,7 @@ async function createSliceSimulatorPage(): Promise<void> {

const contents = sliceSimulatorPageTemplate({ typescript });
await writeFileRecursive(filePath, contents);
log({ type: "file-created", url: filePath });
}

async function moveOrDeleteAppVue(): Promise<void> {
Expand All @@ -194,9 +198,11 @@ async function moveOrDeleteAppVue(): Promise<void> {

if (!(await exists(indexVuePath))) {
await writeFileRecursive(indexVuePath, contents);
log({ type: "file-created", url: indexVuePath });
}

await rm(appVuePath);
log({ type: "file-deleted", url: appVuePath });
}

async function modifySliceLibraryPath(adapter: NuxtAdapter): Promise<void> {
Expand Down Expand Up @@ -246,6 +252,7 @@ async function createPageFile(model: CustomType): Promise<void> {
typescript: await checkIsTypeScriptProject(),
});
await writeFileRecursive(pageFilePath, contents);
log({ type: "file-created", url: pageFilePath });
}

async function getJsFileExtension(): Promise<string> {
Expand Down
Loading
Loading