prismic-ts-codegen 0.0.3 → 0.0.6
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/README.md +4 -0
- package/bin/cli.mjs +128 -59
- package/dist/index.cjs +104 -36
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +23 -0
- package/dist/index.js +105 -37
- package/dist/index.js.map +1 -1
- package/package.json +4 -1
- package/src/cli/configSchema.ts +16 -29
- package/src/cli/index.ts +19 -1
- package/src/cli/types.ts +21 -0
- package/src/generateTypes.ts +54 -1
- package/src/lib/addInterfacePropertiesForFields.ts +26 -2
- package/src/lib/addTypeAliasForCustomType.ts +20 -12
- package/src/lib/buildFieldDocs.ts +2 -0
- package/src/lib/writeFieldDocsDescription.ts +5 -0
package/README.md
CHANGED
package/bin/cli.mjs
CHANGED
|
@@ -4,7 +4,7 @@ import { readFileSync, existsSync, writeFileSync } from 'fs';
|
|
|
4
4
|
import { resolve } from 'path';
|
|
5
5
|
import meow from 'meow';
|
|
6
6
|
import { stripIndent } from 'common-tags';
|
|
7
|
-
import { Project } from 'ts-morph';
|
|
7
|
+
import { Project, ModuleDeclarationKind } from 'ts-morph';
|
|
8
8
|
import { CustomTypeModelFieldType, CustomTypeModelLinkSelectType, CustomTypeModelSliceType } from '@prismicio/types';
|
|
9
9
|
import { pascalCase as pascalCase$1, pascalCaseTransformMerge } from 'pascal-case';
|
|
10
10
|
import Joi from 'joi';
|
|
@@ -154,6 +154,9 @@ const writeFieldDocsDescription = (config) => {
|
|
|
154
154
|
if (config.apiIDPath) {
|
|
155
155
|
config.writer.writeLine(`- **API ID Path**: ${config.apiIDPath}`);
|
|
156
156
|
}
|
|
157
|
+
if (config.tabName) {
|
|
158
|
+
config.writer.writeLine(`- **Tab**: ${config.tabName}`);
|
|
159
|
+
}
|
|
157
160
|
if (config.documentationURL) {
|
|
158
161
|
config.writer.writeLine(`- **Documentation**: ${config.documentationURL}`);
|
|
159
162
|
}
|
|
@@ -185,6 +188,7 @@ const buildFieldDocs = (config) => {
|
|
|
185
188
|
placeholder,
|
|
186
189
|
defaultValue,
|
|
187
190
|
apiIDPath,
|
|
191
|
+
tabName: config.tabName,
|
|
188
192
|
documentationURL: getFieldDocumentationURL({ model: config.model })
|
|
189
193
|
});
|
|
190
194
|
}
|
|
@@ -200,7 +204,7 @@ const buildSharedSliceInterfaceName = (config) => {
|
|
|
200
204
|
return pascalCase(`${config.id} Slice`);
|
|
201
205
|
};
|
|
202
206
|
|
|
203
|
-
const
|
|
207
|
+
const addInterfacePropertyForField = (config) => {
|
|
204
208
|
switch (config.model.type) {
|
|
205
209
|
case "UID": {
|
|
206
210
|
break;
|
|
@@ -212,7 +216,8 @@ const addInterfacePropertyFromField = (config) => {
|
|
|
212
216
|
docs: buildFieldDocs({
|
|
213
217
|
id: config.id,
|
|
214
218
|
model: config.model,
|
|
215
|
-
path: config.path
|
|
219
|
+
path: config.path,
|
|
220
|
+
tabName: config.tabName
|
|
216
221
|
})
|
|
217
222
|
});
|
|
218
223
|
break;
|
|
@@ -224,7 +229,8 @@ const addInterfacePropertyFromField = (config) => {
|
|
|
224
229
|
docs: buildFieldDocs({
|
|
225
230
|
id: config.id,
|
|
226
231
|
model: config.model,
|
|
227
|
-
path: config.path
|
|
232
|
+
path: config.path,
|
|
233
|
+
tabName: config.tabName
|
|
228
234
|
})
|
|
229
235
|
});
|
|
230
236
|
break;
|
|
@@ -236,7 +242,8 @@ const addInterfacePropertyFromField = (config) => {
|
|
|
236
242
|
docs: buildFieldDocs({
|
|
237
243
|
id: config.id,
|
|
238
244
|
model: config.model,
|
|
239
|
-
path: config.path
|
|
245
|
+
path: config.path,
|
|
246
|
+
tabName: config.tabName
|
|
240
247
|
})
|
|
241
248
|
});
|
|
242
249
|
break;
|
|
@@ -255,7 +262,8 @@ const addInterfacePropertyFromField = (config) => {
|
|
|
255
262
|
docs: buildFieldDocs({
|
|
256
263
|
id: config.id,
|
|
257
264
|
model: config.model,
|
|
258
|
-
path: config.path
|
|
265
|
+
path: config.path,
|
|
266
|
+
tabName: config.tabName
|
|
259
267
|
})
|
|
260
268
|
});
|
|
261
269
|
break;
|
|
@@ -267,7 +275,8 @@ const addInterfacePropertyFromField = (config) => {
|
|
|
267
275
|
docs: buildFieldDocs({
|
|
268
276
|
id: config.id,
|
|
269
277
|
model: config.model,
|
|
270
|
-
path: config.path
|
|
278
|
+
path: config.path,
|
|
279
|
+
tabName: config.tabName
|
|
271
280
|
})
|
|
272
281
|
});
|
|
273
282
|
break;
|
|
@@ -281,7 +290,8 @@ const addInterfacePropertyFromField = (config) => {
|
|
|
281
290
|
docs: buildFieldDocs({
|
|
282
291
|
id: config.id,
|
|
283
292
|
model: config.model,
|
|
284
|
-
path: config.path
|
|
293
|
+
path: config.path,
|
|
294
|
+
tabName: config.tabName
|
|
285
295
|
})
|
|
286
296
|
});
|
|
287
297
|
} else {
|
|
@@ -291,7 +301,8 @@ const addInterfacePropertyFromField = (config) => {
|
|
|
291
301
|
docs: buildFieldDocs({
|
|
292
302
|
id: config.id,
|
|
293
303
|
model: config.model,
|
|
294
|
-
path: config.path
|
|
304
|
+
path: config.path,
|
|
305
|
+
tabName: config.tabName
|
|
295
306
|
})
|
|
296
307
|
});
|
|
297
308
|
}
|
|
@@ -305,7 +316,8 @@ const addInterfacePropertyFromField = (config) => {
|
|
|
305
316
|
docs: buildFieldDocs({
|
|
306
317
|
id: config.id,
|
|
307
318
|
model: config.model,
|
|
308
|
-
path: config.path
|
|
319
|
+
path: config.path,
|
|
320
|
+
tabName: config.tabName
|
|
309
321
|
})
|
|
310
322
|
});
|
|
311
323
|
break;
|
|
@@ -319,7 +331,8 @@ const addInterfacePropertyFromField = (config) => {
|
|
|
319
331
|
docs: buildFieldDocs({
|
|
320
332
|
id: config.id,
|
|
321
333
|
model: config.model,
|
|
322
|
-
path: config.path
|
|
334
|
+
path: config.path,
|
|
335
|
+
tabName: config.tabName
|
|
323
336
|
})
|
|
324
337
|
});
|
|
325
338
|
break;
|
|
@@ -331,7 +344,8 @@ const addInterfacePropertyFromField = (config) => {
|
|
|
331
344
|
docs: buildFieldDocs({
|
|
332
345
|
id: config.id,
|
|
333
346
|
model: config.model,
|
|
334
|
-
path: config.path
|
|
347
|
+
path: config.path,
|
|
348
|
+
tabName: config.tabName
|
|
335
349
|
})
|
|
336
350
|
});
|
|
337
351
|
break;
|
|
@@ -343,7 +357,8 @@ const addInterfacePropertyFromField = (config) => {
|
|
|
343
357
|
docs: buildFieldDocs({
|
|
344
358
|
id: config.id,
|
|
345
359
|
model: config.model,
|
|
346
|
-
path: config.path
|
|
360
|
+
path: config.path,
|
|
361
|
+
tabName: config.tabName
|
|
347
362
|
})
|
|
348
363
|
});
|
|
349
364
|
}
|
|
@@ -357,7 +372,8 @@ const addInterfacePropertyFromField = (config) => {
|
|
|
357
372
|
docs: buildFieldDocs({
|
|
358
373
|
id: config.id,
|
|
359
374
|
model: config.model,
|
|
360
|
-
path: config.path
|
|
375
|
+
path: config.path,
|
|
376
|
+
tabName: config.tabName
|
|
361
377
|
})
|
|
362
378
|
});
|
|
363
379
|
break;
|
|
@@ -371,7 +387,8 @@ const addInterfacePropertyFromField = (config) => {
|
|
|
371
387
|
docs: buildFieldDocs({
|
|
372
388
|
id: config.id,
|
|
373
389
|
model: config.model,
|
|
374
|
-
path: config.path
|
|
390
|
+
path: config.path,
|
|
391
|
+
tabName: config.tabName
|
|
375
392
|
})
|
|
376
393
|
});
|
|
377
394
|
} else {
|
|
@@ -381,7 +398,8 @@ const addInterfacePropertyFromField = (config) => {
|
|
|
381
398
|
docs: buildFieldDocs({
|
|
382
399
|
id: config.id,
|
|
383
400
|
model: config.model,
|
|
384
|
-
path: config.path
|
|
401
|
+
path: config.path,
|
|
402
|
+
tabName: config.tabName
|
|
385
403
|
})
|
|
386
404
|
});
|
|
387
405
|
}
|
|
@@ -397,7 +415,8 @@ const addInterfacePropertyFromField = (config) => {
|
|
|
397
415
|
docs: buildFieldDocs({
|
|
398
416
|
id: config.id,
|
|
399
417
|
model: config.model,
|
|
400
|
-
path: config.path
|
|
418
|
+
path: config.path,
|
|
419
|
+
tabName: config.tabName
|
|
401
420
|
})
|
|
402
421
|
});
|
|
403
422
|
} else {
|
|
@@ -407,7 +426,8 @@ const addInterfacePropertyFromField = (config) => {
|
|
|
407
426
|
docs: buildFieldDocs({
|
|
408
427
|
id: config.id,
|
|
409
428
|
model: config.model,
|
|
410
|
-
path: config.path
|
|
429
|
+
path: config.path,
|
|
430
|
+
tabName: config.tabName
|
|
411
431
|
})
|
|
412
432
|
});
|
|
413
433
|
}
|
|
@@ -420,7 +440,8 @@ const addInterfacePropertyFromField = (config) => {
|
|
|
420
440
|
docs: buildFieldDocs({
|
|
421
441
|
id: config.id,
|
|
422
442
|
model: config.model,
|
|
423
|
-
path: config.path
|
|
443
|
+
path: config.path,
|
|
444
|
+
tabName: config.tabName
|
|
424
445
|
})
|
|
425
446
|
});
|
|
426
447
|
break;
|
|
@@ -432,7 +453,8 @@ const addInterfacePropertyFromField = (config) => {
|
|
|
432
453
|
docs: buildFieldDocs({
|
|
433
454
|
id: config.id,
|
|
434
455
|
model: config.model,
|
|
435
|
-
path: config.path
|
|
456
|
+
path: config.path,
|
|
457
|
+
tabName: config.tabName
|
|
436
458
|
})
|
|
437
459
|
});
|
|
438
460
|
break;
|
|
@@ -477,7 +499,8 @@ const addInterfacePropertyFromField = (config) => {
|
|
|
477
499
|
docs: buildFieldDocs({
|
|
478
500
|
id: config.id,
|
|
479
501
|
model: config.model,
|
|
480
|
-
path: config.path
|
|
502
|
+
path: config.path,
|
|
503
|
+
tabName: config.tabName
|
|
481
504
|
})
|
|
482
505
|
});
|
|
483
506
|
break;
|
|
@@ -626,7 +649,8 @@ const addInterfacePropertyFromField = (config) => {
|
|
|
626
649
|
docs: buildFieldDocs({
|
|
627
650
|
id: config.id,
|
|
628
651
|
model: config.model,
|
|
629
|
-
path: config.path
|
|
652
|
+
path: config.path,
|
|
653
|
+
tabName: config.tabName
|
|
630
654
|
})
|
|
631
655
|
});
|
|
632
656
|
break;
|
|
@@ -638,7 +662,8 @@ const addInterfacePropertyFromField = (config) => {
|
|
|
638
662
|
docs: buildFieldDocs({
|
|
639
663
|
id: config.id,
|
|
640
664
|
model: config.model,
|
|
641
|
-
path: config.path
|
|
665
|
+
path: config.path,
|
|
666
|
+
tabName: config.tabName
|
|
642
667
|
})
|
|
643
668
|
});
|
|
644
669
|
}
|
|
@@ -646,10 +671,11 @@ const addInterfacePropertyFromField = (config) => {
|
|
|
646
671
|
};
|
|
647
672
|
const addInterfacePropertiesForFields = (config) => {
|
|
648
673
|
for (const name in config.fields) {
|
|
649
|
-
|
|
674
|
+
addInterfacePropertyForField({
|
|
650
675
|
...config,
|
|
651
676
|
id: name.includes("-") ? `"${name}"` : name,
|
|
652
|
-
model: config.fields[name]
|
|
677
|
+
model: config.fields[name],
|
|
678
|
+
tabName: config.tabName
|
|
653
679
|
});
|
|
654
680
|
}
|
|
655
681
|
};
|
|
@@ -676,18 +702,22 @@ const addTypeAliasForCustomType = ({
|
|
|
676
702
|
}
|
|
677
703
|
]
|
|
678
704
|
});
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
705
|
+
for (const tabName in model.json) {
|
|
706
|
+
const { uid: _uidField, ...tabFields } = model.json[tabName];
|
|
707
|
+
addInterfacePropertiesForFields({
|
|
708
|
+
fields: tabFields,
|
|
709
|
+
interface: dataInterface,
|
|
710
|
+
sourceFile,
|
|
711
|
+
path: [
|
|
712
|
+
{
|
|
713
|
+
id: model.id,
|
|
714
|
+
model
|
|
715
|
+
}
|
|
716
|
+
],
|
|
717
|
+
fieldConfigs,
|
|
718
|
+
tabName
|
|
719
|
+
});
|
|
720
|
+
}
|
|
691
721
|
} else {
|
|
692
722
|
dataInterface = sourceFile.addTypeAlias({
|
|
693
723
|
name: pascalCase(`${model.id} Document Data`),
|
|
@@ -910,6 +940,13 @@ const generateTypes = (config = {}) => {
|
|
|
910
940
|
fieldConfigs: config.fieldConfigs || {}
|
|
911
941
|
});
|
|
912
942
|
}
|
|
943
|
+
if (config.customTypeModels.length > 0) {
|
|
944
|
+
sourceFile.addTypeAlias({
|
|
945
|
+
name: "AllDocumentTypes",
|
|
946
|
+
type: config.customTypeModels.map((customTypeModel) => pascalCase(`${customTypeModel.id} Document`)).join(" | "),
|
|
947
|
+
isExported: true
|
|
948
|
+
});
|
|
949
|
+
}
|
|
913
950
|
}
|
|
914
951
|
if (config.sharedSliceModels) {
|
|
915
952
|
for (const model of config.sharedSliceModels) {
|
|
@@ -920,20 +957,60 @@ const generateTypes = (config = {}) => {
|
|
|
920
957
|
});
|
|
921
958
|
}
|
|
922
959
|
}
|
|
960
|
+
if (config.clientIntegration?.includeCreateClientInterface) {
|
|
961
|
+
sourceFile.addImportDeclaration({
|
|
962
|
+
moduleSpecifier: "@prismicio/client",
|
|
963
|
+
namespaceImport: "prismic",
|
|
964
|
+
isTypeOnly: true
|
|
965
|
+
});
|
|
966
|
+
const clientModuleDeclaration = sourceFile.addModule({
|
|
967
|
+
name: '"@prismicio/client"',
|
|
968
|
+
hasDeclareKeyword: true,
|
|
969
|
+
declarationKind: ModuleDeclarationKind.Module
|
|
970
|
+
});
|
|
971
|
+
clientModuleDeclaration.addInterface({
|
|
972
|
+
name: "CreateClient",
|
|
973
|
+
callSignatures: [
|
|
974
|
+
{
|
|
975
|
+
parameters: [
|
|
976
|
+
{
|
|
977
|
+
name: "repositoryNameOrEndpoint",
|
|
978
|
+
type: "string"
|
|
979
|
+
},
|
|
980
|
+
{
|
|
981
|
+
name: "options",
|
|
982
|
+
type: "prismic.ClientConfig",
|
|
983
|
+
hasQuestionToken: true
|
|
984
|
+
}
|
|
985
|
+
],
|
|
986
|
+
returnType: (config.customTypeModels?.length || 0) > 0 ? "prismic.Client<AllDocumentTypes>" : "prismic.Client"
|
|
987
|
+
}
|
|
988
|
+
]
|
|
989
|
+
});
|
|
990
|
+
}
|
|
923
991
|
return getSourceFileText(sourceFile);
|
|
924
992
|
};
|
|
925
993
|
|
|
926
994
|
const configSchema = Joi.object({
|
|
927
|
-
repositoryName: Joi.string(),
|
|
995
|
+
repositoryName: Joi.string().when("locales.fetchFromRepository", {
|
|
996
|
+
is: true,
|
|
997
|
+
then: Joi.required()
|
|
998
|
+
}).when("models.fetchFromRepository", {
|
|
999
|
+
is: true,
|
|
1000
|
+
then: Joi.required()
|
|
1001
|
+
}),
|
|
928
1002
|
accessToken: Joi.string(),
|
|
929
1003
|
customTypesAPIToken: Joi.string(),
|
|
930
1004
|
output: Joi.string(),
|
|
1005
|
+
clientIntegration: {
|
|
1006
|
+
includeCreateClientInterface: Joi.boolean
|
|
1007
|
+
},
|
|
931
1008
|
locales: Joi.alternatives(Joi.array().items(Joi.string().required()), Joi.object({
|
|
932
1009
|
ids: Joi.array().items(Joi.string().required()),
|
|
933
1010
|
fetchFromRepository: Joi.boolean()
|
|
934
1011
|
})),
|
|
935
|
-
models: Joi.alternatives(Joi.array().items(Joi.string()
|
|
936
|
-
files: Joi.array().items(Joi.string()
|
|
1012
|
+
models: Joi.alternatives(Joi.array().items(Joi.string()), Joi.object({
|
|
1013
|
+
files: Joi.array().items(Joi.string()),
|
|
937
1014
|
fetchFromRepository: Joi.boolean()
|
|
938
1015
|
})),
|
|
939
1016
|
fields: Joi.object({
|
|
@@ -944,23 +1021,6 @@ const configSchema = Joi.object({
|
|
|
944
1021
|
catalogTypes: Joi.object().pattern(Joi.string(), Joi.string().required())
|
|
945
1022
|
})
|
|
946
1023
|
})
|
|
947
|
-
}).when(Joi.object({
|
|
948
|
-
locales: Joi.object({
|
|
949
|
-
fetchFromRepository: Joi.valid(true)
|
|
950
|
-
})
|
|
951
|
-
}), {
|
|
952
|
-
then: Joi.object({
|
|
953
|
-
repositoryName: Joi.required()
|
|
954
|
-
})
|
|
955
|
-
}).when(Joi.object({
|
|
956
|
-
models: Joi.object({
|
|
957
|
-
fetchFromRepository: Joi.valid(true)
|
|
958
|
-
})
|
|
959
|
-
}), {
|
|
960
|
-
then: Joi.object({
|
|
961
|
-
repositoryName: Joi.required(),
|
|
962
|
-
customTypesAPIToken: Joi.required()
|
|
963
|
-
})
|
|
964
1024
|
});
|
|
965
1025
|
|
|
966
1026
|
const loadLocaleIDs = async (config) => {
|
|
@@ -1090,7 +1150,6 @@ ${configPath} already exists.`);
|
|
|
1090
1150
|
|
|
1091
1151
|
const config: Config = {
|
|
1092
1152
|
output: "./types.generated.ts",
|
|
1093
|
-
|
|
1094
1153
|
models: ["./customtypes/**/index.json", "./slices/**/model.json"],
|
|
1095
1154
|
};
|
|
1096
1155
|
|
|
@@ -1127,13 +1186,23 @@ Created prismic-ts-codegen config file: ${configPath}`);
|
|
|
1127
1186
|
accessToken: config.accessToken,
|
|
1128
1187
|
fetchFromRepository: config.locales && "fetchFromRepository" in config.locales && config.locales.fetchFromRepository
|
|
1129
1188
|
});
|
|
1189
|
+
const hasCustomTypeModels = customTypeModels.length > 0;
|
|
1130
1190
|
const types = generateTypes({
|
|
1131
1191
|
customTypeModels,
|
|
1132
1192
|
sharedSliceModels,
|
|
1133
|
-
localeIDs
|
|
1193
|
+
localeIDs,
|
|
1194
|
+
fieldConfigs: config.fields,
|
|
1195
|
+
clientIntegration: {
|
|
1196
|
+
includeCreateClientInterface: hasCustomTypeModels ? config.clientIntegration?.includeCreateClientInterface ?? true : false
|
|
1197
|
+
}
|
|
1134
1198
|
});
|
|
1199
|
+
if (config.clientIntegration?.includeCreateClientInterface && !hasCustomTypeModels) {
|
|
1200
|
+
console.info("[INFO]: prismic-ts-codegen was configured to automatically integrate with `@prismicio/client`, but the integration was generated because no Custom Type models were found. Automatic integration requires at least one Custom Type model.");
|
|
1201
|
+
}
|
|
1135
1202
|
if (config.output) {
|
|
1136
1203
|
writeFileSync(resolve(config.output), types);
|
|
1204
|
+
console.info(`
|
|
1205
|
+
Generated types in: ${config.output}`);
|
|
1137
1206
|
} else {
|
|
1138
1207
|
process.stdout.write(types + "\n");
|
|
1139
1208
|
}
|