diff --git a/src/commands/pull.ts b/src/commands/pull.ts index d0aa8fd..85ff1c2 100644 --- a/src/commands/pull.ts +++ b/src/commands/pull.ts @@ -88,8 +88,7 @@ export default createCommand(config, async ({ values }) => { localCustomTypes.map((customType) => customType.model), { getKey: (model) => model.id, - equals: (a, b) => - JSON.stringify(canonicalizeCustomType(a)) === JSON.stringify(canonicalizeCustomType(b)), + equals: (remote, local) => JSON.stringify(canonicalizeCustomType(remote)) === JSON.stringify(local), }, ); const sliceOps = diffArrays( @@ -97,8 +96,7 @@ export default createCommand(config, async ({ values }) => { localSlices.map((slice) => slice.model), { getKey: (model) => model.id, - equals: (a, b) => - JSON.stringify(canonicalizeSlice(a)) === JSON.stringify(canonicalizeSlice(b)), + equals: (remote, local) => JSON.stringify(canonicalizeSlice(remote)) === JSON.stringify(local), }, ); diff --git a/test/pull.test.ts b/test/pull.test.ts index a306739..3b12e51 100644 --- a/test/pull.test.ts +++ b/test/pull.test.ts @@ -1,5 +1,5 @@ import { pascalCase } from "change-case"; -import { writeFile, mkdir } from "node:fs/promises"; +import { readFile, writeFile, mkdir } from "node:fs/promises"; import { sep } from "node:path"; import { fileURLToPath } from "node:url"; import { x } from "tinyexec"; @@ -250,6 +250,31 @@ it.sequential("removes route when page type is deleted", async ({ await expect(project).not.toHaveRoute({ type: customType.id }); }); +it.sequential("rewrites model files whose key order is not canonical", async ({ + expect, + project, + prismic, + repo, + token, + host, +}) => { + const customType = buildCustomType(); + await insertCustomType(customType, { repo, token, host }); + + const first = await prismic("pull", ["--repo", repo]); + expect(first.exitCode, first.stderr).toBe(0); + + const modelPath = new URL(`customtypes/${customType.id}/index.json`, project); + const canonical = await readFile(modelPath, "utf8"); + const reversed = Object.fromEntries(Object.entries(JSON.parse(canonical)).reverse()); + await writeFile(modelPath, JSON.stringify(reversed, null, 2)); + + const second = await prismic("pull", ["--repo", repo, "--force"]); + expect(second.exitCode, second.stderr).toBe(0); + expect(second.stdout).toContain("updated 1"); + expect(await readFile(modelPath, "utf8")).toBe(canonical); +}); + it.sequential("blocks pull when local model files have uncommitted changes", async ({ expect, project,