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.cjs CHANGED
@@ -3,15 +3,10 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  const tsMorph = require('ts-morph');
6
- const commonTags = require('common-tags');
7
6
  const types = require('@prismicio/types');
8
7
  const pascalCase$1 = require('pascal-case');
9
8
 
10
9
  const BLANK_LINE_IDENTIFIER = "// ___BLANK_LINE_TO_BE_REPLACED___";
11
- const NON_EDITABLE_FILE_HEADER = commonTags.stripIndent`
12
- // Code generated by prismic-ts-codegen. DO NOT EDIT.
13
- ${BLANK_LINE_IDENTIFIER}
14
- `;
15
10
  const CUSTOM_TYPES_DOCUMENTATION_URL = "https://prismic.io/docs/core-concepts/custom-types";
16
11
  const SHARED_SLICES_DOCUMENTATION_URL = "https://prismic.io/docs/core-concepts/reusing-slices";
17
12
  const FIELD_DOCUMENTATION_URLS = {
@@ -202,10 +197,18 @@ const pascalCase = (input) => pascalCase$1.pascalCase(input, {
202
197
  transform: pascalCase$1.pascalCaseTransformMerge
203
198
  });
204
199
 
205
- const buildSharedSliceInterfaceName = (config) => {
200
+ const buildSharedSliceInterfaceNamePart = (config) => {
206
201
  return pascalCase(`${config.id} Slice`);
207
202
  };
208
203
 
204
+ const buildTypeName = (...parts) => {
205
+ let name = pascalCase(parts.filter(Boolean).join(" "));
206
+ if (/^[0-9]/.test(name) ? "_" : "") {
207
+ name = `_${name}`;
208
+ }
209
+ return name;
210
+ };
211
+
209
212
  const addInterfacePropertyForField = (config) => {
210
213
  switch (config.model.type) {
211
214
  case "UID": {
@@ -255,12 +258,16 @@ const addInterfacePropertyForField = (config) => {
255
258
  if (config.fieldConfigs.embed?.providerTypes) {
256
259
  for (const providerType in config.fieldConfigs.embed?.providerTypes) {
257
260
  const configuredProviderType = config.fieldConfigs.embed?.providerTypes[providerType];
258
- providerTypes.push(`({ provider_name: "${providerType}" } & ${configuredProviderType})`);
261
+ providerTypes.push(
262
+ `({ provider_name: "${providerType}" } & ${configuredProviderType})`
263
+ );
259
264
  }
260
265
  }
261
266
  config.interface.addProperty({
262
267
  name: config.id,
263
- type: providerTypes.length > 0 ? `prismicT.EmbedField<prismicT.AnyOEmbed & prismicT.OEmbedExtra & (${providerTypes.join(" | ")})>` : "prismicT.EmbedField",
268
+ type: providerTypes.length > 0 ? `prismicT.EmbedField<prismicT.AnyOEmbed & prismicT.OEmbedExtra & (${providerTypes.join(
269
+ " | "
270
+ )})>` : "prismicT.EmbedField",
264
271
  docs: buildFieldDocs({
265
272
  id: config.id,
266
273
  model: config.model,
@@ -464,7 +471,13 @@ const addInterfacePropertyForField = (config) => {
464
471
  }
465
472
  case "Group": {
466
473
  const itemInterface = config.sourceFile.addInterface({
467
- name: pascalCase(`${config.path[0].id} Document Data ${config.id} Item`),
474
+ name: buildTypeName(
475
+ config.path[0].id,
476
+ "Document",
477
+ "Data",
478
+ config.id,
479
+ "Item"
480
+ ),
468
481
  docs: [
469
482
  {
470
483
  description: (writer) => {
@@ -516,12 +529,22 @@ const addInterfacePropertyForField = (config) => {
516
529
  for (const choiceId in config.model.config.choices) {
517
530
  const choice = config.model.config.choices[choiceId];
518
531
  if (choice.type === types.CustomTypeModelSliceType.SharedSlice) {
519
- choiceInterfaceNames.push(buildSharedSliceInterfaceName({ id: choiceId }));
532
+ choiceInterfaceNames.push(
533
+ buildSharedSliceInterfaceNamePart({ id: choiceId })
534
+ );
520
535
  } else if (choice.type === types.CustomTypeModelSliceType.Slice) {
521
536
  let primaryInterface;
522
537
  if (choice["non-repeat"] && Object.keys(choice["non-repeat"]).length > 0) {
523
538
  primaryInterface = config.sourceFile.addInterface({
524
- name: pascalCase(`${config.path[0].id} Document Data ${config.id} ${choiceId} Slice Primary`),
539
+ name: buildTypeName(
540
+ config.path[0].id,
541
+ "Document",
542
+ "Data",
543
+ config.id,
544
+ choiceId,
545
+ "Slice",
546
+ "Primary"
547
+ ),
525
548
  docs: [
526
549
  {
527
550
  description: (writer) => {
@@ -542,7 +565,9 @@ const addInterfacePropertyForField = (config) => {
542
565
  }
543
566
  ]
544
567
  });
545
- writer.writeLine(`Primary content in ${humanReadablePath}`);
568
+ writer.writeLine(
569
+ `Primary content in ${humanReadablePath}`
570
+ );
546
571
  }
547
572
  }
548
573
  ]
@@ -572,7 +597,15 @@ const addInterfacePropertyForField = (config) => {
572
597
  let itemInterface;
573
598
  if (choice.repeat && Object.keys(choice.repeat).length > 0) {
574
599
  itemInterface = config.sourceFile.addInterface({
575
- name: pascalCase(`${config.path[0].id} Document Data ${config.id} ${choiceId} Slice Item`),
600
+ name: buildTypeName(
601
+ config.path[0].id,
602
+ "Document",
603
+ "Data",
604
+ config.id,
605
+ choiceId,
606
+ "Slice",
607
+ "Item"
608
+ ),
576
609
  docs: [
577
610
  {
578
611
  description: (writer) => {
@@ -622,7 +655,14 @@ const addInterfacePropertyForField = (config) => {
622
655
  });
623
656
  }
624
657
  const sliceType = config.sourceFile.addTypeAlias({
625
- name: pascalCase(`${config.path[0].id} Document Data ${config.id} ${choiceId} Slice`),
658
+ name: buildTypeName(
659
+ config.path[0].id,
660
+ "Document",
661
+ "Data",
662
+ config.id,
663
+ choiceId,
664
+ "Slice"
665
+ ),
626
666
  type: `prismicT.Slice<"${choiceId}", ${primaryInterface ? `Simplify<${primaryInterface.getName()}>` : "Record<string, never>"}, ${itemInterface ? `Simplify<${itemInterface.getName()}>` : "never"}>`,
627
667
  isExported: true
628
668
  });
@@ -631,7 +671,13 @@ const addInterfacePropertyForField = (config) => {
631
671
  }
632
672
  }
633
673
  const slicesType = config.sourceFile.addTypeAlias({
634
- name: pascalCase(`${config.path[0].id} Document Data ${config.id} Slice`),
674
+ name: buildTypeName(
675
+ config.path[0].id,
676
+ "Document",
677
+ "Data",
678
+ config.id,
679
+ "Slice"
680
+ ),
635
681
  type: choiceInterfaceNames.length > 0 ? choiceInterfaceNames.join(" | ") : "never",
636
682
  docs: [
637
683
  {
@@ -702,7 +748,7 @@ const addTypeAliasForCustomType = ({
702
748
  let dataInterface;
703
749
  if (hasDataFields) {
704
750
  dataInterface = sourceFile.addInterface({
705
- name: pascalCase(`${model.id} Document Data`),
751
+ name: buildTypeName(model.id, "Document", "Data"),
706
752
  docs: [
707
753
  {
708
754
  description: `Content for ${model.label} documents`
@@ -727,7 +773,7 @@ const addTypeAliasForCustomType = ({
727
773
  }
728
774
  } else {
729
775
  dataInterface = sourceFile.addTypeAlias({
730
- name: pascalCase(`${model.id} Document Data`),
776
+ name: buildTypeName(model.id, "Document", "Data"),
731
777
  type: `Record<string, never>`,
732
778
  docs: [
733
779
  {
@@ -737,7 +783,7 @@ const addTypeAliasForCustomType = ({
737
783
  });
738
784
  }
739
785
  return sourceFile.addTypeAlias({
740
- name: pascalCase(`${model.id} Document`),
786
+ name: buildTypeName(model.id, "Document"),
741
787
  typeParameters: [
742
788
  {
743
789
  name: "Lang",
@@ -753,7 +799,9 @@ const addTypeAliasForCustomType = ({
753
799
  writer.blankLine();
754
800
  writer.writeLine(`- **API ID**: \`${model.id}\``);
755
801
  writer.writeLine(`- **Repeatable**: \`${model.repeatable}\``);
756
- writer.writeLine(`- **Documentation**: ${CUSTOM_TYPES_DOCUMENTATION_URL}`);
802
+ writer.writeLine(
803
+ `- **Documentation**: ${CUSTOM_TYPES_DOCUMENTATION_URL}`
804
+ );
757
805
  },
758
806
  tags: [
759
807
  {
@@ -773,7 +821,11 @@ const addTypeAliasForSharedSlice = (config) => {
773
821
  let primaryInterface;
774
822
  if (variation.primary && Object.keys(variation.primary).length > 0) {
775
823
  primaryInterface = config.sourceFile.addInterface({
776
- name: pascalCase(`${buildSharedSliceInterfaceName({ id: config.model.id })} ${variation.id} Primary`),
824
+ name: buildTypeName(
825
+ buildSharedSliceInterfaceNamePart({ id: config.model.id }),
826
+ variation.id,
827
+ "Primary"
828
+ ),
777
829
  docs: [
778
830
  {
779
831
  description: (writer) => {
@@ -814,7 +866,11 @@ const addTypeAliasForSharedSlice = (config) => {
814
866
  let itemInterface;
815
867
  if (variation.items && Object.keys(variation.items).length > 0) {
816
868
  itemInterface = config.sourceFile.addInterface({
817
- name: pascalCase(`${buildSharedSliceInterfaceName({ id: config.model.id })} ${variation.id} Item`),
869
+ name: buildTypeName(
870
+ buildSharedSliceInterfaceNamePart({ id: config.model.id }),
871
+ variation.id,
872
+ "Item"
873
+ ),
818
874
  docs: [
819
875
  {
820
876
  description: (writer) => {
@@ -854,18 +910,23 @@ const addTypeAliasForSharedSlice = (config) => {
854
910
  });
855
911
  }
856
912
  const variationType = config.sourceFile.addTypeAlias({
857
- name: pascalCase(`${buildSharedSliceInterfaceName({
858
- id: config.model.id
859
- })} ${variation.id}`),
913
+ name: buildTypeName(
914
+ buildSharedSliceInterfaceNamePart({ id: config.model.id }),
915
+ variation.id
916
+ ),
860
917
  type: `prismicT.SharedSliceVariation<"${variation.id}", ${primaryInterface ? `Simplify<${primaryInterface.getName()}>` : "Record<string, never>"}, ${itemInterface ? `Simplify<${itemInterface.getName()}>` : "never"}>`,
861
918
  docs: [
862
919
  {
863
920
  description: (writer) => {
864
- writer.writeLine(`${variation.name} variation for ${config.model.name} Slice`);
921
+ writer.writeLine(
922
+ `${variation.name} variation for ${config.model.name} Slice`
923
+ );
865
924
  writer.blankLine();
866
925
  writer.writeLine(`- **API ID**: \`${variation.id}\``);
867
926
  writer.writeLine(`- **Description**: \`${variation.description}\``);
868
- writer.writeLine(`- **Documentation**: ${SHARED_SLICES_DOCUMENTATION_URL}`);
927
+ writer.writeLine(
928
+ `- **Documentation**: ${SHARED_SLICES_DOCUMENTATION_URL}`
929
+ );
869
930
  }
870
931
  }
871
932
  ],
@@ -874,7 +935,7 @@ const addTypeAliasForSharedSlice = (config) => {
874
935
  variationTypeNames.push(variationType.getName());
875
936
  }
876
937
  const variationsType = config.sourceFile.addTypeAlias({
877
- name: pascalCase(`${config.model.id} Slice Variation`),
938
+ name: buildTypeName(config.model.id, "Slice", "Variation"),
878
939
  type: variationTypeNames.length > 0 ? variationTypeNames.join(" | ") : "never",
879
940
  docs: [
880
941
  {
@@ -893,9 +954,9 @@ const addTypeAliasForSharedSlice = (config) => {
893
954
  ]
894
955
  });
895
956
  return config.sourceFile.addTypeAlias({
896
- name: pascalCase(buildSharedSliceInterfaceName({
897
- id: config.model.id
898
- })),
957
+ name: buildTypeName(
958
+ buildSharedSliceInterfaceNamePart({ id: config.model.id })
959
+ ),
899
960
  type: `prismicT.SharedSlice<"${config.model.id}", ${variationsType.getName()}>`,
900
961
  docs: [
901
962
  {
@@ -903,8 +964,12 @@ const addTypeAliasForSharedSlice = (config) => {
903
964
  writer.writeLine(`${config.model.name} Shared Slice`);
904
965
  writer.blankLine();
905
966
  writer.writeLine(`- **API ID**: \`${config.model.id}\``);
906
- writer.writeLine(`- **Description**: \`${config.model.description}\``);
907
- writer.writeLine(`- **Documentation**: ${SHARED_SLICES_DOCUMENTATION_URL}`);
967
+ writer.writeLine(
968
+ `- **Description**: \`${config.model.description}\``
969
+ );
970
+ writer.writeLine(
971
+ `- **Documentation**: ${SHARED_SLICES_DOCUMENTATION_URL}`
972
+ );
908
973
  }
909
974
  }
910
975
  ],
@@ -925,8 +990,7 @@ const generateTypes = (config = {}) => {
925
990
  sourceFile.addImportDeclaration({
926
991
  moduleSpecifier: "@prismicio/types",
927
992
  namespaceImport: "prismicT",
928
- isTypeOnly: true,
929
- leadingTrivia: NON_EDITABLE_FILE_HEADER
993
+ isTypeOnly: true
930
994
  });
931
995
  sourceFile.addStatements(BLANK_LINE_IDENTIFIER);
932
996
  const simplifyTypeAlias = sourceFile.addTypeAlias({
@@ -950,7 +1014,9 @@ const generateTypes = (config = {}) => {
950
1014
  if (config.customTypeModels.length > 0) {
951
1015
  sourceFile.addTypeAlias({
952
1016
  name: "AllDocumentTypes",
953
- type: config.customTypeModels.map((customTypeModel) => pascalCase(`${customTypeModel.id} Document`)).join(" | "),
1017
+ type: config.customTypeModels.map(
1018
+ (customTypeModel) => buildTypeName(customTypeModel.id, "Document")
1019
+ ).join(" | "),
954
1020
  isExported: true
955
1021
  });
956
1022
  }