-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
ast-utils.ts
137 lines (122 loc) · 3.28 KB
/
ast-utils.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
import * as ts from 'typescript';
import { ChangeType, StringChange } from '@nrwl/devkit';
import { findNodes } from 'nx/src/utils/typescript';
import {
addImport,
findClosestOpening,
findElements,
} from '../utils/ast-utils';
export function addRemoteToConfig(
source: ts.SourceFile,
app: string
): StringChange[] {
const assignments = findNodes(
source,
ts.SyntaxKind.PropertyAssignment
) as ts.PropertyAssignment[];
const remotesAssignment = assignments.find(
(s) => s.name.getText() === 'remotes'
);
if (remotesAssignment) {
const arrayExpression =
remotesAssignment.initializer as ts.ArrayLiteralExpression;
if (!arrayExpression.elements) return [];
const lastElement =
arrayExpression.elements[arrayExpression.elements.length - 1];
return [
lastElement
? {
type: ChangeType.Insert,
index: lastElement.end,
text: `,`,
}
: null,
{
type: ChangeType.Insert,
index: remotesAssignment.end - 1,
text: `'${app}',\n`,
},
].filter(Boolean) as StringChange[];
}
const binaryExpressions = findNodes(
source,
ts.SyntaxKind.BinaryExpression
) as ts.BinaryExpression[];
const exportExpression = binaryExpressions.find((b) => {
if (b.left.kind === ts.SyntaxKind.PropertyAccessExpression) {
const pae = b.left as ts.PropertyAccessExpression;
return (
pae.expression.getText() === 'module' &&
pae.name.getText() === 'exports'
);
}
});
if (exportExpression?.right.kind === ts.SyntaxKind.ObjectLiteralExpression) {
const ole = exportExpression.right as ts.ObjectLiteralExpression;
return [
{
type: ChangeType.Insert,
index: ole.end - 1,
text: `remotes: ['${app}']\n`,
},
];
}
return [];
}
export function addRemoteDefinition(
source: ts.SourceFile,
app: string
): StringChange[] {
return [
{
type: ChangeType.Insert,
index: source.end,
text: `\ndeclare module '${app}/Module';`,
},
];
}
export function addRemoteRoute(
source: ts.SourceFile,
names: {
fileName: string;
className: string;
}
): StringChange[] {
const routes = findElements(source, 'Route');
const links = findElements(source, 'Link');
if (routes.length === 0) {
return [];
} else {
const changes: StringChange[] = [];
const firstRoute = routes[0];
const firstLink = links[0];
changes.push(
...addImport(
source,
`const ${names.className} = React.lazy(() => import('${names.fileName}/Module'));`
)
);
changes.push({
type: ChangeType.Insert,
index: firstRoute.end,
text: `\n<Route path="/${names.fileName}" element={<${names.className} />} />`,
});
if (firstLink) {
const parentLi = findClosestOpening('li', firstLink);
if (parentLi) {
changes.push({
type: ChangeType.Insert,
index: parentLi.end,
text: `\n<li><Link to="/${names.fileName}">${names.className}</Link></li>`,
});
} else {
changes.push({
type: ChangeType.Insert,
index: firstLink.parent.end,
text: `\n<Link to="/${names.fileName}">${names.className}</Link>`,
});
}
}
return changes;
}
}