@content-collections/core 0.5.0 → 0.6.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/dist/index.d.ts +12 -9
- package/dist/index.js +57 -38
- package/package.json +9 -4
package/dist/index.d.ts
CHANGED
|
@@ -23,13 +23,17 @@ declare const parsers: {
|
|
|
23
23
|
|
|
24
24
|
type CacheFn = <TInput, TOutput>(input: TInput, compute: (input: TInput) => Promise<TOutput> | TOutput) => Promise<TOutput>;
|
|
25
25
|
|
|
26
|
-
declare const literalSchema: z.ZodUnion<[z.ZodString, z.ZodNumber, z.ZodBoolean, z.ZodNull, z.ZodUndefined]>;
|
|
26
|
+
declare const literalSchema: z.ZodUnion<[z.ZodString, z.ZodNumber, z.ZodBoolean, z.ZodNull, z.ZodUndefined, z.ZodDate, z.ZodMap<z.ZodUnknown, z.ZodUnknown>, z.ZodSet<z.ZodUnknown>, z.ZodBigInt]>;
|
|
27
27
|
type Literal = z.infer<typeof literalSchema>;
|
|
28
|
-
type
|
|
29
|
-
[key: string]:
|
|
30
|
-
} |
|
|
31
|
-
|
|
32
|
-
|
|
28
|
+
type SchemaType = Literal | {
|
|
29
|
+
[key: string]: SchemaType;
|
|
30
|
+
} | SchemaType[];
|
|
31
|
+
type NotSerializableError = `The return type of the transform function must be an object serializable object.
|
|
32
|
+
See https://www.content-collections.dev/docs/serialization for more information.
|
|
33
|
+
|
|
34
|
+
The following type is not valid:`;
|
|
35
|
+
declare const serializableSchema: z.ZodRecord<z.ZodString, z.ZodType<SchemaType, z.ZodTypeDef, SchemaType>>;
|
|
36
|
+
type Serializable = z.infer<typeof serializableSchema>;
|
|
33
37
|
|
|
34
38
|
type Meta = {
|
|
35
39
|
filePath: string;
|
|
@@ -75,13 +79,12 @@ type Collection<TName extends string, TShape extends ZodRawShape, TParser extend
|
|
|
75
79
|
parser: TParser;
|
|
76
80
|
};
|
|
77
81
|
type AnyCollection = Collection<any, ZodRawShape, Parser, any, any, any>;
|
|
78
|
-
type NonJSONObjectError = "The return type of the transform function must be an valid JSONObject, the following type is not valid:";
|
|
79
82
|
declare const InvalidReturnTypeSymbol: unique symbol;
|
|
80
83
|
type InvalidReturnType<TMessage extends string, TObject> = {
|
|
81
84
|
[InvalidReturnTypeSymbol]: TMessage;
|
|
82
85
|
object: TObject;
|
|
83
86
|
};
|
|
84
|
-
declare function defineCollection<TName extends string, TShape extends ZodRawShape, TParser extends Parser = "frontmatter", TSchema = Schema<TParser, TShape>, TTransformResult = never, TDocument = [TTransformResult] extends [never] ? Schema<TParser, TShape> : Awaited<TTransformResult>, TResult = TDocument extends
|
|
87
|
+
declare function defineCollection<TName extends string, TShape extends ZodRawShape, TParser extends Parser = "frontmatter", TSchema = Schema<TParser, TShape>, TTransformResult = never, TDocument = [TTransformResult] extends [never] ? Schema<TParser, TShape> : Awaited<TTransformResult>, TResult = TDocument extends Serializable ? Collection<TName, TShape, TParser, TSchema, TTransformResult, TDocument> : InvalidReturnType<NotSerializableError, TDocument>>(collection: CollectionRequest<TName, TShape, TParser, TSchema, TTransformResult, TDocument>): TResult;
|
|
85
88
|
type Cache = "memory" | "file" | "none";
|
|
86
89
|
type Configuration<TCollections extends Array<AnyCollection>> = {
|
|
87
90
|
collections: TCollections;
|
|
@@ -200,4 +203,4 @@ declare function createBuilder(configurationPath: string, options?: Options): Pr
|
|
|
200
203
|
}>;
|
|
201
204
|
type Builder = Awaited<ReturnType<typeof createBuilder>>;
|
|
202
205
|
|
|
203
|
-
export { AnyCollection, AnyConfiguration, Builder, BuilderEvents, CollectError, Collection, CollectionRequest, Configuration, ConfigurationError, Context, Document, GetTypeByName, Meta, Modification, Schema, TransformError, createBuilder, defineCollection, defineConfig };
|
|
206
|
+
export { type AnyCollection, type AnyConfiguration, type Builder, type BuilderEvents, CollectError, type Collection, type CollectionRequest, type Configuration, ConfigurationError, type Context, type Document, type GetTypeByName, type Meta, type Modification, type Schema, TransformError, createBuilder, defineCollection, defineConfig };
|
package/dist/index.js
CHANGED
|
@@ -45,7 +45,7 @@ import path from "path";
|
|
|
45
45
|
// package.json
|
|
46
46
|
var package_default = {
|
|
47
47
|
name: "@content-collections/core",
|
|
48
|
-
version: "0.
|
|
48
|
+
version: "0.6.0",
|
|
49
49
|
type: "module",
|
|
50
50
|
main: "dist/index.js",
|
|
51
51
|
types: "./dist/index.d.ts",
|
|
@@ -67,12 +67,16 @@ var package_default = {
|
|
|
67
67
|
prepack: "cp ../../README.md ./README.md",
|
|
68
68
|
postpack: "rm -f ./README.md"
|
|
69
69
|
},
|
|
70
|
+
peerDependencies: {
|
|
71
|
+
typescript: "^5.0.2"
|
|
72
|
+
},
|
|
70
73
|
devDependencies: {
|
|
71
74
|
"@types/micromatch": "^4.0.7",
|
|
72
|
-
"@types/node": "^20.
|
|
75
|
+
"@types/node": "^20.14.2",
|
|
73
76
|
"@types/pluralize": "^0.0.33",
|
|
77
|
+
"@types/serialize-javascript": "^5.0.4",
|
|
74
78
|
"@vitest/coverage-v8": "^1.5.0",
|
|
75
|
-
tsup: "^
|
|
79
|
+
tsup: "^8.0.2",
|
|
76
80
|
tsx: "^4.1.1",
|
|
77
81
|
typescript: "^5.4.5",
|
|
78
82
|
vitest: "^1.5.0"
|
|
@@ -80,11 +84,12 @@ var package_default = {
|
|
|
80
84
|
dependencies: {
|
|
81
85
|
"@parcel/watcher": "^2.4.1",
|
|
82
86
|
camelcase: "^8.0.0",
|
|
83
|
-
esbuild: "^0.
|
|
87
|
+
esbuild: "^0.21.4",
|
|
84
88
|
"fast-glob": "^3.3.2",
|
|
85
89
|
"gray-matter": "^4.0.3",
|
|
86
90
|
micromatch: "^4.0.5",
|
|
87
91
|
pluralize: "^8.0.0",
|
|
92
|
+
"serialize-javascript": "^6.0.2",
|
|
88
93
|
yaml: "^2.4.1",
|
|
89
94
|
zod: "^3.22.5"
|
|
90
95
|
}
|
|
@@ -288,6 +293,38 @@ function createCollector(emitter, baseDirectory = ".") {
|
|
|
288
293
|
import fs2 from "fs/promises";
|
|
289
294
|
import path3 from "path";
|
|
290
295
|
import pluralize2 from "pluralize";
|
|
296
|
+
|
|
297
|
+
// src/serializer.ts
|
|
298
|
+
import z2 from "zod";
|
|
299
|
+
import serializeJs from "serialize-javascript";
|
|
300
|
+
var literalSchema = z2.union([
|
|
301
|
+
// json
|
|
302
|
+
z2.string(),
|
|
303
|
+
z2.number(),
|
|
304
|
+
z2.boolean(),
|
|
305
|
+
z2.null(),
|
|
306
|
+
// serializable-javascript
|
|
307
|
+
z2.undefined(),
|
|
308
|
+
z2.date(),
|
|
309
|
+
z2.map(z2.unknown(), z2.unknown()),
|
|
310
|
+
z2.set(z2.unknown()),
|
|
311
|
+
z2.bigint()
|
|
312
|
+
]);
|
|
313
|
+
var schema = z2.lazy(
|
|
314
|
+
() => z2.union([literalSchema, z2.array(schema), z2.record(schema)])
|
|
315
|
+
);
|
|
316
|
+
var extension = "js";
|
|
317
|
+
var serializableSchema = z2.record(schema);
|
|
318
|
+
function serialize(value) {
|
|
319
|
+
const serializedValue = serializeJs(value, {
|
|
320
|
+
space: 2,
|
|
321
|
+
unsafe: true,
|
|
322
|
+
ignoreFunction: true
|
|
323
|
+
});
|
|
324
|
+
return `export default ${serializedValue};`;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
// src/writer.ts
|
|
291
328
|
function createArrayConstName(name) {
|
|
292
329
|
let suffix = name.charAt(0).toUpperCase() + name.slice(1);
|
|
293
330
|
return "all" + pluralize2(suffix);
|
|
@@ -295,15 +332,11 @@ function createArrayConstName(name) {
|
|
|
295
332
|
async function createDataFile(directory, collection) {
|
|
296
333
|
const dataPath = path3.join(
|
|
297
334
|
directory,
|
|
298
|
-
`${createArrayConstName(collection.name)}
|
|
335
|
+
`${createArrayConstName(collection.name)}.${extension}`
|
|
299
336
|
);
|
|
300
337
|
await fs2.writeFile(
|
|
301
338
|
dataPath,
|
|
302
|
-
|
|
303
|
-
collection.documents.map((doc) => doc.document),
|
|
304
|
-
null,
|
|
305
|
-
2
|
|
306
|
-
)
|
|
339
|
+
serialize(collection.documents.map((doc) => doc.document))
|
|
307
340
|
);
|
|
308
341
|
}
|
|
309
342
|
function createDataFiles(directory, collections) {
|
|
@@ -319,7 +352,7 @@ async function createJavaScriptFile(directory, configuration) {
|
|
|
319
352
|
|
|
320
353
|
`;
|
|
321
354
|
for (const name of collections) {
|
|
322
|
-
content += `import ${name} from "./${name}
|
|
355
|
+
content += `import ${name} from "./${name}.${extension}";
|
|
323
356
|
`;
|
|
324
357
|
}
|
|
325
358
|
content += "\n";
|
|
@@ -327,7 +360,9 @@ async function createJavaScriptFile(directory, configuration) {
|
|
|
327
360
|
await fs2.writeFile(path3.join(directory, "index.js"), content, "utf-8");
|
|
328
361
|
}
|
|
329
362
|
function createImportPath(directory, target) {
|
|
330
|
-
let importPath = path3.posix.join(
|
|
363
|
+
let importPath = path3.posix.join(
|
|
364
|
+
...path3.relative(directory, target).split(path3.sep)
|
|
365
|
+
);
|
|
331
366
|
if (!importPath.startsWith(".")) {
|
|
332
367
|
importPath = "./" + importPath;
|
|
333
368
|
}
|
|
@@ -368,22 +403,6 @@ async function createWriter(directory) {
|
|
|
368
403
|
// src/transformer.ts
|
|
369
404
|
import { basename, dirname, extname } from "path";
|
|
370
405
|
import { z as z3 } from "zod";
|
|
371
|
-
|
|
372
|
-
// src/json.ts
|
|
373
|
-
import z2 from "zod";
|
|
374
|
-
var literalSchema = z2.union([
|
|
375
|
-
z2.string(),
|
|
376
|
-
z2.number(),
|
|
377
|
-
z2.boolean(),
|
|
378
|
-
z2.null(),
|
|
379
|
-
z2.undefined()
|
|
380
|
-
]);
|
|
381
|
-
var jsonSchema = z2.lazy(
|
|
382
|
-
() => z2.union([literalSchema, z2.array(jsonSchema), z2.record(jsonSchema)])
|
|
383
|
-
);
|
|
384
|
-
var jsonObjectScheme = z2.record(jsonSchema);
|
|
385
|
-
|
|
386
|
-
// src/transformer.ts
|
|
387
406
|
var TransformError = class extends Error {
|
|
388
407
|
type;
|
|
389
408
|
constructor(type, message) {
|
|
@@ -399,20 +418,20 @@ function createPath(path7, ext) {
|
|
|
399
418
|
return p;
|
|
400
419
|
}
|
|
401
420
|
function createTransformer(emitter, cacheManager) {
|
|
402
|
-
function createSchema(parserName,
|
|
421
|
+
function createSchema(parserName, schema2) {
|
|
403
422
|
const parser = parsers[parserName];
|
|
404
423
|
if (!parser.hasContent) {
|
|
405
|
-
return z3.object(
|
|
424
|
+
return z3.object(schema2);
|
|
406
425
|
}
|
|
407
426
|
return z3.object({
|
|
408
427
|
content: z3.string(),
|
|
409
|
-
...
|
|
428
|
+
...schema2
|
|
410
429
|
});
|
|
411
430
|
}
|
|
412
431
|
async function parseFile(collection, file) {
|
|
413
432
|
const { data, path: path7 } = file;
|
|
414
|
-
const
|
|
415
|
-
let parsedData = await
|
|
433
|
+
const schema2 = createSchema(collection.parser, collection.schema);
|
|
434
|
+
let parsedData = await schema2.safeParseAsync(data);
|
|
416
435
|
if (!parsedData.success) {
|
|
417
436
|
emitter.emit("transformer:validation-error", {
|
|
418
437
|
collection,
|
|
@@ -422,9 +441,9 @@ function createTransformer(emitter, cacheManager) {
|
|
|
422
441
|
return null;
|
|
423
442
|
}
|
|
424
443
|
const ext = extname(path7);
|
|
425
|
-
let
|
|
426
|
-
if (
|
|
427
|
-
|
|
444
|
+
let extension2 = ext;
|
|
445
|
+
if (extension2.startsWith(".")) {
|
|
446
|
+
extension2 = extension2.slice(1);
|
|
428
447
|
}
|
|
429
448
|
const document = {
|
|
430
449
|
...parsedData.data,
|
|
@@ -432,7 +451,7 @@ function createTransformer(emitter, cacheManager) {
|
|
|
432
451
|
filePath: path7,
|
|
433
452
|
fileName: basename(path7),
|
|
434
453
|
directory: dirname(path7),
|
|
435
|
-
extension,
|
|
454
|
+
extension: extension2,
|
|
436
455
|
path: createPath(path7, ext)
|
|
437
456
|
}
|
|
438
457
|
};
|
|
@@ -506,7 +525,7 @@ function createTransformer(emitter, cacheManager) {
|
|
|
506
525
|
async function validateDocuments(collection, documents) {
|
|
507
526
|
const docs = [];
|
|
508
527
|
for (const doc of documents) {
|
|
509
|
-
let parsedData = await
|
|
528
|
+
let parsedData = await serializableSchema.safeParseAsync(doc.document);
|
|
510
529
|
if (parsedData.success) {
|
|
511
530
|
docs.push(doc);
|
|
512
531
|
} else {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@content-collections/core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -15,12 +15,16 @@
|
|
|
15
15
|
"dist",
|
|
16
16
|
"README.md"
|
|
17
17
|
],
|
|
18
|
+
"peerDependencies": {
|
|
19
|
+
"typescript": "^5.0.2"
|
|
20
|
+
},
|
|
18
21
|
"devDependencies": {
|
|
19
22
|
"@types/micromatch": "^4.0.7",
|
|
20
|
-
"@types/node": "^20.
|
|
23
|
+
"@types/node": "^20.14.2",
|
|
21
24
|
"@types/pluralize": "^0.0.33",
|
|
25
|
+
"@types/serialize-javascript": "^5.0.4",
|
|
22
26
|
"@vitest/coverage-v8": "^1.5.0",
|
|
23
|
-
"tsup": "^
|
|
27
|
+
"tsup": "^8.0.2",
|
|
24
28
|
"tsx": "^4.1.1",
|
|
25
29
|
"typescript": "^5.4.5",
|
|
26
30
|
"vitest": "^1.5.0"
|
|
@@ -28,11 +32,12 @@
|
|
|
28
32
|
"dependencies": {
|
|
29
33
|
"@parcel/watcher": "^2.4.1",
|
|
30
34
|
"camelcase": "^8.0.0",
|
|
31
|
-
"esbuild": "^0.
|
|
35
|
+
"esbuild": "^0.21.4",
|
|
32
36
|
"fast-glob": "^3.3.2",
|
|
33
37
|
"gray-matter": "^4.0.3",
|
|
34
38
|
"micromatch": "^4.0.5",
|
|
35
39
|
"pluralize": "^8.0.0",
|
|
40
|
+
"serialize-javascript": "^6.0.2",
|
|
36
41
|
"yaml": "^2.4.1",
|
|
37
42
|
"zod": "^3.22.5"
|
|
38
43
|
},
|