@content-collections/core 0.14.2 → 0.15.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 CHANGED
@@ -730,6 +730,89 @@ The following type is not valid:`;
730
730
  declare const serializableSchema: ZodMiniRecord<ZodMiniString<string>, ZodMiniType<SchemaType, unknown, $ZodTypeInternals<SchemaType, unknown>>>;
731
731
  type Serializable = output<typeof serializableSchema>;
732
732
  //#endregion
733
+ //#region src/configurationReader.d.ts
734
+ type ErrorType$2 = "Read" | "Compile";
735
+ declare class ConfigurationError extends Error {
736
+ type: ErrorType$2;
737
+ constructor(type: ErrorType$2, message: string);
738
+ }
739
+ type InternalConfiguration = {
740
+ collections: Array<AnyContent>;
741
+ path: string;
742
+ inputPaths: Array<string>;
743
+ checksum: string;
744
+ hooks: Hooks;
745
+ generateTypes?: boolean;
746
+ };
747
+ type Options = {
748
+ configName: string;
749
+ cacheDir?: string;
750
+ };
751
+ //#endregion
752
+ //#region src/types.d.ts
753
+ type Modification = "create" | "update" | "delete";
754
+ type Document = {
755
+ _meta: Meta;
756
+ };
757
+ type CollectionFile = {
758
+ data: {
759
+ content?: string;
760
+ [key: string]: unknown;
761
+ };
762
+ path: string;
763
+ };
764
+ type ConfigurationSources<TConfiguration extends AnyConfiguration> = TConfiguration extends {
765
+ content: infer TSources extends Array<AnyContent>;
766
+ } ? TSources : TConfiguration extends {
767
+ collections: infer TSources extends Array<AnyContent>;
768
+ } ? TSources : never;
769
+ type CollectionByName<TConfiguration extends AnyConfiguration> = { [TCollection in ConfigurationSources<TConfiguration>[number] as TCollection["name"]]: TCollection };
770
+ type GetDocument<TSource extends AnyContent> = TSource extends Collection<any, any, any, any, any, infer TDocument> ? TDocument : TSource extends Singleton<any, any, any, any, any, infer TDocument> ? TDocument : never;
771
+ type GetTypeByName<TConfiguration extends AnyConfiguration, TName extends keyof CollectionByName<TConfiguration>, TCollection$1 = CollectionByName<TConfiguration>[TName]> = TCollection$1 extends AnyContent ? GetDocument<TCollection$1> : never;
772
+ //#endregion
773
+ //#region src/transformer.d.ts
774
+ type TransformerEvents = {
775
+ "transformer:validation-error": {
776
+ collection: AnyContent;
777
+ file: CollectionFile;
778
+ error: TransformError;
779
+ };
780
+ "transformer:singleton-warning": {
781
+ collection: AnyContent;
782
+ };
783
+ "transformer:result-error": {
784
+ collection: AnyContent;
785
+ document: any;
786
+ error: TransformError;
787
+ };
788
+ "transformer:error": {
789
+ collection: AnyContent;
790
+ error: TransformError;
791
+ };
792
+ "transformer:document-skipped": {
793
+ collection: AnyContent;
794
+ filePath: string;
795
+ reason?: string;
796
+ };
797
+ };
798
+ type ErrorType$1 = "Validation" | "Configuration" | "Transform" | "Result";
799
+ declare class TransformError extends Error {
800
+ type: ErrorType$1;
801
+ constructor(type: ErrorType$1, message: string);
802
+ }
803
+ //#endregion
804
+ //#region src/writer.d.ts
805
+ type FileType = "data" | "javascript" | "typeDefinition";
806
+ type WriterHookContext = {
807
+ fileType: FileType;
808
+ content: string;
809
+ filePath: string;
810
+ };
811
+ type WriterHookResult = {
812
+ content: string;
813
+ };
814
+ type WriterHook = (ctx: WriterHookContext) => Promise<WriterHookResult> | WriterHookResult;
815
+ //#endregion
733
816
  //#region src/config.d.ts
734
817
  type Meta = {
735
818
  filePath: string;
@@ -841,6 +924,7 @@ type ConfigurationWithContent<TCollections extends Array<AnyContent>> = {
841
924
  */
842
925
  collections?: TCollections;
843
926
  cache?: Cache;
927
+ hooks?: Hooks;
844
928
  };
845
929
  type ConfigurationWithCollections<TCollections extends Array<AnyContent>> = {
846
930
  /**
@@ -849,12 +933,17 @@ type ConfigurationWithCollections<TCollections extends Array<AnyContent>> = {
849
933
  collections: TCollections;
850
934
  content?: TCollections;
851
935
  cache?: Cache;
936
+ hooks?: Hooks;
852
937
  };
853
938
  type Configuration<TCollections extends Array<AnyContent>> = ConfigurationWithContent<TCollections> | ConfigurationWithCollections<TCollections>;
854
939
  type AnyConfiguration = Configuration<Array<AnyContent>>;
940
+ type Hooks = {
941
+ writer?: Array<WriterHook>;
942
+ };
855
943
  declare function defineConfig<TCollections extends Array<AnyContent>>(config: {
856
944
  content: TCollections;
857
945
  cache?: Cache;
946
+ hooks?: Hooks;
858
947
  }): ConfigurationWithContent<TCollections>;
859
948
  declare function defineConfig<TCollections extends Array<AnyContent>>(config: {
860
949
  /**
@@ -862,29 +951,9 @@ declare function defineConfig<TCollections extends Array<AnyContent>>(config: {
862
951
  */
863
952
  collections: TCollections;
864
953
  cache?: Cache;
954
+ hooks?: Hooks;
865
955
  }): ConfigurationWithCollections<TCollections>;
866
956
  //#endregion
867
- //#region src/types.d.ts
868
- type Modification = "create" | "update" | "delete";
869
- type Document = {
870
- _meta: Meta;
871
- };
872
- type CollectionFile = {
873
- data: {
874
- content?: string;
875
- [key: string]: unknown;
876
- };
877
- path: string;
878
- };
879
- type ConfigurationSources<TConfiguration extends AnyConfiguration> = TConfiguration extends {
880
- content: infer TSources extends Array<AnyContent>;
881
- } ? TSources : TConfiguration extends {
882
- collections: infer TSources extends Array<AnyContent>;
883
- } ? TSources : never;
884
- type CollectionByName<TConfiguration extends AnyConfiguration> = { [TCollection in ConfigurationSources<TConfiguration>[number] as TCollection["name"]]: TCollection };
885
- type GetDocument<TSource extends AnyContent> = TSource extends Collection<any, any, any, any, any, infer TDocument> ? TDocument : TSource extends Singleton<any, any, any, any, any, infer TDocument> ? TDocument : never;
886
- type GetTypeByName<TConfiguration extends AnyConfiguration, TName extends keyof CollectionByName<TConfiguration>, TCollection$1 = CollectionByName<TConfiguration>[TName]> = TCollection$1 extends AnyContent ? GetDocument<TCollection$1> : never;
887
- //#endregion
888
957
  //#region src/collector.d.ts
889
958
  type CollectorEvents = {
890
959
  "collector:read-error": {
@@ -896,41 +965,10 @@ type CollectorEvents = {
896
965
  error: CollectError;
897
966
  };
898
967
  };
899
- type ErrorType$2 = "Parse" | "Read";
968
+ type ErrorType = "Parse" | "Read";
900
969
  declare class CollectError extends Error {
901
- type: ErrorType$2;
902
- constructor(type: ErrorType$2, message: string);
903
- }
904
- //#endregion
905
- //#region src/transformer.d.ts
906
- type TransformerEvents = {
907
- "transformer:validation-error": {
908
- collection: AnyContent;
909
- file: CollectionFile;
910
- error: TransformError;
911
- };
912
- "transformer:singleton-warning": {
913
- collection: AnyContent;
914
- };
915
- "transformer:result-error": {
916
- collection: AnyContent;
917
- document: any;
918
- error: TransformError;
919
- };
920
- "transformer:error": {
921
- collection: AnyContent;
922
- error: TransformError;
923
- };
924
- "transformer:document-skipped": {
925
- collection: AnyContent;
926
- filePath: string;
927
- reason?: string;
928
- };
929
- };
930
- type ErrorType$1 = "Validation" | "Configuration" | "Transform" | "Result";
931
- declare class TransformError extends Error {
932
- type: ErrorType$1;
933
- constructor(type: ErrorType$1, message: string);
970
+ type: ErrorType;
971
+ constructor(type: ErrorType, message: string);
934
972
  }
935
973
  //#endregion
936
974
  //#region src/watcher.d.ts
@@ -986,24 +1024,6 @@ declare function createEmitter<TEvents extends EventMap>(): {
986
1024
  };
987
1025
  type Emitter = ReturnType<typeof createEmitter<Events>>;
988
1026
  //#endregion
989
- //#region src/configurationReader.d.ts
990
- type ErrorType = "Read" | "Compile";
991
- declare class ConfigurationError extends Error {
992
- type: ErrorType;
993
- constructor(type: ErrorType, message: string);
994
- }
995
- type InternalConfiguration = {
996
- collections: Array<AnyContent>;
997
- path: string;
998
- inputPaths: Array<string>;
999
- checksum: string;
1000
- generateTypes?: boolean;
1001
- };
1002
- type Options = {
1003
- configName: string;
1004
- cacheDir?: string;
1005
- };
1006
- //#endregion
1007
1027
  //#region src/build.d.ts
1008
1028
  type BuildEvents = {
1009
1029
  "builder:start": {
@@ -1077,4 +1097,4 @@ declare const deprecations: {
1077
1097
  type Deprecation = keyof typeof deprecations;
1078
1098
  declare function suppressDeprecatedWarnings(...suppresses: Array<Deprecation | "all">): void;
1079
1099
  //#endregion
1080
- export { AnyCollection, AnyConfiguration, AnyContent, AnySingleton, Builder, BuilderEvents, CollectError, Collection, CollectionContext, CollectionRequest, Configuration, ConfigurationError, ConfigurationReloadError, ConfigurationWithCollections, ConfigurationWithContent, ContentType, Context, type Document, type GetTypeByName, Meta, type Modification, Schema, Singleton, SingletonContext, SingletonRequest, SkippedSignal, TransformError, type Watcher, createBuilder, createDefaultImport, createInternalBuilder, createNamedImport, defineCollection, defineConfig, defineParser, defineSingleton, isSingleton, skippedSymbol, suppressDeprecatedWarnings };
1100
+ export { AnyCollection, AnyConfiguration, AnyContent, AnySingleton, Builder, BuilderEvents, CollectError, Collection, CollectionContext, CollectionRequest, Configuration, ConfigurationError, ConfigurationReloadError, ConfigurationWithCollections, ConfigurationWithContent, ContentType, Context, type Document, type GetTypeByName, Hooks, Meta, type Modification, Schema, Singleton, SingletonContext, SingletonRequest, SkippedSignal, TransformError, type Watcher, type WriterHook, type WriterHookContext, type WriterHookResult, createBuilder, createDefaultImport, createInternalBuilder, createNamedImport, defineCollection, defineConfig, defineParser, defineSingleton, isSingleton, skippedSymbol, suppressDeprecatedWarnings };
package/dist/index.js CHANGED
@@ -28,7 +28,9 @@ async function createCacheDirectory(cacheDirectory) {
28
28
  if (!existsSync(cacheDirectory)) await mkdir(cacheDirectory, { recursive: true });
29
29
  }
30
30
  function fileName(input) {
31
- return input.replace(/[^a-z0-9]/gi, "_").toLowerCase();
31
+ const readable = input.replace(/[^a-z0-9]/gi, "_").toLowerCase();
32
+ const hash = createHash("sha1").update(input).digest("hex").slice(0, 12);
33
+ return `${readable.slice(0, 24)}_${hash}`.replace(/^_([a-f0-9]{12})$/, "$1");
32
34
  }
33
35
  async function readMapping(mappingPath) {
34
36
  if (existsSync(mappingPath)) try {
@@ -381,7 +383,7 @@ function createConfigurationReader() {
381
383
  const outfile = path.join(cacheDir, options.configName);
382
384
  try {
383
385
  const configurationPaths = await compile(configurationPath, outfile);
384
- const { content, collections,...rest } = (await import(`file://${path.resolve(outfile)}?x=${Date.now()}`)).default;
386
+ const { content, collections, hooks,...rest } = (await import(`file://${path.resolve(outfile)}?x=${Date.now()}`)).default;
385
387
  if (!content && collections) deprecated("collectionsConfigProperty");
386
388
  const hash = createHash("sha256");
387
389
  hash.update(await fs.readFile(outfile, "utf-8"));
@@ -389,6 +391,7 @@ function createConfigurationReader() {
389
391
  return {
390
392
  ...rest,
391
393
  collections: content ?? collections ?? [],
394
+ hooks: hooks ?? {},
392
395
  path: configurationPath,
393
396
  inputPaths: configurationPaths.map((p) => path.resolve(p)),
394
397
  generateTypes: true,
@@ -2146,29 +2149,40 @@ function createTransformer(emitter, cacheManager) {
2146
2149
 
2147
2150
  //#endregion
2148
2151
  //#region src/writer.ts
2149
- async function createDataFile(directory, collection) {
2152
+ async function createDataFile(directory, collection, hooks) {
2150
2153
  const constName = isSingleton(collection) ? generateSingletonConstName(collection.typeName) : generateArrayConstName(collection.name);
2151
2154
  const dataPath = path.join(directory, `${constName}.${extension}`);
2152
- const value = isSingleton(collection) ? collection.documents[0]?.document : collection.documents.map((doc) => doc.document);
2153
- await fs.writeFile(dataPath, serialize(value));
2154
- }
2155
- function createDataFiles(directory, collections) {
2156
- return Promise.all(collections.map((collection) => createDataFile(directory, collection)));
2157
- }
2158
- async function createJavaScriptFile(directory, configuration) {
2155
+ let content = serialize(isSingleton(collection) ? collection.documents[0]?.document : collection.documents.map((doc) => doc.document));
2156
+ for (const hook of hooks) content = (await hook({
2157
+ fileType: "data",
2158
+ content,
2159
+ filePath: dataPath
2160
+ })).content;
2161
+ await fs.writeFile(dataPath, content);
2162
+ }
2163
+ function createDataFiles(directory, collections, hooks) {
2164
+ return Promise.all(collections.map((collection) => createDataFile(directory, collection, hooks)));
2165
+ }
2166
+ async function createJavaScriptFile(directory, configuration, hooks) {
2159
2167
  const exports = configuration.collections.map((collection) => isSingleton(collection) ? generateSingletonConstName(collection.typeName) : generateArrayConstName(collection.name));
2160
2168
  let content = `// generated by content-collections at ${/* @__PURE__ */ new Date()}\n\n`;
2161
2169
  for (const name of exports) content += `import ${name} from "./${name}.${extension}";\n`;
2162
2170
  content += "\n";
2163
2171
  content += "export { " + exports.join(", ") + " };\n";
2164
- await fs.writeFile(path.join(directory, "index.js"), content, "utf-8");
2172
+ const filePath = path.join(directory, "index.js");
2173
+ for (const hook of hooks) content = (await hook({
2174
+ fileType: "javascript",
2175
+ content,
2176
+ filePath
2177
+ })).content;
2178
+ await fs.writeFile(filePath, content, "utf-8");
2165
2179
  }
2166
2180
  function createImportPath(directory, target) {
2167
2181
  let importPath = path.posix.join(...path.relative(directory, target).split(path.sep));
2168
2182
  if (!importPath.startsWith(".")) importPath = "./" + importPath;
2169
2183
  return importPath;
2170
2184
  }
2171
- async function createTypeDefinitionFile(directory, configuration) {
2185
+ async function createTypeDefinitionFile(directory, configuration, hooks) {
2172
2186
  if (!configuration.generateTypes) return;
2173
2187
  let content = `import configuration from "${createImportPath(directory, configuration.path)}";
2174
2188
  import { GetTypeByName } from "@content-collections/core";
@@ -2182,14 +2196,20 @@ import { GetTypeByName } from "@content-collections/core";
2182
2196
  }
2183
2197
  content += "\n";
2184
2198
  content += "export {};\n";
2185
- await fs.writeFile(path.join(directory, "index.d.ts"), content, "utf-8");
2186
- }
2187
- async function createWriter(directory) {
2199
+ const filePath = path.join(directory, "index.d.ts");
2200
+ for (const hook of hooks) content = (await hook({
2201
+ fileType: "typeDefinition",
2202
+ content,
2203
+ filePath
2204
+ })).content;
2205
+ await fs.writeFile(filePath, content, "utf-8");
2206
+ }
2207
+ async function createWriter(directory, hooks) {
2188
2208
  await fs.mkdir(directory, { recursive: true });
2189
2209
  return {
2190
- createJavaScriptFile: (configuration) => createJavaScriptFile(directory, configuration),
2191
- createTypeDefinitionFile: (configuration) => createTypeDefinitionFile(directory, configuration),
2192
- createDataFiles: (collections) => createDataFiles(directory, collections)
2210
+ createJavaScriptFile: (configuration) => createJavaScriptFile(directory, configuration, hooks),
2211
+ createTypeDefinitionFile: (configuration) => createTypeDefinitionFile(directory, configuration, hooks),
2212
+ createDataFiles: (collections) => createDataFiles(directory, collections, hooks)
2193
2213
  };
2194
2214
  }
2195
2215
 
@@ -2198,7 +2218,7 @@ async function createWriter(directory) {
2198
2218
  async function createBuildContext({ emitter, outputDirectory, cacheDirectory, baseDirectory, configuration }) {
2199
2219
  const collector = createCollector(emitter, baseDirectory);
2200
2220
  const [writer, resolved, cacheManager] = await Promise.all([
2201
- createWriter(outputDirectory),
2221
+ createWriter(outputDirectory, configuration.hooks?.writer ?? []),
2202
2222
  collector.collect(configuration.collections),
2203
2223
  createCacheManager(cacheDirectory, configuration.checksum)
2204
2224
  ]);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@content-collections/core",
3
3
  "type": "module",
4
- "version": "0.14.2",
4
+ "version": "0.15.0",
5
5
  "description": "Core of Content Collections",
6
6
  "author": "Sebastian Sdorra <s.sdorra@gmail.com>",
7
7
  "license": "MIT",
@@ -26,7 +26,7 @@
26
26
  "README.md"
27
27
  ],
28
28
  "peerDependencies": {
29
- "typescript": "^5.0.2"
29
+ "typescript": "^5.0.2 || ^6.0.0"
30
30
  },
31
31
  "devDependencies": {
32
32
  "@types/node": "^20.14.9",