diff --git a/.changeset/little-bobcats-applaud.md b/.changeset/little-bobcats-applaud.md new file mode 100644 index 00000000..781d7ab5 --- /dev/null +++ b/.changeset/little-bobcats-applaud.md @@ -0,0 +1,5 @@ +--- +"@content-collections/core": patch +--- + +Fixed watch on windows diff --git a/packages/core/src/synchronizer.test.ts b/packages/core/src/synchronizer.test.ts index 4ca2300d..57304f11 100644 --- a/packages/core/src/synchronizer.test.ts +++ b/packages/core/src/synchronizer.test.ts @@ -1,4 +1,4 @@ -import { describe, it, expect } from "vitest"; +import { describe, it, expect, vitest } from "vitest"; import { createSynchronizer } from "./synchronizer"; import { CollectionFile, FileCollection, ResolvedCollection } from "./types"; @@ -318,4 +318,79 @@ describe("synchronizer", () => { expect(collection.files.length).toBe(0); }); + + describe("posix", () => { + + it("should add a new file", async () => { + + vitest.mock("node:path", async (importOriginal) => { + const origin = await importOriginal(); + return { + default: { + ...origin, + sep: origin.posix.sep, + resolve: origin.posix.resolve, + }, + }; + }); + + const collection = { + directory: "content", + include: "**/*.md", + files: [], + }; + + const synchronizer = createSynchronizer( + createCollectionFileReader({ + data: { + content: "changed", + }, + path: "new.md", + }), + [collection] + ); + expect(await synchronizer.changed("content/new.md")).toBe(true); + + expect(collection.files.length).toBe(1); + }); + + }); + + describe("windows", () => { + + it("should add a new file", async () => { + + vitest.mock("node:path", async (importOriginal) => { + const origin = await importOriginal(); + return { + default: { + ...origin, + sep: origin.win32.sep, + resolve: origin.win32.resolve, + }, + }; + }); + + const collection = { + directory: "content", + include: "**/*.md", + files: [], + }; + + const synchronizer = createSynchronizer( + createCollectionFileReader({ + data: { + content: "changed", + }, + path: "new.md", + }), + [collection] + ); + expect(await synchronizer.changed("content\\new.md")).toBe(true); + + expect(collection.files.length).toBe(1); + }); + + }); + }); diff --git a/packages/core/src/synchronizer.ts b/packages/core/src/synchronizer.ts index af240505..d40998da 100644 --- a/packages/core/src/synchronizer.ts +++ b/packages/core/src/synchronizer.ts @@ -27,8 +27,8 @@ export function createSynchronizer( const resolvedFilePath = path.resolve(filePath); let relativePath = resolvedFilePath.slice(resolvedCollectionPath.length); - if (relativePath.startsWith("/")) { - relativePath = relativePath.slice(1); + if (relativePath.startsWith(path.sep)) { + relativePath = relativePath.slice(path.sep.length); } return relativePath; }