@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.
Files changed (87) hide show
  1. package/.turbo/turbo-build.log +14 -0
  2. package/.turbo/turbo-test.log +47 -0
  3. package/.turbo/turbo-typecheck.log +4 -0
  4. package/LICENSE +21 -0
  5. package/coverage/.tmp/coverage-0.json +1 -0
  6. package/coverage/.tmp/coverage-1.json +1 -0
  7. package/coverage/.tmp/coverage-2.json +1 -0
  8. package/coverage/.tmp/coverage-3.json +1 -0
  9. package/coverage/.tmp/coverage-4.json +1 -0
  10. package/coverage/.tmp/coverage-5.json +1 -0
  11. package/coverage/.tmp/coverage-6.json +1 -0
  12. package/coverage/.tmp/coverage-7.json +1 -0
  13. package/coverage/.tmp/coverage-8.json +1 -0
  14. package/coverage/.tmp/coverage-9.json +1 -0
  15. package/coverage/base.css +224 -0
  16. package/coverage/block-navigation.js +87 -0
  17. package/coverage/builder.ts.html +424 -0
  18. package/coverage/clover.xml +960 -0
  19. package/coverage/collector.ts.html +427 -0
  20. package/coverage/config.ts.html +403 -0
  21. package/coverage/configurationReader.ts.html +409 -0
  22. package/coverage/coverage-final.json +11 -0
  23. package/coverage/events.ts.html +310 -0
  24. package/coverage/favicon.png +0 -0
  25. package/coverage/index.html +251 -0
  26. package/coverage/index.ts.html +106 -0
  27. package/coverage/prettify.css +1 -0
  28. package/coverage/prettify.js +2 -0
  29. package/coverage/sort-arrow-sprite.png +0 -0
  30. package/coverage/sorter.js +196 -0
  31. package/coverage/synchronizer.ts.html +394 -0
  32. package/coverage/transformer.ts.html +607 -0
  33. package/coverage/utils.ts.html +118 -0
  34. package/coverage/writer.ts.html +424 -0
  35. package/dist/index.cjs +416 -0
  36. package/dist/index.d.cts +59 -0
  37. package/dist/index.d.ts +146 -0
  38. package/dist/index.js +630 -0
  39. package/package.json +39 -0
  40. package/src/__tests__/.content-collections/cache/config.001.ts.js +19 -0
  41. package/src/__tests__/.content-collections/cache/config.002.mjs +16 -0
  42. package/src/__tests__/.content-collections/cache/config.002.ts.js +16 -0
  43. package/src/__tests__/.content-collections/cache/config.003.ts.js +24 -0
  44. package/src/__tests__/.content-collections/cache/config.004.mjs +47 -0
  45. package/src/__tests__/.content-collections/different-cache-dir/different.config.js +19 -0
  46. package/src/__tests__/.content-collections/generated-config.002/allPosts.json +13 -0
  47. package/src/__tests__/.content-collections/generated-config.002/index.d.ts +7 -0
  48. package/src/__tests__/.content-collections/generated-config.002/index.js +5 -0
  49. package/src/__tests__/.content-collections/generated-config.004/allAuthors.json +13 -0
  50. package/src/__tests__/.content-collections/generated-config.004/allPosts.json +13 -0
  51. package/src/__tests__/.content-collections/generated-config.004/index.d.ts +10 -0
  52. package/src/__tests__/.content-collections/generated-config.004/index.js +6 -0
  53. package/src/__tests__/collections/posts.ts +15 -0
  54. package/src/__tests__/config.001.ts +19 -0
  55. package/src/__tests__/config.002.ts +14 -0
  56. package/src/__tests__/config.003.ts +6 -0
  57. package/src/__tests__/config.004.ts +47 -0
  58. package/src/__tests__/invalid +1 -0
  59. package/src/__tests__/sources/authors/trillian.md +8 -0
  60. package/src/__tests__/sources/posts/first.md +6 -0
  61. package/src/__tests__/sources/test/001.md +5 -0
  62. package/src/__tests__/sources/test/002.md +5 -0
  63. package/src/__tests__/sources/test/broken-frontmatter +6 -0
  64. package/src/builder.test.ts +180 -0
  65. package/src/builder.ts +113 -0
  66. package/src/collector.test.ts +157 -0
  67. package/src/collector.ts +114 -0
  68. package/src/config.ts +106 -0
  69. package/src/configurationReader.test.ts +104 -0
  70. package/src/configurationReader.ts +108 -0
  71. package/src/events.test.ts +84 -0
  72. package/src/events.ts +75 -0
  73. package/src/index.ts +7 -0
  74. package/src/synchronizer.test.ts +192 -0
  75. package/src/synchronizer.ts +103 -0
  76. package/src/transformer.test.ts +431 -0
  77. package/src/transformer.ts +174 -0
  78. package/src/types.test.ts +137 -0
  79. package/src/types.ts +33 -0
  80. package/src/utils.test.ts +48 -0
  81. package/src/utils.ts +11 -0
  82. package/src/watcher.test.ts +200 -0
  83. package/src/watcher.ts +56 -0
  84. package/src/writer.test.ts +135 -0
  85. package/src/writer.ts +113 -0
  86. package/tsconfig.json +27 -0
  87. package/vite.config.ts +24 -0
package/src/writer.ts ADDED
@@ -0,0 +1,113 @@
1
+ import fs from "node:fs/promises";
2
+ import path from "node:path";
3
+ import pluralize from "pluralize";
4
+ import { TransformedCollection } from "./transformer";
5
+ import { InternalConfiguration } from "./configurationReader";
6
+
7
+ function createArrayConstName(name: string) {
8
+ let suffix = name.charAt(0).toUpperCase() + name.slice(1);
9
+ return "all" + pluralize(suffix);
10
+ }
11
+
12
+ type DataFileCollection = Pick<TransformedCollection, "name" | "documents">;
13
+
14
+ async function createDataFile(
15
+ directory: string,
16
+ collection: DataFileCollection
17
+ ) {
18
+ const dataPath = path.join(
19
+ directory,
20
+ `${createArrayConstName(collection.name)}.json`
21
+ );
22
+
23
+ await fs.writeFile(
24
+ dataPath,
25
+ JSON.stringify(
26
+ collection.documents.map((doc) => doc.document),
27
+ null,
28
+ 2
29
+ )
30
+ );
31
+ }
32
+
33
+ function createDataFiles(
34
+ directory: string,
35
+ collections: Array<DataFileCollection>
36
+ ) {
37
+ return Promise.all(
38
+ collections.map((collection) => createDataFile(directory, collection))
39
+ );
40
+ }
41
+
42
+ type JavaScriptFileConfiguration = {
43
+ collections: Array<Pick<TransformedCollection, "name">>;
44
+ };
45
+
46
+ async function createJavaScriptFile(
47
+ directory: string,
48
+ configuration: JavaScriptFileConfiguration
49
+ ) {
50
+ const collections = configuration.collections.map(({ name }) =>
51
+ createArrayConstName(name)
52
+ );
53
+
54
+ let content = `// generated by content-collections at ${new Date()}\n\n`;
55
+ for (const name of collections) {
56
+ content += `import ${name} from "./${name}.json";\n`;
57
+ }
58
+ content += "\n";
59
+ content += "export { " + collections.join(", ") + " };\n";
60
+
61
+ await fs.writeFile(path.join(directory, "index.js"), content, "utf-8");
62
+ }
63
+
64
+ type TypeDefinitionFileConfiguration = Pick<
65
+ InternalConfiguration,
66
+ "path" | "generateTypes"
67
+ > & {
68
+ collections: Array<Pick<TransformedCollection, "name" | "typeName">>;
69
+ };
70
+
71
+ async function createTypeDefinitionFile(
72
+ directory: string,
73
+ configuration: TypeDefinitionFileConfiguration
74
+ ) {
75
+ if (!configuration.generateTypes) {
76
+ return;
77
+ }
78
+
79
+ const importPath = path.relative(directory, configuration.path);
80
+ let content = `import configuration from "${importPath}";
81
+ import { GetTypeByName } from "@content-collections/core";
82
+ `;
83
+
84
+ const collections = configuration.collections;
85
+ for (const collection of collections) {
86
+ content += `\n`;
87
+ content += `export type ${collection.typeName} = GetTypeByName<typeof configuration, "${collection.name}">;\n`;
88
+ content += `export declare const ${createArrayConstName(
89
+ collection.name
90
+ )}: Array<${collection.typeName}>;\n`;
91
+ }
92
+
93
+ content += "\n";
94
+ // https://github.com/microsoft/TypeScript/issues/38592
95
+ content += "export {};\n";
96
+
97
+ await fs.writeFile(path.join(directory, "index.d.ts"), content, "utf-8");
98
+ }
99
+
100
+ export async function createWriter(directory: string) {
101
+ await fs.mkdir(directory, { recursive: true });
102
+ return {
103
+ createJavaScriptFile: (configuration: JavaScriptFileConfiguration) =>
104
+ createJavaScriptFile(directory, configuration),
105
+ createTypeDefinitionFile: (
106
+ configuration: TypeDefinitionFileConfiguration
107
+ ) => createTypeDefinitionFile(directory, configuration),
108
+ createDataFiles: (collections: Array<DataFileCollection>) =>
109
+ createDataFiles(directory, collections),
110
+ };
111
+ }
112
+
113
+ export type Writer = Awaited<ReturnType<typeof createWriter>>;
package/tsconfig.json ADDED
@@ -0,0 +1,27 @@
1
+ {
2
+ "compilerOptions": {
3
+ /* Base Options: */
4
+ "esModuleInterop": true,
5
+ "skipLibCheck": true,
6
+ "target": "es2022",
7
+ "allowJs": true,
8
+ "resolveJsonModule": true,
9
+ "moduleDetection": "force",
10
+ "isolatedModules": true,
11
+ /* Strictness */
12
+ "strict": true,
13
+ "noUncheckedIndexedAccess": true,
14
+ /* If NOT transpiling with TypeScript: */
15
+ "moduleResolution": "Bundler",
16
+ "module": "ESNext",
17
+ "noEmit": true,
18
+ /* If your code doesn't run in the DOM: */
19
+ "lib": ["es2022"],
20
+
21
+ "baseUrl": ".",
22
+ "paths": {
23
+ "@content-collections/core": ["./src"]
24
+ },
25
+ },
26
+ "include": ["src/**/*"]
27
+ }
package/vite.config.ts ADDED
@@ -0,0 +1,24 @@
1
+ import { defineConfig } from "vitest/config";
2
+
3
+ const excludes = [
4
+ "**/node_modules/**",
5
+ "**/tmp/**",
6
+ "**/.content-collections/**",
7
+ "**/__tests__/**",
8
+ "**/types.ts",
9
+ ];
10
+
11
+ if (process.env.ENABLE_WATCHER_TESTS !== "true") {
12
+ excludes.push("**/watcher.ts");
13
+ }
14
+
15
+ export default defineConfig({
16
+ test: {
17
+ coverage: {
18
+ exclude: excludes
19
+ },
20
+ poolMatchGlobs: [
21
+ ["**/watcher.test.ts", "forks"],
22
+ ],
23
+ },
24
+ });