-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathtest.ts
127 lines (115 loc) · 3.38 KB
/
test.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
import * as ts from 'typescript';
import { Node, ParameterDeclaration, SyntaxKind } from 'typescript';
import { readFileSync } from 'fs';
const DECORATOR_NAME = 'RpcHandler';
type ParamsOpenRPC = {
name: string;
description?: string;
required: boolean;
schema: {
type: string | undefined;
};
};
function getDeclaration(
node: Node,
nameDeclaration: SyntaxKind,
findone: true
): undefined | Node;
function getDeclaration(
node: Node,
nameDeclaration: SyntaxKind,
findone: false
): Node[];
function getDeclaration(
node: Node,
nameDeclaration: SyntaxKind,
findone: boolean
): undefined | Node | Node[] {
const result: Node[] = [];
function iterate(node: Node) {
if (node.kind === nameDeclaration) {
result.push(node);
}
if (findone && result.length === 1) return;
ts.forEachChild(node, iterate);
}
iterate(node);
return findone ? result.shift() : result;
}
function getName(node: Node): string | null {
const identifier = getDeclaration(node, SyntaxKind.Identifier, true);
if (!identifier) return null;
return identifier.getText().replace('\n ', '');
}
function getMethodForClass(node: Node): Node[] {
const methodDeclaration = getDeclaration(
node,
SyntaxKind.MethodDeclaration,
false
);
return methodDeclaration.filter((i) => {
const isPrivate = !!getDeclaration(i, SyntaxKind.PrivateKeyword, true);
if (isPrivate) return false;
const isPrivateIdentifier = !!getDeclaration(
i,
SyntaxKind.PrivateIdentifier,
true
);
if (isPrivateIdentifier) return false;
const isProtected = !!getDeclaration(i, SyntaxKind.ProtectedKeyword, true);
return !isProtected;
});
}
function getParams(node: Node): ParamsOpenRPC[][] {
const methodDeclaration = getMethodForClass(node);
return methodDeclaration.map((method) => {
return getDeclaration(method, SyntaxKind.Parameter, false)
.map<ParamsOpenRPC | null>((i, index) => {
let param = i as ParameterDeclaration;
let type = undefined;
if (param.type) {
if (param.type.kind !== SyntaxKind.TypeReference) {
type = param.type.getText();
}
}
return {
name: param.name.getText(),
required: !!param.questionToken,
schema: {
type,
},
};
})
.filter((i): i is ParamsOpenRPC => !!i);
});
}
export function workWithFile(file: string) {
const code = readFileSync(file, 'utf-8');
const sc = ts.createSourceFile('x.ts', code, ts.ScriptTarget.Latest, true);
const classList = getDeclaration(
sc,
SyntaxKind.ClassDeclaration,
false
).filter((item) => {
const decoratorForClass = getDeclaration(item, SyntaxKind.Decorator, true);
if (!decoratorForClass) return false;
const callExpression = getDeclaration(
decoratorForClass,
SyntaxKind.CallExpression,
true
);
if (!callExpression) return false;
const identifier = getName(callExpression);
if (!identifier) return false;
return identifier === DECORATOR_NAME;
});
const methods = classList.reduce<string[]>((acum, item) => {
const methods = getMethodForClass(item)
.map((i) => getName(i))
.filter((i): i is string => !!i);
const params = getParams(item);
console.log(JSON.stringify(params));
return acum;
}, []);
}
workWithFile('apps/json-api-server/src/app/rpc/service/rpc.service.ts');