-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-types.ts
149 lines (127 loc) · 3.72 KB
/
generate-types.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
import {
Project,
VariableDeclarationKind,
InterfaceDeclaration,
FunctionDeclaration,
SourceFile,
ParameterDeclaration,
Type,
} from 'ts-morph';
const fs = require('fs');
import * as falso from '@ngneat/falso';
import * as process from 'process';
const Keys = (intName: string): string[] => {
const project = new Project();
const sourceFile = project.addSourceFileAtPath(
`./node_modules/@ngneat/falso/lib/**/*{.d.ts}`,
);
const node = sourceFile.getInterface(intName)!;
const allKeys = node.getProperties().map((p) => p.getName());
return allKeys;
};
const getFunctions = (): FunctionDeclaration[] => {
const project = new Project();
const sourceFiles = project.addSourceFilesAtPaths(
`./node_modules/@ngneat/falso/lib/*.d.ts`,
);
const functions = [];
sourceFiles.forEach((sf: SourceFile) => {
functions.push(...sf.getFunctions());
});
return functions;
};
const buildTypeProps = (type: Type): any => {
const params = {};
if (type.isUnion() && !type.isBoolean()) {
return type.getUnionTypes().map((t) => t.getText().slice(1, -1));
}
if (type.isClassOrInterface() || type.isObject()) {
type.getProperties().map((p) => {
try {
const tt = p.getValueDeclarationOrThrow().getType();
params[p.getName()] = buildTypeProps(tt);
} catch (e) {
console.log(`Error: ${p.compilerSymbol.getName()}`, e.message);
}
});
return params;
}
return type.getText();
};
const getFunctionParams = (f: FunctionDeclaration): any => {
const paramsDec = f.getParameters();
const params = {};
paramsDec.forEach((p: ParameterDeclaration) => {
const name = p.getName();
if (name !== 'options') {
return;
}
const type = p.getType();
type.getProperties().forEach((p) => {
if (p.getName() === 'locale' || p.getName() === 'length') {
return;
}
params[p.getName()] = buildTypeProps(
p.getValueDeclarationOrThrow().getType(),
);
});
});
return params;
};
const formatFunctionName = (f: FunctionDeclaration): string => {
return f
.getName()
.replace(/((?<=[A-Z])[A-Z](?=([A-Z]|\b)))/g, (letter) =>
letter.toLowerCase(),
)
.replace(/[A-Z]/g, (letter) => `_${letter.toLowerCase()}`)
.replace('rand_', '');
};
// class SubClass {
// aa: string;
// bb: number;
// cc: boolean;
// }
//
// interface SubInterface {
// a: string;
// b: string;
// c: number;
// d: SubClass;
// m: SubType;
// n: SubType2;
// p: Required<SS2>;
// }
//
// interface SS2 {
// aaa: string;
// bbb: number;
// }
//
// type SubType = 'Hello' | 'There' | 'World';
// type SubType2 = Partial<SS2>;
// const project = new Project();
// const sourceFile = project.addSourceFileAtPath('./generate-types.ts');
// const intfc = sourceFile.getInterface('SubInterface');
// const typ = sourceFile.getTypeAliasOrThrow('SubType');
// console.log(typ.getChildCount());
// console.log(typ.getType().getProperties().map(p => p.getTypeAtLocation(typ).getText()));
// const somethingTypeAlias = sourceFile.getTypeAliasOrThrow('SubType');
// const somethingType = somethingTypeAlias.getType();
// const properties = somethingType.getProperties();
// for (const prop of properties)
// console.log(prop.getTypeAtLocation(somethingTypeAlias).getText());
// process.exit(1);
// const params = buildTypeProps(intfc.getType());
// console.log(params);
// process.exit(0);
const functions = getFunctions();
const types = functions.map((f: FunctionDeclaration) => {
return {
func: f.getName(),
name: formatFunctionName(f),
properties: getFunctionParams(f),
};
});
fs.writeFileSync('data/types.json', JSON.stringify(types, null, 2));
console.log(`Done writing ${types.length} types to data/types.json`);