From 58e659bef0f6604f4c3f991ae4d4c64c0350fc59 Mon Sep 17 00:00:00 2001 From: Sebastian Sdorra Date: Fri, 12 Apr 2024 12:56:42 +0200 Subject: [PATCH] feat(core): add option to exclude files from collection --- .changeset/silver-planets-warn.md | 5 ++++ packages/core/src/collector.test.ts | 41 +++++++++++++++++++++++++++++ packages/core/src/collector.ts | 13 +++++++++ packages/core/src/config.ts | 1 + packages/core/src/types.ts | 2 +- 5 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 .changeset/silver-planets-warn.md diff --git a/.changeset/silver-planets-warn.md b/.changeset/silver-planets-warn.md new file mode 100644 index 00000000..132771c5 --- /dev/null +++ b/.changeset/silver-planets-warn.md @@ -0,0 +1,5 @@ +--- +"@content-collections/core": minor +--- + +Add option to exclude files from collection diff --git a/packages/core/src/collector.test.ts b/packages/core/src/collector.test.ts index f2798ebc..490a195e 100644 --- a/packages/core/src/collector.test.ts +++ b/packages/core/src/collector.test.ts @@ -181,6 +181,45 @@ describe("collector", () => { expect(collections).toHaveLength(2); }); + + it("should exclude file", async () => { + const [collection] = await collect([ + { + directory: "./__tests__/sources/test/", + include: "*.md", + exclude: "002.md", + parser: "frontmatter", + }, + ]); + + expect(collection?.files).toHaveLength(1); + }); + + it("should exclude multiple files", async () => { + const [collection] = await collect([ + { + directory: "./__tests__/sources/test/", + include: "*.md", + exclude: ["001.md", "002.md"], + parser: "frontmatter", + }, + ]); + + expect(collection?.files).toHaveLength(0); + }); + + it("should exclude files which matches glob", async () => { + const [collection] = await collect([ + { + directory: "./__tests__/sources/test/", + include: "*.md", + exclude: "00?.md", + parser: "frontmatter", + }, + ]); + + expect(collection?.files).toHaveLength(0); + }); }); it("should treat dates as string", async () => { @@ -197,4 +236,6 @@ describe("collector", () => { expect(typeof file.data.published).toBe("string"); }); + + }); diff --git a/packages/core/src/collector.ts b/packages/core/src/collector.ts index fbcd21b6..24dedca9 100644 --- a/packages/core/src/collector.ts +++ b/packages/core/src/collector.ts @@ -67,12 +67,25 @@ export function createCollector(emitter: Emitter, baseDirectory: string = ".") { } } + function createIgnorePattern(collection: T): Array | undefined { + if (collection.exclude) { + if (Array.isArray(collection.exclude)) { + return collection.exclude; + } else { + return [collection.exclude]; + } + } + return undefined; + } + async function resolveCollection(collection: T) { const collectionDirectory = path.join(baseDirectory, collection.directory); + const filePaths = await fg(collection.include, { cwd: collectionDirectory, onlyFiles: true, absolute: false, + ignore: createIgnorePattern(collection), }); const promises = filePaths.map((filePath) => collectFile(collection, filePath) diff --git a/packages/core/src/config.ts b/packages/core/src/config.ts index 0fa93cc8..7f103de2 100644 --- a/packages/core/src/config.ts +++ b/packages/core/src/config.ts @@ -65,6 +65,7 @@ export type CollectionRequest< transform?: (data: TSchema, context: Context) => TTransformResult; directory: string; include: string | string[]; + exclude?: string | string[]; onSuccess?: (documents: Array) => void | Promise; }; diff --git a/packages/core/src/types.ts b/packages/core/src/types.ts index 843b6dae..35437b9b 100644 --- a/packages/core/src/types.ts +++ b/packages/core/src/types.ts @@ -13,7 +13,7 @@ export type CollectionFile = { export type FileCollection = Pick< AnyCollection, - "directory" | "include" | "parser" + "directory" | "include" | "exclude" | "parser" >; export type ResolvedCollection = T & {