prismic-ts-codegen 0.1.3-alpha.0 → 0.1.5

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.js CHANGED
@@ -1,13 +1,8 @@
1
1
  import { Project, ModuleDeclarationKind } from 'ts-morph';
2
- import { stripIndent } from 'common-tags';
3
2
  import { CustomTypeModelSliceType, CustomTypeModelFieldType, CustomTypeModelLinkSelectType } from '@prismicio/types';
4
3
  import { pascalCase as pascalCase$1, pascalCaseTransformMerge } from 'pascal-case';
5
4
 
6
5
  const BLANK_LINE_IDENTIFIER = "// ___BLANK_LINE_TO_BE_REPLACED___";
7
- const NON_EDITABLE_FILE_HEADER = stripIndent`
8
- // Code generated by prismic-ts-codegen. DO NOT EDIT.
9
- ${BLANK_LINE_IDENTIFIER}
10
- `;
11
6
  const CUSTOM_TYPES_DOCUMENTATION_URL = "https://prismic.io/docs/core-concepts/custom-types";
12
7
  const SHARED_SLICES_DOCUMENTATION_URL = "https://prismic.io/docs/core-concepts/reusing-slices";
13
8
  const FIELD_DOCUMENTATION_URLS = {
@@ -198,10 +193,18 @@ const pascalCase = (input) => pascalCase$1(input, {
198
193
  transform: pascalCaseTransformMerge
199
194
  });
200
195
 
201
- const buildSharedSliceInterfaceName = (config) => {
196
+ const buildSharedSliceInterfaceNamePart = (config) => {
202
197
  return pascalCase(`${config.id} Slice`);
203
198
  };
204
199
 
200
+ const buildTypeName = (...parts) => {
201
+ let name = pascalCase(parts.filter(Boolean).join(" "));
202
+ if (/^[0-9]/.test(name) ? "_" : "") {
203
+ name = `_${name}`;
204
+ }
205
+ return name;
206
+ };
207
+
205
208
  const addInterfacePropertyForField = (config) => {
206
209
  switch (config.model.type) {
207
210
  case "UID": {
@@ -251,12 +254,16 @@ const addInterfacePropertyForField = (config) => {
251
254
  if (config.fieldConfigs.embed?.providerTypes) {
252
255
  for (const providerType in config.fieldConfigs.embed?.providerTypes) {
253
256
  const configuredProviderType = config.fieldConfigs.embed?.providerTypes[providerType];
254
- providerTypes.push(`({ provider_name: "${providerType}" } & ${configuredProviderType})`);
257
+ providerTypes.push(
258
+ `({ provider_name: "${providerType}" } & ${configuredProviderType})`
259
+ );
255
260
  }
256
261
  }
257
262
  config.interface.addProperty({
258
263
  name: config.id,
259
- type: providerTypes.length > 0 ? `prismicT.EmbedField<prismicT.AnyOEmbed & prismicT.OEmbedExtra & (${providerTypes.join(" | ")})>` : "prismicT.EmbedField",
264
+ type: providerTypes.length > 0 ? `prismicT.EmbedField<prismicT.AnyOEmbed & prismicT.OEmbedExtra & (${providerTypes.join(
265
+ " | "
266
+ )})>` : "prismicT.EmbedField",
260
267
  docs: buildFieldDocs({
261
268
  id: config.id,
262
269
  model: config.model,
@@ -460,7 +467,13 @@ const addInterfacePropertyForField = (config) => {
460
467
  }
461
468
  case "Group": {
462
469
  const itemInterface = config.sourceFile.addInterface({
463
- name: pascalCase(`${config.path[0].id} Document Data ${config.id} Item`),
470
+ name: buildTypeName(
471
+ config.path[0].id,
472
+ "Document",
473
+ "Data",
474
+ config.id,
475
+ "Item"
476
+ ),
464
477
  docs: [
465
478
  {
466
479
  description: (writer) => {
@@ -512,12 +525,22 @@ const addInterfacePropertyForField = (config) => {
512
525
  for (const choiceId in config.model.config.choices) {
513
526
  const choice = config.model.config.choices[choiceId];
514
527
  if (choice.type === CustomTypeModelSliceType.SharedSlice) {
515
- choiceInterfaceNames.push(buildSharedSliceInterfaceName({ id: choiceId }));
528
+ choiceInterfaceNames.push(
529
+ buildSharedSliceInterfaceNamePart({ id: choiceId })
530
+ );
516
531
  } else if (choice.type === CustomTypeModelSliceType.Slice) {
517
532
  let primaryInterface;
518
533
  if (choice["non-repeat"] && Object.keys(choice["non-repeat"]).length > 0) {
519
534
  primaryInterface = config.sourceFile.addInterface({
520
- name: pascalCase(`${config.path[0].id} Document Data ${config.id} ${choiceId} Slice Primary`),
535
+ name: buildTypeName(
536
+ config.path[0].id,
537
+ "Document",
538
+ "Data",
539
+ config.id,
540
+ choiceId,
541
+ "Slice",
542
+ "Primary"
543
+ ),
521
544
  docs: [
522
545
  {
523
546
  description: (writer) => {
@@ -538,7 +561,9 @@ const addInterfacePropertyForField = (config) => {
538
561
  }
539
562
  ]
540
563
  });
541
- writer.writeLine(`Primary content in ${humanReadablePath}`);
564
+ writer.writeLine(
565
+ `Primary content in ${humanReadablePath}`
566
+ );
542
567
  }
543
568
  }
544
569
  ]
@@ -568,7 +593,15 @@ const addInterfacePropertyForField = (config) => {
568
593
  let itemInterface;
569
594
  if (choice.repeat && Object.keys(choice.repeat).length > 0) {
570
595
  itemInterface = config.sourceFile.addInterface({
571
- name: pascalCase(`${config.path[0].id} Document Data ${config.id} ${choiceId} Slice Item`),
596
+ name: buildTypeName(
597
+ config.path[0].id,
598
+ "Document",
599
+ "Data",
600
+ config.id,
601
+ choiceId,
602
+ "Slice",
603
+ "Item"
604
+ ),
572
605
  docs: [
573
606
  {
574
607
  description: (writer) => {
@@ -618,7 +651,14 @@ const addInterfacePropertyForField = (config) => {
618
651
  });
619
652
  }
620
653
  const sliceType = config.sourceFile.addTypeAlias({
621
- name: pascalCase(`${config.path[0].id} Document Data ${config.id} ${choiceId} Slice`),
654
+ name: buildTypeName(
655
+ config.path[0].id,
656
+ "Document",
657
+ "Data",
658
+ config.id,
659
+ choiceId,
660
+ "Slice"
661
+ ),
622
662
  type: `prismicT.Slice<"${choiceId}", ${primaryInterface ? `Simplify<${primaryInterface.getName()}>` : "Record<string, never>"}, ${itemInterface ? `Simplify<${itemInterface.getName()}>` : "never"}>`,
623
663
  isExported: true
624
664
  });
@@ -627,7 +667,13 @@ const addInterfacePropertyForField = (config) => {
627
667
  }
628
668
  }
629
669
  const slicesType = config.sourceFile.addTypeAlias({
630
- name: pascalCase(`${config.path[0].id} Document Data ${config.id} Slice`),
670
+ name: buildTypeName(
671
+ config.path[0].id,
672
+ "Document",
673
+ "Data",
674
+ config.id,
675
+ "Slice"
676
+ ),
631
677
  type: choiceInterfaceNames.length > 0 ? choiceInterfaceNames.join(" | ") : "never",
632
678
  docs: [
633
679
  {
@@ -698,7 +744,7 @@ const addTypeAliasForCustomType = ({
698
744
  let dataInterface;
699
745
  if (hasDataFields) {
700
746
  dataInterface = sourceFile.addInterface({
701
- name: pascalCase(`${model.id} Document Data`),
747
+ name: buildTypeName(model.id, "Document", "Data"),
702
748
  docs: [
703
749
  {
704
750
  description: `Content for ${model.label} documents`
@@ -723,7 +769,7 @@ const addTypeAliasForCustomType = ({
723
769
  }
724
770
  } else {
725
771
  dataInterface = sourceFile.addTypeAlias({
726
- name: pascalCase(`${model.id} Document Data`),
772
+ name: buildTypeName(model.id, "Document", "Data"),
727
773
  type: `Record<string, never>`,
728
774
  docs: [
729
775
  {
@@ -733,7 +779,7 @@ const addTypeAliasForCustomType = ({
733
779
  });
734
780
  }
735
781
  return sourceFile.addTypeAlias({
736
- name: pascalCase(`${model.id} Document`),
782
+ name: buildTypeName(model.id, "Document"),
737
783
  typeParameters: [
738
784
  {
739
785
  name: "Lang",
@@ -749,7 +795,9 @@ const addTypeAliasForCustomType = ({
749
795
  writer.blankLine();
750
796
  writer.writeLine(`- **API ID**: \`${model.id}\``);
751
797
  writer.writeLine(`- **Repeatable**: \`${model.repeatable}\``);
752
- writer.writeLine(`- **Documentation**: ${CUSTOM_TYPES_DOCUMENTATION_URL}`);
798
+ writer.writeLine(
799
+ `- **Documentation**: ${CUSTOM_TYPES_DOCUMENTATION_URL}`
800
+ );
753
801
  },
754
802
  tags: [
755
803
  {
@@ -769,7 +817,11 @@ const addTypeAliasForSharedSlice = (config) => {
769
817
  let primaryInterface;
770
818
  if (variation.primary && Object.keys(variation.primary).length > 0) {
771
819
  primaryInterface = config.sourceFile.addInterface({
772
- name: pascalCase(`${buildSharedSliceInterfaceName({ id: config.model.id })} ${variation.id} Primary`),
820
+ name: buildTypeName(
821
+ buildSharedSliceInterfaceNamePart({ id: config.model.id }),
822
+ variation.id,
823
+ "Primary"
824
+ ),
773
825
  docs: [
774
826
  {
775
827
  description: (writer) => {
@@ -810,7 +862,11 @@ const addTypeAliasForSharedSlice = (config) => {
810
862
  let itemInterface;
811
863
  if (variation.items && Object.keys(variation.items).length > 0) {
812
864
  itemInterface = config.sourceFile.addInterface({
813
- name: pascalCase(`${buildSharedSliceInterfaceName({ id: config.model.id })} ${variation.id} Item`),
865
+ name: buildTypeName(
866
+ buildSharedSliceInterfaceNamePart({ id: config.model.id }),
867
+ variation.id,
868
+ "Item"
869
+ ),
814
870
  docs: [
815
871
  {
816
872
  description: (writer) => {
@@ -850,18 +906,23 @@ const addTypeAliasForSharedSlice = (config) => {
850
906
  });
851
907
  }
852
908
  const variationType = config.sourceFile.addTypeAlias({
853
- name: pascalCase(`${buildSharedSliceInterfaceName({
854
- id: config.model.id
855
- })} ${variation.id}`),
909
+ name: buildTypeName(
910
+ buildSharedSliceInterfaceNamePart({ id: config.model.id }),
911
+ variation.id
912
+ ),
856
913
  type: `prismicT.SharedSliceVariation<"${variation.id}", ${primaryInterface ? `Simplify<${primaryInterface.getName()}>` : "Record<string, never>"}, ${itemInterface ? `Simplify<${itemInterface.getName()}>` : "never"}>`,
857
914
  docs: [
858
915
  {
859
916
  description: (writer) => {
860
- writer.writeLine(`${variation.name} variation for ${config.model.name} Slice`);
917
+ writer.writeLine(
918
+ `${variation.name} variation for ${config.model.name} Slice`
919
+ );
861
920
  writer.blankLine();
862
921
  writer.writeLine(`- **API ID**: \`${variation.id}\``);
863
922
  writer.writeLine(`- **Description**: \`${variation.description}\``);
864
- writer.writeLine(`- **Documentation**: ${SHARED_SLICES_DOCUMENTATION_URL}`);
923
+ writer.writeLine(
924
+ `- **Documentation**: ${SHARED_SLICES_DOCUMENTATION_URL}`
925
+ );
865
926
  }
866
927
  }
867
928
  ],
@@ -870,7 +931,7 @@ const addTypeAliasForSharedSlice = (config) => {
870
931
  variationTypeNames.push(variationType.getName());
871
932
  }
872
933
  const variationsType = config.sourceFile.addTypeAlias({
873
- name: pascalCase(`${config.model.id} Slice Variation`),
934
+ name: buildTypeName(config.model.id, "Slice", "Variation"),
874
935
  type: variationTypeNames.length > 0 ? variationTypeNames.join(" | ") : "never",
875
936
  docs: [
876
937
  {
@@ -889,9 +950,9 @@ const addTypeAliasForSharedSlice = (config) => {
889
950
  ]
890
951
  });
891
952
  return config.sourceFile.addTypeAlias({
892
- name: pascalCase(buildSharedSliceInterfaceName({
893
- id: config.model.id
894
- })),
953
+ name: buildTypeName(
954
+ buildSharedSliceInterfaceNamePart({ id: config.model.id })
955
+ ),
895
956
  type: `prismicT.SharedSlice<"${config.model.id}", ${variationsType.getName()}>`,
896
957
  docs: [
897
958
  {
@@ -899,8 +960,12 @@ const addTypeAliasForSharedSlice = (config) => {
899
960
  writer.writeLine(`${config.model.name} Shared Slice`);
900
961
  writer.blankLine();
901
962
  writer.writeLine(`- **API ID**: \`${config.model.id}\``);
902
- writer.writeLine(`- **Description**: \`${config.model.description}\``);
903
- writer.writeLine(`- **Documentation**: ${SHARED_SLICES_DOCUMENTATION_URL}`);
963
+ writer.writeLine(
964
+ `- **Description**: \`${config.model.description}\``
965
+ );
966
+ writer.writeLine(
967
+ `- **Documentation**: ${SHARED_SLICES_DOCUMENTATION_URL}`
968
+ );
904
969
  }
905
970
  }
906
971
  ],
@@ -921,8 +986,7 @@ const generateTypes = (config = {}) => {
921
986
  sourceFile.addImportDeclaration({
922
987
  moduleSpecifier: "@prismicio/types",
923
988
  namespaceImport: "prismicT",
924
- isTypeOnly: true,
925
- leadingTrivia: NON_EDITABLE_FILE_HEADER
989
+ isTypeOnly: true
926
990
  });
927
991
  sourceFile.addStatements(BLANK_LINE_IDENTIFIER);
928
992
  const simplifyTypeAlias = sourceFile.addTypeAlias({
@@ -946,7 +1010,9 @@ const generateTypes = (config = {}) => {
946
1010
  if (config.customTypeModels.length > 0) {
947
1011
  sourceFile.addTypeAlias({
948
1012
  name: "AllDocumentTypes",
949
- type: config.customTypeModels.map((customTypeModel) => pascalCase(`${customTypeModel.id} Document`)).join(" | "),
1013
+ type: config.customTypeModels.map(
1014
+ (customTypeModel) => buildTypeName(customTypeModel.id, "Document")
1015
+ ).join(" | "),
950
1016
  isExported: true
951
1017
  });
952
1018
  }