-
-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathapp-config.ts
More file actions
291 lines (254 loc) · 8.87 KB
/
app-config.ts
File metadata and controls
291 lines (254 loc) · 8.87 KB
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
import type { ObjectProperty } from '@babel/types';
// @ts-expect-error - magicast is ESM and TS complains about that. It works though
import type { ProxifiedModule } from 'magicast';
// @ts-expect-error - clack is ESM and TS complains about that. It works though
import * as clack from '@clack/prompts';
import { gte, type SemVer } from 'semver';
import * as recast from 'recast';
import pc from 'picocolors';
export function updateAppConfigMod(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
originalAppConfigMod: ProxifiedModule<any>,
angularVersion: SemVer,
isTracingEnabled: boolean,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
): ProxifiedModule<any> {
const isAboveAngularV19 = gte(angularVersion, '19.0.0');
addImports(originalAppConfigMod, isAboveAngularV19, isTracingEnabled);
addProviders(originalAppConfigMod, isAboveAngularV19, isTracingEnabled);
return originalAppConfigMod;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function addSentryImport(originalAppConfigMod: ProxifiedModule<any>): void {
const imports = originalAppConfigMod.imports;
const hasSentryImport = imports.$items.some(
(item) => item.from === '@sentry/angular',
);
if (!hasSentryImport) {
imports.$add({
from: '@sentry/angular',
imported: '*',
local: 'Sentry',
});
}
}
function addErrorHandlerImport(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
originalAppConfigMod: ProxifiedModule<any>,
): void {
const imports = originalAppConfigMod.imports;
const hasErrorHandler = imports.$items.some(
(item) => item.local === 'ErrorHandler' && item.from === '@angular/core',
);
if (!hasErrorHandler) {
imports.$add({
from: '@angular/core',
imported: 'ErrorHandler',
local: 'ErrorHandler',
});
}
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function addRouterImport(originalAppConfigMod: ProxifiedModule<any>): void {
const imports = originalAppConfigMod.imports;
const hasRouter = imports.$items.some(
(item) => item.local === 'Router' && item.from === '@angular/router',
);
if (!hasRouter) {
imports.$add({
from: '@angular/router',
imported: 'Router',
local: 'Router',
});
}
}
function addMissingImportsV19(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
originalAppConfigMod: ProxifiedModule<any>,
): void {
const imports = originalAppConfigMod.imports;
const hasProvideAppInitializer = imports.$items.some(
(item) =>
item.local === 'provideAppInitializer' && item.from === '@angular/core',
);
if (!hasProvideAppInitializer) {
imports.$add({
from: '@angular/core',
imported: 'provideAppInitializer',
local: 'provideAppInitializer',
});
}
const hasInject = imports.$items.some(
(item) => item.local === 'inject' && item.from === '@angular/core',
);
if (!hasInject) {
imports.$add({
from: '@angular/core',
imported: 'inject',
local: 'inject',
});
}
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function addAppInitializer(originalAppConfigMod: ProxifiedModule<any>): void {
const imports = originalAppConfigMod.imports;
const hasAppInitializer = imports.$items.some(
(item) => item.local === 'APP_INITIALIZER' && item.from === '@angular/core',
);
if (!hasAppInitializer) {
imports.$add({
from: '@angular/core',
imported: 'APP_INITIALIZER',
local: 'APP_INITIALIZER',
});
}
}
function addImports(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
originalAppConfigMod: ProxifiedModule<any>,
isAboveAngularV19: boolean,
isTracingEnabled: boolean,
): void {
addSentryImport(originalAppConfigMod);
addErrorHandlerImport(originalAppConfigMod);
if (isTracingEnabled) {
addRouterImport(originalAppConfigMod);
}
if (isAboveAngularV19) {
addMissingImportsV19(originalAppConfigMod);
} else if (isTracingEnabled) {
addAppInitializer(originalAppConfigMod);
}
}
function addProviders(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
originalAppConfigMod: ProxifiedModule<any>,
isAboveAngularV19: boolean,
isTracingEnabled: boolean,
): void {
const b = recast.types.builders;
recast.visit(originalAppConfigMod.exports.$ast, {
visitExportNamedDeclaration(path) {
if (
path.node.declaration?.type !== 'VariableDeclaration' ||
path.node.declaration.declarations[0]?.type !== 'VariableDeclarator' ||
path.node.declaration.declarations[0].id.type !== 'Identifier' ||
path.node.declaration.declarations[0].id.name !== 'appConfig' ||
path.node.declaration.declarations[0].init?.type !==
'ObjectExpression' ||
!path.node.declaration.declarations[0].init.properties
) {
return;
}
const appConfigProps =
path.node.declaration.declarations[0].init.properties;
const providersProperty = appConfigProps.find(
(prop) =>
prop?.type === 'ObjectProperty' &&
prop.key.type === 'Identifier' &&
prop.key.name === 'providers',
// type cast is safe because we already check the type in the find condition
) as ObjectProperty | undefined;
const validProviders =
providersProperty?.value?.type === 'ArrayExpression'
? providersProperty.value
: undefined;
if (!validProviders) {
return;
}
// Check if there is already an ErrorHandler provider
const hasErrorHandlerProvider = validProviders?.elements.some(
(element) =>
element &&
element.type === 'ObjectExpression' &&
element.properties.some(
(prop) =>
prop.type === 'ObjectProperty' &&
prop.key.type === 'Identifier' &&
prop.key.name === 'provide' &&
prop.value.type === 'Identifier' &&
prop.value.name === 'ErrorHandler',
),
);
// If there is already an ErrorHandler provider, we skip adding it and log a message
if (hasErrorHandlerProvider) {
clack.log.warn(`ErrorHandler provider already exists in your app config.
Please refer to the Sentry Angular SDK documentation to combine it manually with Sentry's ErrorHandler.
${pc.underline(
'https://docs.sentry.io/platforms/javascript/guides/angular/features/error-handler/',
)}
`);
} else {
const errorHandlerObject = b.objectExpression([
b.objectProperty(
b.identifier('provide'),
b.identifier('ErrorHandler'),
),
b.objectProperty(
b.identifier('useValue'),
b.identifier('Sentry.createErrorHandler()'),
),
]);
validProviders.elements.push(
// @ts-expect-error - errorHandlerObject is an objectExpression
errorHandlerObject,
);
}
if (isTracingEnabled) {
const traceServiceObject = b.objectExpression([
b.objectProperty(
b.identifier('provide'),
b.identifier('Sentry.TraceService'),
),
b.objectProperty(
b.identifier('deps'),
b.arrayExpression([b.identifier('Router')]),
),
]);
// @ts-expect-error - errorHandlerObject is an objectExpression
validProviders.elements.push(traceServiceObject);
if (isAboveAngularV19) {
const provideAppInitializerCall = b.callExpression(
b.identifier('provideAppInitializer'),
[
b.arrowFunctionExpression(
[],
b.blockStatement([
b.expressionStatement(
b.callExpression(b.identifier('inject'), [
b.identifier('Sentry.TraceService'),
]),
),
]),
),
],
);
// @ts-expect-error - provideAppInitializerCall is an objectExpression
validProviders.elements.push(provideAppInitializerCall);
} else {
const provideAppInitializerObject = b.objectExpression([
b.objectProperty(
b.identifier('provide'),
b.identifier('APP_INITIALIZER'),
),
b.objectProperty(
b.identifier('useFactory'),
b.arrowFunctionExpression(
[],
b.arrowFunctionExpression([], b.blockStatement([])),
),
),
b.objectProperty(
b.identifier('deps'),
b.arrayExpression([b.identifier('Sentry.TraceService')]),
),
b.objectProperty(b.identifier('multi'), b.booleanLiteral(true)),
]);
// @ts-expect-error - provideAppInitializerObject is an objectExpression
validProviders.elements.push(provideAppInitializerObject);
}
}
this.traverse(path);
},
});
}