Skip to content

Commit

Permalink
feat(shared): add statistics to build output
Browse files Browse the repository at this point in the history
Show count of collections and documents to the output of build:end
  • Loading branch information
sdorra committed Aug 3, 2024
1 parent 207a3de commit a1dd55b
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 3 deletions.
6 changes: 6 additions & 0 deletions .changeset/real-peaches-smash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@content-collections/integrations": minor
"@content-collections/core": minor
---

Add count of collections and documents to build output
22 changes: 20 additions & 2 deletions packages/core/src/builder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { createBuilder as origCreateBuilder } from "./builder";
import path from "node:path";
import { existsSync } from "node:fs";
import fs from "node:fs/promises";
import { createEmitter, type Emitter } from "./events";
import { createEmitter, Events, type Emitter } from "./events";

describe("builder", () => {
afterEach(async () => {
Expand Down Expand Up @@ -111,7 +111,25 @@ describe("builder", () => {
expect(events).toEqual(["builder:start", "builder:end"]);
});

it("should resolve to default output directory", async () => {});
it("should report some statistics on build:end", async () => {
const { builder } = await createBuilder("config.002");
const events: Array<Events["builder:end"]> = [];

builder.on("builder:end", (event) => {
events.push(event);
});

await builder.build();

const event = events[0];
if (!event) {
throw new Error("Event is undefined");
}

expect(event.stats).toBeDefined();
expect(event.stats.collections).toBe(1);
expect(event.stats.documents).toBe(1);
});
});

describe("sync", () => {
Expand Down
14 changes: 14 additions & 0 deletions packages/core/src/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ export type BuilderEvents = {
"builder:end": {
startedAt: number;
endedAt: number;
stats: {
collections: number;
documents: number;
},
};
};

Expand Down Expand Up @@ -169,9 +173,19 @@ export async function createInternalBuilder(

await Promise.all(pendingOnSuccess.filter(isDefined));

const stats = collections.reduce((acc, collection) => {
acc.collections++;
acc.documents += collection.documents.length;
return acc;
}, {
collections: 0,
documents: 0
});

emitter.emit("builder:end", {
startedAt,
endedAt: Date.now(),
stats
});
}

Expand Down
8 changes: 7 additions & 1 deletion packages/integrations/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ export function configureLogging(builder: Builder) {

builder.on("builder:end", (event) => {
console.log(
"... finished build in",
"... finished build of",
event.stats.collections,
event.stats.collections === 1 ? "collection" : "collections",
"and",
event.stats.documents,
event.stats.documents === 1 ? "document" : "documents",
"in",
event.endedAt - event.startedAt + "ms"
);
});
Expand Down

0 comments on commit a1dd55b

Please sign in to comment.