@content-collections/core 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +14 -0
- package/.turbo/turbo-test.log +47 -0
- package/.turbo/turbo-typecheck.log +4 -0
- package/LICENSE +21 -0
- package/coverage/.tmp/coverage-0.json +1 -0
- package/coverage/.tmp/coverage-1.json +1 -0
- package/coverage/.tmp/coverage-2.json +1 -0
- package/coverage/.tmp/coverage-3.json +1 -0
- package/coverage/.tmp/coverage-4.json +1 -0
- package/coverage/.tmp/coverage-5.json +1 -0
- package/coverage/.tmp/coverage-6.json +1 -0
- package/coverage/.tmp/coverage-7.json +1 -0
- package/coverage/.tmp/coverage-8.json +1 -0
- package/coverage/.tmp/coverage-9.json +1 -0
- package/coverage/base.css +224 -0
- package/coverage/block-navigation.js +87 -0
- package/coverage/builder.ts.html +424 -0
- package/coverage/clover.xml +960 -0
- package/coverage/collector.ts.html +427 -0
- package/coverage/config.ts.html +403 -0
- package/coverage/configurationReader.ts.html +409 -0
- package/coverage/coverage-final.json +11 -0
- package/coverage/events.ts.html +310 -0
- package/coverage/favicon.png +0 -0
- package/coverage/index.html +251 -0
- package/coverage/index.ts.html +106 -0
- package/coverage/prettify.css +1 -0
- package/coverage/prettify.js +2 -0
- package/coverage/sort-arrow-sprite.png +0 -0
- package/coverage/sorter.js +196 -0
- package/coverage/synchronizer.ts.html +394 -0
- package/coverage/transformer.ts.html +607 -0
- package/coverage/utils.ts.html +118 -0
- package/coverage/writer.ts.html +424 -0
- package/dist/index.cjs +416 -0
- package/dist/index.d.cts +59 -0
- package/dist/index.d.ts +146 -0
- package/dist/index.js +630 -0
- package/package.json +39 -0
- package/src/__tests__/.content-collections/cache/config.001.ts.js +19 -0
- package/src/__tests__/.content-collections/cache/config.002.mjs +16 -0
- package/src/__tests__/.content-collections/cache/config.002.ts.js +16 -0
- package/src/__tests__/.content-collections/cache/config.003.ts.js +24 -0
- package/src/__tests__/.content-collections/cache/config.004.mjs +47 -0
- package/src/__tests__/.content-collections/different-cache-dir/different.config.js +19 -0
- package/src/__tests__/.content-collections/generated-config.002/allPosts.json +13 -0
- package/src/__tests__/.content-collections/generated-config.002/index.d.ts +7 -0
- package/src/__tests__/.content-collections/generated-config.002/index.js +5 -0
- package/src/__tests__/.content-collections/generated-config.004/allAuthors.json +13 -0
- package/src/__tests__/.content-collections/generated-config.004/allPosts.json +13 -0
- package/src/__tests__/.content-collections/generated-config.004/index.d.ts +10 -0
- package/src/__tests__/.content-collections/generated-config.004/index.js +6 -0
- package/src/__tests__/collections/posts.ts +15 -0
- package/src/__tests__/config.001.ts +19 -0
- package/src/__tests__/config.002.ts +14 -0
- package/src/__tests__/config.003.ts +6 -0
- package/src/__tests__/config.004.ts +47 -0
- package/src/__tests__/invalid +1 -0
- package/src/__tests__/sources/authors/trillian.md +8 -0
- package/src/__tests__/sources/posts/first.md +6 -0
- package/src/__tests__/sources/test/001.md +5 -0
- package/src/__tests__/sources/test/002.md +5 -0
- package/src/__tests__/sources/test/broken-frontmatter +6 -0
- package/src/builder.test.ts +180 -0
- package/src/builder.ts +113 -0
- package/src/collector.test.ts +157 -0
- package/src/collector.ts +114 -0
- package/src/config.ts +106 -0
- package/src/configurationReader.test.ts +104 -0
- package/src/configurationReader.ts +108 -0
- package/src/events.test.ts +84 -0
- package/src/events.ts +75 -0
- package/src/index.ts +7 -0
- package/src/synchronizer.test.ts +192 -0
- package/src/synchronizer.ts +103 -0
- package/src/transformer.test.ts +431 -0
- package/src/transformer.ts +174 -0
- package/src/types.test.ts +137 -0
- package/src/types.ts +33 -0
- package/src/utils.test.ts +48 -0
- package/src/utils.ts +11 -0
- package/src/watcher.test.ts +200 -0
- package/src/watcher.ts +56 -0
- package/src/writer.test.ts +135 -0
- package/src/writer.ts +113 -0
- package/tsconfig.json +27 -0
- package/vite.config.ts +24 -0
package/src/events.ts
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { EventEmitter } from "node:events";
|
|
2
|
+
import { BuilderEvents } from "./builder";
|
|
3
|
+
import { CollectorEvents } from "./collector";
|
|
4
|
+
import { TransformerEvents } from "./transformer";
|
|
5
|
+
import { WatcherEvents } from "./watcher";
|
|
6
|
+
|
|
7
|
+
type EventMap = Record<string, object>;
|
|
8
|
+
|
|
9
|
+
type EventWithError = {
|
|
10
|
+
error: Error;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
type SystemEvent = {
|
|
14
|
+
_event: string;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
type ErrorEvent = EventWithError & SystemEvent;
|
|
18
|
+
|
|
19
|
+
export type Events = BuilderEvents &
|
|
20
|
+
CollectorEvents &
|
|
21
|
+
TransformerEvents &
|
|
22
|
+
WatcherEvents;
|
|
23
|
+
|
|
24
|
+
export type SystemEvents = {
|
|
25
|
+
_error: ErrorEvent;
|
|
26
|
+
_all: SystemEvent;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
type Keys<TEvents extends EventMap> = keyof TEvents & string;
|
|
30
|
+
type Listener<TEvent> = (event: TEvent) => void;
|
|
31
|
+
|
|
32
|
+
function isEventWithError(event: unknown): event is EventWithError {
|
|
33
|
+
return typeof event === "object" && event !== null && "error" in event;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function createEmitter<TEvents extends EventMap>() {
|
|
37
|
+
const emitter = new EventEmitter();
|
|
38
|
+
|
|
39
|
+
function on<TKey extends Keys<TEvents>>(
|
|
40
|
+
key: TKey,
|
|
41
|
+
listener: Listener<TEvents[TKey]>
|
|
42
|
+
): void;
|
|
43
|
+
|
|
44
|
+
function on<TKey extends Keys<SystemEvents>>(
|
|
45
|
+
key: TKey,
|
|
46
|
+
listener: Listener<SystemEvents[TKey]>
|
|
47
|
+
): void;
|
|
48
|
+
|
|
49
|
+
function on(key: string, listener: Listener<any>) {
|
|
50
|
+
emitter.on(key, listener);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function emit<TKey extends Keys<TEvents>>(key: TKey, event: TEvents[TKey]) {
|
|
54
|
+
emitter.emit(key, event);
|
|
55
|
+
|
|
56
|
+
if (isEventWithError(event)) {
|
|
57
|
+
emitter.emit("_error", {
|
|
58
|
+
...event,
|
|
59
|
+
_event: key,
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
emitter.emit("_all", {
|
|
64
|
+
...event,
|
|
65
|
+
_event: key,
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return {
|
|
70
|
+
on,
|
|
71
|
+
emit,
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export type Emitter = ReturnType<typeof createEmitter<Events>>;
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export * from "./config";
|
|
2
|
+
export * from "./builder";
|
|
3
|
+
export type { GetTypeByName, Modification } from "./types";
|
|
4
|
+
|
|
5
|
+
export { TransformError } from "./transformer";
|
|
6
|
+
export { CollectError } from "./collector";
|
|
7
|
+
export { ConfigurationError } from "./configurationReader";
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { createSynchronizer } from "./synchronizer";
|
|
3
|
+
import { CollectionFile } from "./types";
|
|
4
|
+
|
|
5
|
+
describe("synchronizer", () => {
|
|
6
|
+
async function noopCollectionFileReader(): Promise<CollectionFile | null> {
|
|
7
|
+
return null;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
function createCollectionFileReader(collectionFile: CollectionFile) {
|
|
11
|
+
return async (_: string, filePath: string) => {
|
|
12
|
+
if (filePath !== collectionFile.path) {
|
|
13
|
+
return null;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
return collectionFile;
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
it("should add new file", async () => {
|
|
21
|
+
const collection = {
|
|
22
|
+
directory: "content",
|
|
23
|
+
include: "**/*.md",
|
|
24
|
+
files: [],
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
const synchronizer = createSynchronizer(
|
|
28
|
+
createCollectionFileReader({
|
|
29
|
+
data: {
|
|
30
|
+
content: "changed"
|
|
31
|
+
},
|
|
32
|
+
path: "new.md",
|
|
33
|
+
}),
|
|
34
|
+
[collection]
|
|
35
|
+
);
|
|
36
|
+
expect(await synchronizer.changed("content/new.md")).toBe(true);
|
|
37
|
+
|
|
38
|
+
expect(collection.files.length).toBe(1);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("should delete file", () => {
|
|
42
|
+
const collection = {
|
|
43
|
+
directory: "content",
|
|
44
|
+
include: "**/*.md",
|
|
45
|
+
files: [
|
|
46
|
+
{
|
|
47
|
+
data: {
|
|
48
|
+
content: ""
|
|
49
|
+
},
|
|
50
|
+
path: "new.md",
|
|
51
|
+
},
|
|
52
|
+
],
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
const synchronizer = createSynchronizer(noopCollectionFileReader, [
|
|
56
|
+
collection,
|
|
57
|
+
]);
|
|
58
|
+
synchronizer.deleted("content/new.md");
|
|
59
|
+
|
|
60
|
+
expect(collection.files.length).toBe(0);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it("should change file", async () => {
|
|
64
|
+
const collection = {
|
|
65
|
+
directory: "content",
|
|
66
|
+
include: "**/*.md",
|
|
67
|
+
files: [
|
|
68
|
+
{
|
|
69
|
+
data: {
|
|
70
|
+
content: ""
|
|
71
|
+
},
|
|
72
|
+
path: "new.md",
|
|
73
|
+
},
|
|
74
|
+
],
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
const synchronizer = createSynchronizer(
|
|
78
|
+
createCollectionFileReader({
|
|
79
|
+
data: {
|
|
80
|
+
content: "changed"
|
|
81
|
+
},
|
|
82
|
+
path: "new.md",
|
|
83
|
+
}),
|
|
84
|
+
[collection]
|
|
85
|
+
);
|
|
86
|
+
|
|
87
|
+
expect(await synchronizer.changed("content/new.md")).toBe(true);
|
|
88
|
+
expect(collection.files[0]?.data.content).toBe("changed");
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it("should not add file, if path is not in collection directory", async () => {
|
|
92
|
+
const collection = {
|
|
93
|
+
directory: "content",
|
|
94
|
+
include: "**/*.md",
|
|
95
|
+
files: [],
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
const synchronizer = createSynchronizer(
|
|
99
|
+
createCollectionFileReader({
|
|
100
|
+
data: {
|
|
101
|
+
content: "changed"
|
|
102
|
+
},
|
|
103
|
+
path: "other/new.md",
|
|
104
|
+
}),
|
|
105
|
+
[collection]
|
|
106
|
+
);
|
|
107
|
+
expect(await synchronizer.changed("other/new.md")).toBe(false);
|
|
108
|
+
|
|
109
|
+
expect(collection.files.length).toBe(0);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it("should not add file, if path is not included", async () => {
|
|
113
|
+
const collection = {
|
|
114
|
+
directory: "content",
|
|
115
|
+
include: "**/*.md",
|
|
116
|
+
files: [],
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
const synchronizer = createSynchronizer(
|
|
120
|
+
createCollectionFileReader({
|
|
121
|
+
data: {
|
|
122
|
+
content: "changed"
|
|
123
|
+
},
|
|
124
|
+
path: "content/new.html",
|
|
125
|
+
}),
|
|
126
|
+
[collection]
|
|
127
|
+
);
|
|
128
|
+
expect(await synchronizer.changed("content/new.html")).toBe(false);
|
|
129
|
+
|
|
130
|
+
expect(collection.files.length).toBe(0);
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
it("should not delete file, if path is not in collection directory", () => {
|
|
134
|
+
const collection = {
|
|
135
|
+
directory: "content",
|
|
136
|
+
include: "**/*.md",
|
|
137
|
+
files: [
|
|
138
|
+
{
|
|
139
|
+
data: {
|
|
140
|
+
content: ""
|
|
141
|
+
},
|
|
142
|
+
path: "new.md",
|
|
143
|
+
},
|
|
144
|
+
],
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
const synchronizer = createSynchronizer(noopCollectionFileReader, [
|
|
148
|
+
collection,
|
|
149
|
+
]);
|
|
150
|
+
synchronizer.deleted("other/new.md");
|
|
151
|
+
|
|
152
|
+
expect(collection.files.length).toBe(1);
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
it("should not delete file, if path is not included", () => {
|
|
156
|
+
const collection = {
|
|
157
|
+
directory: "content",
|
|
158
|
+
include: "**/*.md",
|
|
159
|
+
files: [
|
|
160
|
+
{
|
|
161
|
+
data: {
|
|
162
|
+
content: ""
|
|
163
|
+
},
|
|
164
|
+
path: "new.md",
|
|
165
|
+
},
|
|
166
|
+
],
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
const synchronizer = createSynchronizer(noopCollectionFileReader, [
|
|
170
|
+
collection,
|
|
171
|
+
]);
|
|
172
|
+
synchronizer.deleted("content/new.html");
|
|
173
|
+
|
|
174
|
+
expect(collection.files.length).toBe(1);
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
it("should not sync file, if file could not be read", async () => {
|
|
178
|
+
const collection = {
|
|
179
|
+
directory: "content",
|
|
180
|
+
include: "**/*.md",
|
|
181
|
+
files: [],
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
const synchronizer = createSynchronizer(
|
|
185
|
+
noopCollectionFileReader,
|
|
186
|
+
[collection]
|
|
187
|
+
);
|
|
188
|
+
|
|
189
|
+
expect(await synchronizer.changed("content/new.md")).toBe(false);
|
|
190
|
+
expect(collection.files.length).toBe(0);
|
|
191
|
+
});
|
|
192
|
+
});
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import micromatch from "micromatch";
|
|
2
|
+
import { CollectionFile, FileCollection, ResolvedCollection } from "./types";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
|
|
5
|
+
type CollectionFileReader = (
|
|
6
|
+
directory: string,
|
|
7
|
+
filePath: string
|
|
8
|
+
) => Promise<CollectionFile | null>;
|
|
9
|
+
|
|
10
|
+
export function createSynchronizer<T extends FileCollection>(
|
|
11
|
+
readCollectionFile: CollectionFileReader,
|
|
12
|
+
collections: Array<ResolvedCollection<T>>,
|
|
13
|
+
baseDirectory: string = "."
|
|
14
|
+
) {
|
|
15
|
+
function findCollection(filePath: string) {
|
|
16
|
+
const resolvedFilePath = path.resolve(filePath);
|
|
17
|
+
return collections.find((collection) => {
|
|
18
|
+
return resolvedFilePath.startsWith(
|
|
19
|
+
path.resolve(baseDirectory, collection.directory)
|
|
20
|
+
);
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function createRelativePath(collectionPath: string, filePath: string) {
|
|
25
|
+
const resolvedCollectionPath = path.resolve(baseDirectory, collectionPath);
|
|
26
|
+
const resolvedFilePath = path.resolve(filePath);
|
|
27
|
+
|
|
28
|
+
let relativePath = resolvedFilePath.slice(resolvedCollectionPath.length);
|
|
29
|
+
if (relativePath.startsWith("/")) {
|
|
30
|
+
relativePath = relativePath.slice(1);
|
|
31
|
+
}
|
|
32
|
+
return relativePath;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function resolve(filePath: string) {
|
|
36
|
+
const collection = findCollection(filePath);
|
|
37
|
+
if (!collection) {
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const relativePath = createRelativePath(collection.directory, filePath);
|
|
42
|
+
if (!micromatch.isMatch(relativePath, collection.include)) {
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return {
|
|
47
|
+
collection,
|
|
48
|
+
relativePath,
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function deleted(filePath: string) {
|
|
53
|
+
const resolved = resolve(filePath);
|
|
54
|
+
if (!resolved) {
|
|
55
|
+
return false;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const { collection, relativePath } = resolved;
|
|
59
|
+
|
|
60
|
+
const index = collection.files.findIndex(
|
|
61
|
+
(file) => file.path === relativePath
|
|
62
|
+
);
|
|
63
|
+
const deleted = collection.files.splice(index, 1);
|
|
64
|
+
|
|
65
|
+
return deleted.length > 0;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
async function changed(filePath: string) {
|
|
69
|
+
const resolved = resolve(filePath);
|
|
70
|
+
if (!resolved) {
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const { collection, relativePath } = resolved;
|
|
75
|
+
const index = collection.files.findIndex(
|
|
76
|
+
(file) => file.path === relativePath
|
|
77
|
+
);
|
|
78
|
+
|
|
79
|
+
const file = await readCollectionFile(
|
|
80
|
+
path.join(baseDirectory, collection.directory),
|
|
81
|
+
relativePath
|
|
82
|
+
);
|
|
83
|
+
|
|
84
|
+
if (!file) {
|
|
85
|
+
return false;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (index === -1) {
|
|
89
|
+
collection.files.push(file);
|
|
90
|
+
} else {
|
|
91
|
+
collection.files[index] = file;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return true;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
return {
|
|
98
|
+
deleted,
|
|
99
|
+
changed,
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export type Synchronizer = ReturnType<typeof createSynchronizer>;
|