-
-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathangular-wizard.ts
More file actions
262 lines (216 loc) · 7.44 KB
/
angular-wizard.ts
File metadata and controls
262 lines (216 loc) · 7.44 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
// @ts-expect-error - clack is ESM and TS complains about that. It works though
import clack from '@clack/prompts';
import pc from 'picocolors';
import type { WizardOptions } from '../utils/types';
import { traceStep, withTelemetry } from '../telemetry';
import {
abortIfCancelled,
askShouldCreateExampleComponent,
confirmContinueIfNoOrDirtyGitRepo,
ensurePackageIsInstalled,
featureSelectionPrompt,
getOrAskForProjectData,
getPackageDotJson,
installPackage,
printWelcome,
runPrettierIfInstalled,
abort,
} from '../utils/clack';
import { getPackageVersion, hasPackageInstalled } from '../utils/package-json';
import { gte, minVersion, SemVer } from 'semver';
import * as Sentry from '@sentry/node';
import { initializeSentryOnApplicationEntry } from './sdk-setup';
import { updateAppConfig } from './sdk-setup';
import { runSourcemapsWizard } from '../sourcemaps/sourcemaps-wizard';
import { addSourcemapEntryToAngularJSON } from './codemods/sourcemaps';
import { createExampleComponent } from './example-component';
const MIN_SUPPORTED_ANGULAR_VERSION = '14.0.0';
const MIN_SUPPORTED_WIZARD_ANGULAR_VERSION = '17.0.0';
export async function runAngularWizard(options: WizardOptions): Promise<void> {
return withTelemetry(
{
enabled: options.telemetryEnabled,
integration: 'angular',
wizardOptions: options,
},
() => runAngularWizardWithTelemetry(options),
);
}
async function runAngularWizardWithTelemetry(
options: WizardOptions,
): Promise<void> {
printWelcome({
wizardName: 'Sentry Angular Wizard',
promoCode: options.promoCode,
telemetryEnabled: options.telemetryEnabled,
});
await confirmContinueIfNoOrDirtyGitRepo({
ignoreGitChanges: options.ignoreGitChanges,
cwd: undefined,
});
const packageJson = await getPackageDotJson();
await ensurePackageIsInstalled(packageJson, '@angular/core', 'Angular');
let installedAngularVersion = getPackageVersion('@angular/core', packageJson);
if (!installedAngularVersion) {
clack.log.warn('Could not determine installed Angular version.');
installedAngularVersion = await abortIfCancelled(
clack.text({
message: `Please enter your installed Angular major version (e.g. ${pc.cyan(
'18',
)} for Angular 18)`,
validate(value) {
if (!value) {
return 'Angular version is required';
}
try {
if (!minVersion(value)) {
return `Invalid Angular version provided: ${value}`;
}
} catch (error) {
return `Invalid Angular version provided: ${value}`;
}
},
}),
);
}
Sentry.setTag('angular-version', installedAngularVersion);
const installedMinVersion = minVersion(installedAngularVersion) as SemVer;
const sdkSupportsAngularVersion = gte(
installedMinVersion,
MIN_SUPPORTED_ANGULAR_VERSION,
);
const wizardSupportsAngularVersion = gte(
installedMinVersion,
MIN_SUPPORTED_WIZARD_ANGULAR_VERSION,
);
if (!sdkSupportsAngularVersion) {
Sentry.setTag('angular-version-compatible', false);
clack.log.warn(
`Angular version ${pc.cyan(
MIN_SUPPORTED_ANGULAR_VERSION,
)} or higher is required for the Sentry SDK.`,
);
clack.log.warn(
`Please refer to Sentry's version compatibility table for more information:
${pc.underline(
'https://docs.sentry.io/platforms/javascript/guides/angular/#angular-version-compatibility',
)}
`,
);
return abort('Exiting the wizard.', 0);
}
if (!wizardSupportsAngularVersion) {
Sentry.setTag('angular-wizard-version-compatible', false);
clack.log.warn(
`The Sentry Angular Wizard requires Angular version ${pc.cyan(
MIN_SUPPORTED_WIZARD_ANGULAR_VERSION,
)} or higher.`,
);
clack.log.warn(
`Your Angular version (${installedAngularVersion}) is compatible with the Sentry SDK but you need to set it up manually by following our documentation:
${pc.underline('https://docs.sentry.io/platforms/javascript/guides/angular')}
Apologies for the inconvenience!`,
);
return abort('Exiting the wizard.', 0);
}
const { selectedProject, authToken, sentryUrl, selfHosted } =
await getOrAskForProjectData(options, 'javascript-angular');
const dsn = selectedProject.keys[0].dsn.public;
const sdkAlreadyInstalled = hasPackageInstalled(
'@sentry/angular',
packageJson,
);
Sentry.setTag('sdk-already-installed', sdkAlreadyInstalled);
await installPackage({
packageName: '@sentry/angular@^10',
packageNameDisplayLabel: '@sentry/angular',
alreadyInstalled: sdkAlreadyInstalled,
});
const selectedFeatures = await featureSelectionPrompt([
{
id: 'performance',
prompt: `Do you want to enable ${pc.bold(
'Tracing',
)} to track the performance of your application?`,
enabledHint: 'recommended',
},
{
id: 'replay',
prompt: `Do you want to enable ${pc.bold(
'Sentry Session Replay',
)} to get a video-like reproduction of errors during a user session?`,
enabledHint: 'recommended, but increases bundle size',
},
{
id: 'logs',
prompt: `Do you want to enable ${pc.bold(
'Logs',
)} to send your application logs to Sentry?`,
enabledHint: 'recommended',
},
] as const);
await traceStep(
'Initialize Sentry on Angular application entry point',
async () => {
await initializeSentryOnApplicationEntry(dsn, selectedFeatures);
},
);
await traceStep('Update Angular project configuration', async () => {
await updateAppConfig(installedMinVersion, selectedFeatures.performance);
});
await traceStep('Setup for sourcemap uploads', async () => {
await addSourcemapEntryToAngularJSON();
if (!options.preSelectedProject) {
options.preSelectedProject = {
authToken,
selfHosted,
project: {
organization: {
id: selectedProject.organization.id,
name: selectedProject.organization.name,
slug: selectedProject.organization.slug,
},
id: selectedProject.id,
slug: selectedProject.slug,
keys: [
{
dsn: {
public: dsn,
},
},
],
},
};
options.url = sentryUrl;
}
await runSourcemapsWizard(options, 'angular');
});
const shouldCreateExampleComponent = await askShouldCreateExampleComponent();
Sentry.setTag('create-example-component', shouldCreateExampleComponent);
if (shouldCreateExampleComponent) {
await traceStep(
'create-example-component',
async () =>
await createExampleComponent({
url: sentryUrl,
orgSlug: selectedProject.organization.slug,
projectId: selectedProject.id,
}),
);
}
await traceStep('Run Prettier', async () => {
await runPrettierIfInstalled({ cwd: undefined });
});
clack.outro(buildOutroMessage(shouldCreateExampleComponent));
}
export function buildOutroMessage(createdExampleComponent: boolean): string {
let msg = pc.green('\nSuccessfully installed the Sentry Angular SDK!');
if (createdExampleComponent) {
msg += `\n\nYou can validate your setup by starting your dev environment (${pc.cyan(
'ng serve',
)}) and throwing an error in the example component.`;
}
msg += `\n\nCheck out the SDK documentation for further configuration:
https://docs.sentry.io/platforms/javascript/guides/angular/`;
return msg;
}