Skip to content

Commit

Permalink
fix(core): #227 watch on windows
Browse files Browse the repository at this point in the history
Remove leading directory separator from path instead of hard coded '/'.

Closes: #227
  • Loading branch information
sdorra committed Jul 26, 2024
1 parent 7b804f3 commit 8dc1da0
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/little-bobcats-applaud.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@content-collections/core": patch
---

Fixed watch on windows
77 changes: 76 additions & 1 deletion packages/core/src/synchronizer.test.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand Down Expand Up @@ -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<typeof import("node:path")>();
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<typeof import("node:path")>();
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);
});

});

});
4 changes: 2 additions & 2 deletions packages/core/src/synchronizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ export function createSynchronizer<T extends FileCollection>(
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;
}
Expand Down

0 comments on commit 8dc1da0

Please sign in to comment.