-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDocGenerator.ts
861 lines (764 loc) · 26.6 KB
/
DocGenerator.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
import fs from 'fs';
import {
ArrayTypeParser,
ClassMethodParser,
ClassParser,
InterfaceParser,
IntersectionTypeParser,
IntrinsicTypeParser,
LiteralTypeParser,
ProjectParser,
ReferenceTypeParser,
ReflectionTypeParser,
TypeAliasParser,
UnionTypeParser,
} from 'typedoc-json-parser';
import {
IInterfaceDocs,
IJson,
IMethodDocs,
IMethodParamTypes,
IMethodReturnType,
IParamType,
IReturnType,
ITypeAliasDocs,
} from './src/stories/types';
export const parsedProjectDocs = './src/stories/docs.ts';
export const typedocDocs = './docs/documentation.json';
/*eslint-disable */
class DocGenerator {
project: ProjectParser;
allClasses: ClassParser[] = [];
allInterfaces: InterfaceParser[] = [];
allTypeAliases: TypeAliasParser[] = [];
constructor() {
// Reading docs generated by typedoc
const raw = fs.readFileSync(typedocDocs, { encoding: 'utf-8' });
// Converting to JSON
const parsedData = JSON.parse(raw);
const projectData: ProjectParser | undefined = new ProjectParser({ data: parsedData });
this.project = projectData;
let classObj: ClassParser[] = [];
this.project.namespaces?.forEach((namespace) => {
classObj = [...classObj, ...namespace.classes];
});
this.allClasses = classObj;
let interfaceArray: InterfaceParser[] = [];
this.project.namespaces?.forEach((namespace) => {
interfaceArray = [...interfaceArray, ...namespace.interfaces];
});
interfaceArray = [...interfaceArray, ...this.project.interfaces];
this.allInterfaces = interfaceArray;
this.allTypeAliases = this.project.typeAliases;
this.project.namespaces?.forEach((namespace) => {
this.allTypeAliases = [...this.allTypeAliases, ...namespace.typeAliases];
});
}
/** Creates documentation for Classes, Interfaces, Methods, and types
* @returns Object with all the parsed docs and saves them to the file
*/
createAllDocsJson() {
const classesDocs = this.createAllClassesJson();
const methodDocs = this.createAllMethodsJson();
const interfaceDocs: (IInterfaceDocs | undefined)[] = this.createAllInterfacesJson();
const typeAliasDocs: (ITypeAliasDocs | undefined)[] = this.createAllTypeAliasJson();
// Create a typescript file that exports an object containing classesDocs and methodDocs
fs.writeFileSync(
parsedProjectDocs,
`export default { classesDocs: ${JSON.stringify(classesDocs)}, methodDocs: ${JSON.stringify(
methodDocs,
)},interfaceDocs: ${JSON.stringify(interfaceDocs)}, typeAliasDocs: ${JSON.stringify(typeAliasDocs)} }`,
);
return {
classesDocs,
methodDocs,
interfaceDocs,
typeAliasDocs,
};
}
/**
* A getter to get the parsed project documentation json
* @returns Parsed project as a json
*/
getProject(): ProjectParser {
return this.project;
}
/**
* A getter to get the parsed json documentation for a class
* @param className Name of the the class to parse
* @returns Parsed class json if the class exist, undefined otherwise
*/
getClass(className: string): ClassParser | undefined {
return this.allClasses.find((classObj) => classObj.name === className);
}
/**
* If the classObj is not undefined, return the method with the given name from the classObj's
* methods array, or undefined if no such method exists.
* @param methodName Name of the method you want to find.
* @param classObj Class object that you want to search for the method in.
* @returns Method object with the name that matches the methodName parameter.
*/
getMethod(methodName: string, classObj?: ClassParser): ClassMethodParser | undefined {
if (!classObj) return undefined;
return classObj.methods?.find((method) => method.name === methodName);
}
/**
* A getter to get the parsed json documentation for an interface
* @param interfaceName Name of the interface to parse
* @returns
*/
getInterface(interfaceName: string): InterfaceParser | undefined {
return this.allInterfaces.find((obj) => obj.name === interfaceName);
}
/**
* A getter to get the parsed json documentation for an type
* @param typeAliasName Name of the type to parse
* @returns Parsed type documentation as json if the type exist, undefined otherwise
*/
getTypeAlias(typeAliasName: string): TypeAliasParser | undefined {
return this.allTypeAliases.find((obj) => obj.name === typeAliasName);
}
/**
* A getter to get the parsed json documentation for all the classes
* @returns Parsed Array of all classes documentation as json if the classes exists, undefined otherwise
*/
getAllClasses(): ClassParser[] | undefined {
return this.allClasses;
}
/**
* A getter to get the parsed json documentation for all the interfaces
* @returns Parsed Array of all interfaces documentation as json if the classes exists, undefined otherwise
*/
getAllInterfaces(): InterfaceParser[] | undefined {
return this.allInterfaces;
}
/**
* A getter to get the parsed json documentation for all the types
* @returns Parsed Array of all types documentation as json if the types exists, undefined otherwise
*/
getAllTypeAlias(): TypeAliasParser[] | undefined {
return this.allTypeAliases;
}
/**
* A getter to get type of an type alias documentation
* @param typeJson Json object of type alias properties
* @returns Type of the type alias
*/
getTypeAliasType(typeJson: any) {
const adjustedType = this.getAdjustedType(typeJson);
let typeAliasTypes;
if (adjustedType === 'union') {
const typeJsonTypes = typeJson?.types;
typeAliasTypes = typeJsonTypes?.map((obj) => {
return obj.value;
});
} else {
typeAliasTypes = adjustedType;
}
return typeAliasTypes;
}
/**
* A getter to get the documentation for a reflection
* @param reflection Object with reflection properties
* @returns
*/
getReflectionString(reflection) {
if (!reflection.children) {
return '';
}
const children: { [x: string]: any }[] = reflection.children;
let typeString = '{ ';
children.map((child, i) => {
if (child.name) {
typeString += `${child.name}: `;
const type = this.getAdjustedType(child.type);
typeString += ` ${type}`;
if (i + 1 !== children.length) {
typeString += ', ';
}
}
});
typeString += ' }';
return typeString;
}
reflectionTypeParser(typeJson: ReflectionTypeParser) {
const children = typeJson.reflection?.children;
if (!typeJson.reflection?.children) {
return '';
}
let typeString = '{ ';
children?.map((child, i) => {
if (child.name) {
typeString += `${child.name}: `;
const type = this.getAdjustedType(child.type);
typeString += ` ${type}`;
if (i + 1 !== children.length) {
typeString += ', ';
}
}
});
typeString += ' }';
return typeString;
}
getSeparatorSymbol(typeKind) {
return typeKind === 'union' ? ' | ' : typeKind === 'intersection' ? ' & ' : '';
}
getTypeString(
typeObj: ReflectionTypeParser | LiteralTypeParser | ArrayTypeParser | ReferenceTypeParser | any,
prevTypeString?: string,
): string {
let finalTypeString = prevTypeString ?? '';
if (typeObj instanceof ReferenceTypeParser) {
if (typeObj.name === 'Promise' || typeObj.name === 'Record') {
const typeObjects = typeObj.typeArguments;
let typeObjArr: any[] = [];
if (typeObj.name === 'Promise') {
typeObjArr = typeObjects.reverse();
} else {
typeObjArr = typeObjects;
}
typeObjArr.map((type, index) => {
finalTypeString += this.typeParser(type);
if (typeObj.name === 'Record' && index + 1 !== typeObjects.length) {
finalTypeString += ', ';
}
});
if (typeObj.name === 'Record') {
finalTypeString = `Record<${finalTypeString}>`;
}
} else {
finalTypeString += typeObj.name;
}
} else if (typeObj instanceof ArrayTypeParser) {
// const arrType = typeObj.type;
let typeString = (typeObj.type as any).name ?? (typeObj.type as any).type;
typeString += '[ ]';
finalTypeString += typeString;
} else if (typeObj instanceof ReflectionTypeParser) {
finalTypeString += this.reflectionTypeParser(typeObj);
} else if (typeObj instanceof LiteralTypeParser) {
finalTypeString += typeObj.value;
} else if (typeObj instanceof IntrinsicTypeParser) {
finalTypeString += this.getAdjustedType(typeObj);
} else if (typeObj instanceof IntersectionTypeParser || typeObj instanceof UnionTypeParser) {
const intersectionTypes = typeObj.types;
let typeString = '';
intersectionTypes.reverse()?.map((typeNew, index) => {
typeString += this.getTypeString(typeNew);
if (index + 1 !== intersectionTypes.length) {
typeString += this.getSeparatorSymbol(typeObj.kind);
}
});
finalTypeString += typeString;
}
return finalTypeString;
}
typeParser(typeJson) {
const typeKind = typeJson?.kind ?? '';
let types, typeArguments;
if (typeJson?.hasOwnProperty('types')) {
types = typeJson.types ?? undefined;
}
if (typeJson?.hasOwnProperty('typeArguments')) {
typeArguments = typeJson.typeArguments ?? undefined;
}
const isUnion = typeKind === 'union';
const selectedTypeArray = isUnion ? (types ? types : typeArguments) : undefined;
let returnType = '';
if (selectedTypeArray?.length > 0 && (typeKind === 'union' || typeKind === 'intersection')) {
selectedTypeArray.reverse().map((parameter, index: number) => {
let typeString = this.getTypeString(parameter);
if (index + 1 !== selectedTypeArray.length) {
typeString += this.getSeparatorSymbol(typeKind);
}
returnType += typeString;
});
}
const type = this.getTypeString(typeJson);
if (!returnType) {
returnType = type;
}
return returnType;
}
getTypeObject(
typeObj: ReflectionTypeParser | LiteralTypeParser | ArrayTypeParser | ReferenceTypeParser | any,
): IReturnType {
let masterType = '';
let types: string[] = [];
const separatorSymbol: IReturnType['separatorSymbol'] = this.getSeparatorSymbolForTypeObject(typeObj);
if (typeObj instanceof ReferenceTypeParser) {
if (typeObj.name === 'Promise' || typeObj.name === 'Record') {
masterType = typeObj.name;
const typeObjects = typeObj.typeArguments;
typeObjects.map((type) => {
const newTypes = this.getTypeObject(type);
if (Array.isArray(newTypes.type)) {
types = [...types, ...newTypes.type];
} else {
types.push(newTypes.type);
}
});
if (masterType === 'Promise') {
types = types.reverse();
}
} else {
types.push(typeObj.name);
}
} else if (typeObj instanceof ArrayTypeParser) {
let typeString: string = (typeObj.type as any).name ?? (typeObj.type as any).type;
typeString += '[ ]';
types.push(typeString);
} else if (typeObj instanceof ReflectionTypeParser) {
types.push(this.reflectionTypeParser(typeObj));
} else if (typeObj instanceof LiteralTypeParser) {
types.push(typeObj.value);
} else if (typeObj instanceof IntrinsicTypeParser) {
types.push(this.getAdjustedType(typeObj));
} else if (typeObj instanceof IntersectionTypeParser || typeObj instanceof UnionTypeParser) {
const intersectionTypes = typeObj.types;
intersectionTypes?.map((typeNew) => {
types.push(this.getTypeString(typeNew));
});
}
if (types.length === 1 && !masterType) {
const result = {
// separatorSymbol,
type: types[0],
fullType: types[0],
};
return result;
}
const result = {
masterType,
separatorSymbol,
type: types,
fullType: `${masterType ? `${masterType}<` : ''}${types.join(', ')}${masterType ? `>` : ''}`,
};
return result;
}
typeParserObject(typeJson, reverseInternalArray: boolean = false) {
const typeKind = typeJson?.kind ?? '';
let types, typeArguments;
if (typeJson?.hasOwnProperty('types')) {
types = typeJson.types ?? undefined;
}
if (typeJson?.hasOwnProperty('typeArguments')) {
typeArguments = typeJson.typeArguments ?? undefined;
}
const isUnion = typeKind === 'union';
const selectedTypeArray = isUnion ? (types ? types : typeArguments) : undefined;
let finalType: IReturnType[] | IReturnType = [];
if (selectedTypeArray?.length > 0 && (typeKind === 'union' || typeKind === 'intersection')) {
selectedTypeArray.map((parameter) => {
const typeObj = this.getTypeObject(parameter);
(finalType as IReturnType[]).push(typeObj);
});
}
const type = this.getTypeObject(typeJson);
if (!finalType || !finalType.length) {
finalType = type;
}
let allStringTypes = true;
if (Array.isArray(finalType)) {
finalType?.map((typeStr) => {
if (typeStr.hasOwnProperty('masterType')) {
allStringTypes = false;
}
});
}
if (allStringTypes && Array.isArray(finalType)) {
let typesResult: string[] = [];
finalType?.map((typeStr) => {
typesResult = [...typesResult, typeStr.type as string];
// const allTypes = typeStr.types as string[];
// allTypes?.map((typeNew) => {
// typesResult = [...typesResult, typeNew];
// });
});
const obj: IReturnType = {
separatorSymbol: this.getSeparatorSymbolForTypeObject(typeJson),
type: reverseInternalArray ? typesResult.reverse() : typesResult,
fullType: typesResult.join(` ${this.getSeparatorSymbolForTypeObject(typeJson)} `),
};
return obj;
}
return finalType;
}
getSeparatorSymbolForTypeObject(typeObj): IReturnType['separatorSymbol'] {
if (typeObj.name === 'Promise') return '|';
switch (typeObj.typeKind ?? typeObj.kind) {
case 'union':
return '|';
case 'intersection':
return '&';
default:
return ',';
}
}
/**
* A getter to get the interface documentation
* @param interfaceObject Json interface object from the parsed project
* @returns Parsed docs object for the interface if it exists, undefined otherwise
*/
createInterfaceDocs(interfaceObject?: InterfaceParser | string): IInterfaceDocs | undefined {
let interfaceObj: InterfaceParser | undefined;
if (typeof interfaceObject === 'string') {
interfaceObj = this.getInterface(interfaceObject);
} else {
interfaceObj = interfaceObject;
}
if (!interfaceObj) {
return undefined;
}
const properties = interfaceObj.properties.reverse().map((property) => {
const propertyDocs = {
label: property.name,
description: property.comment.description ?? '',
type: this.typeParserObject(property.type),
};
return propertyDocs;
});
const docs: IInterfaceDocs = {
name: interfaceObj.name,
comments: { summary: interfaceObj.comment.description },
params: properties,
};
return docs;
}
getAdjustedType(json: any): string {
if (json?.name) {
if (json?.kind === 'array') {
return `${json?.name}[ ]`;
}
return json?.name;
} else if (json?.type && json?.kind === 'array') {
return `${json.type.name ?? json.type.type}[ ]`;
} else if (typeof json?.type === 'string') {
return json?.type;
} else if (json?.type) {
return (json.type as IParamType).type ?? json.type.kind;
} else if (json?.value) {
if (json?.kind === 'array') {
return `${json?.value}[ ]`;
}
return json.value;
} else {
return json?.kind;
}
}
private getClassObj = (classObject?: string | ClassParser): ClassParser | undefined => {
let classObj: ClassParser | undefined;
if (typeof classObject === 'string') {
classObj = this.getClass(classObject);
} else classObj = classObject;
if (!classObj) return undefined;
else return classObj;
};
getConstructorParams(construct: ClassParser['construct']) {
const parameters = construct.parameters;
const parameterTypes = parameters.map((param) => {
const obj = {
name: param.name,
type: this.typeParser(param.type),
};
return obj;
});
return parameterTypes;
}
createClassDocs(classObj: string | ClassParser) {
let classObject: ClassParser | undefined;
if (typeof classObj === 'string') {
classObject = this.allClasses.find((obj) => obj.name === classObj);
} else {
classObject = classObj;
}
if (!classObject || !classObject.name) {
return undefined;
}
const construct = classObject.construct;
const name = classObject.name;
const constructor = {
accessibility: construct.accessibility,
parameters: this.getConstructorParams(construct),
};
const comments = {
summary: classObject.comment.description,
returnSummary: this.getReturnSummary(classObject.comment.blockTags),
};
const properties = classObject.properties.map((property) => {
const prop = property;
const obj = {
name: property.name,
comments: {
summary: property.comment.description,
returnSummary: this.getReturnSummary(property.comment.blockTags),
},
type: this.typeParserObject(prop.type, true),
};
return obj;
});
const docs = {
name,
constructor,
comments,
properties,
};
return docs;
}
getReturnSummary(blockTags: any[]) {
return blockTags.find((tag) => tag.name === 'returns')?.text;
}
createMethodDocs(methodName: string, classObject?: ClassParser | string | undefined): IMethodDocs | undefined {
const classObj = this.getClassObj(classObject);
if (!classObj) return undefined;
const method = this.getMethod(methodName, classObj);
const label = this.createMethodDefinition(method);
const name = method?.name;
const parameters = this.createParameterDefinition(method);
const comments = this.createCommentDocs(method);
const returnTypes = this.typeParserObject(method?.signatures[0].returnType, true);
const async = this.isAsyncMethod(method);
const accessibility: 'public' | 'private' | 'protected' | undefined = method?.accessibility;
const docs = {
async,
name,
label,
params: parameters,
comments,
returnTypes,
accessibility,
};
return docs;
}
createTypeAliasDocs(typeAliasObject: string | TypeAliasParser) {
let typeAliasObj: TypeAliasParser | undefined;
if (typeof typeAliasObject === 'string') {
typeAliasObj = this.getTypeAlias(typeAliasObject);
} else {
typeAliasObj = typeAliasObject;
}
if (!typeAliasObj) {
return undefined;
}
const type = this.typeParserObject(typeAliasObj.type);
const docs: ITypeAliasDocs = {
name: typeAliasObj.name,
comments: { summary: typeAliasObj.comment.description ?? '' },
type: type,
};
return docs;
}
createAllTypeAliasJson() {
const docs = this.allTypeAliases.map((obj) => {
const objDocs = this.createTypeAliasDocs(obj);
return objDocs;
});
return docs;
}
createAllInterfacesJson() {
const docs = this.allInterfaces.map((obj) => {
const objDocs = this.createInterfaceDocs(obj);
return objDocs;
});
return docs;
}
createAllMethodsJson() {
const allClasses = this.getAllClasses();
const allIMethodDocs = allClasses?.map((classObj) => {
const className = classObj?.name;
const classMethods = classObj?.methods.map((method) => {
const data = this.createMethodDocs(method.name, className);
const methodDocs = {
className: classObj?.name,
data,
};
if (data?.accessibility === 'private') {
return undefined;
} else return methodDocs;
});
return classMethods;
});
const finalIMethodDocs: { className: string; data: IMethodDocs | undefined }[] = [];
allIMethodDocs?.forEach((method) =>
method.forEach((meth) => {
if (meth !== null && meth?.className && meth.data?.name) {
finalIMethodDocs.push(meth);
}
}),
);
return finalIMethodDocs;
}
createAllClassesJson() {
const allClasses = this.getAllClasses();
const allCalssesDocs = allClasses?.map((classObj) => {
const classDocs = this.createClassDocs(classObj.name);
return classDocs;
});
return allCalssesDocs;
}
createAllMethodsDocs(classObj: string | ClassParser | undefined): (IMethodDocs | undefined)[] | undefined {
let classObject: ClassParser | undefined;
if (typeof classObj === 'string') {
classObject = this.getClass(classObj);
} else classObject = classObj;
if (classObject) {
const methods = classObject.methods;
const methodsDocs = methods.map((method) => {
return this.createMethodDocs(method.name, classObject);
});
return methodsDocs;
}
return undefined;
}
/**
* If the method's return type is a Promise, then it's an async method
* @param method - any | undefined
* @returns A boolean value.
*/
isAsyncMethod(method: ClassMethodParser | undefined): boolean {
const json: IJson | undefined = method?.signatures[0].returnType;
if (json?.name === 'Promise') return true;
else return false;
}
/**
* It takes a string and returns an object with a label property that has the same value as the string
* @param label - The label of the type.
* @returns An object with a label property.
*/
private createTypeObject(label: string) {
return {
label: label,
};
}
/**
* It takes a method object and returns an array of IMethodReturnType objects
* @param {any | undefined} method - any | undefined
* @returns An array of IMethodReturnType objects.
*/
createReturnTypes(method: ClassMethodParser | undefined): IMethodReturnType[] | undefined {
const returnType: IJson | undefined = method?.signatures[0].returnType;
let typesArr;
if (returnType?.hasOwnProperty('typeArguments') || returnType?.hasOwnProperty('types')) {
const typeStringArr: any[] = [];
typesArr = returnType?.types ?? returnType.typeArguments;
if (typesArr) {
typesArr.reverse().map((type) => {
if (type.types) {
type.types.reverse().map((newType) => {
typeStringArr.push(this.getTypeString(newType));
});
} else {
typeStringArr.push(this.getTypeString(type));
}
});
}
if (typeStringArr.length > 0) {
const types = typeStringArr.map((typeString) => {
return this.createTypeObject(typeString as string);
});
return types;
}
}
const returnString = this.typeParser(returnType);
const types = this.createTypeObject(returnString);
return [types];
}
/**
* It takes a method and returns an object with the method's summary and return summary
* @param {any | undefined} method - any | undefined
* @returns An object with the summary and returnSummary properties.
*/
private createCommentDocs(method: ClassMethodParser | undefined) {
const comments = method?.signatures[0].comment;
const summary = comments?.description;
const returnSummary = comments?.blockTags.find((tag) => tag.name === 'returns')?.text;
const commentDocs = {
summary,
returnSummary,
};
return commentDocs;
}
/**
* It takes a method from the parsed documentation and returns an array of objects that contain the
* name, type, and description of each parameter
* @param method - any | undefined
* @returns An array of objects with the following properties:
* - label
* - type
* - description
*/
private createParameterDefinition(method: ClassMethodParser | undefined): IMethodParamTypes[] {
const parameters = method?.signatures[0].parameters;
/*eslint-disable */
let parsedParameters: IMethodParamTypes[] = []; // eslint-disable-line no-use-before-define
/*eslint-enable */
/* It's iterating over the parameters array and creating an object for each parameter. */
parameters?.forEach((parameter) => {
const obj = {
label: parameter.name,
type: this.typeParser(parameter.type),
description: parameter.comment.description,
};
parsedParameters.push(obj);
});
return parsedParameters;
}
/**
* It takes a method object and returns a string that represents the method's signature
* @param method - any | undefined
* @returns A string that is a method definition.
*/
private createMethodDefinition(method: any | undefined) {
let methodDefinition = '';
const methodSignature = method?.signatures[0];
const parameters = method?.signatures[0].parameters;
const returnType = method?.signatures[0].returnType;
methodDefinition += `${methodSignature?.name}(`;
let parameterString = '';
parameterString += parameters?.map((parameter) => {
let parameterDefinition = '';
parameterDefinition += ` ${parameter.name}`;
// const json: Json = parameter.type;
const parameterType = this.typeParser(parameter.type);
parameterDefinition += `: ${parameterType}`;
return parameterDefinition;
});
methodDefinition += parameterString + ' )';
let returnString = ': ';
if (returnType?.name === 'Promise') {
returnString += 'Promise< ';
returnString += this.typeParser(returnType);
returnString += ' >';
} else {
returnString += this.typeParser(returnType ?? '');
}
if (returnString !== ': ') methodDefinition += returnString;
return methodDefinition;
}
}
export default new DocGenerator();
const docs = new DocGenerator();
docs.createAllDocsJson();
// Convert examples to json
const exampleDirectory = './src/examples/';
const exampleJsonFile = './codeExamples.json';
export function convertExamplesToJson(dirname: string) {
fs.readdir(dirname, function (err, filenames) {
const data: { [x: string]: string } = {};
if (err) {
console.log(err);
return;
}
filenames.forEach(function (filename) {
const content = fs.readFileSync(dirname + filename, 'utf-8');
const className = filename.split('-')[0];
let methodName = filename.split('-')[1];
if (methodName && methodName !== '.js') {
methodName = methodName.split('.')[0];
data[`${className}.${methodName}`] = content;
}
});
fs.writeFileSync(exampleJsonFile, JSON.stringify(data));
});
}
convertExamplesToJson(exampleDirectory);