-
-
Notifications
You must be signed in to change notification settings - Fork 55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Add Angular Wizard #741
base: master
Are you sure you want to change the base?
Conversation
|
8fc1561
to
542ea8a
Compare
CHANGELOG.md
Outdated
@@ -2,6 +2,7 @@ | |||
|
|||
## Unreleased | |||
|
|||
- feat: Add Angular Wizard ([#741](https://github.com/getsentry/sentry-wizard/pull/741)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
l: could you move this down please? these are ordered by ticket number.
src/angular/angular-wizard.ts
Outdated
options: WizardOptions, | ||
): Promise<void> { | ||
printWelcome({ | ||
wizardName: 'Sentry Remix Wizard', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wizardName: 'Sentry Remix Wizard', | |
wizardName: 'Sentry Angular Wizard', |
src/angular/angular-wizard.ts
Outdated
await installPackage({ | ||
packageName: '@sentry/angular@^8', | ||
packageNameDisplayLabel: '@sentry/angular', | ||
alreadyInstalled: hasPackageInstalled('@sentry/angular', packageJson), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
m: let's extract the alreadyInstalled
flag and log it:
Sentry.setTag('sdk-already-installed', sdkAlreadyInstalled);
src/angular/codemods/app-config.ts
Outdated
item.imported === '*' && | ||
item.local === 'Sentry', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
l: Should be enough to just check the from
here.
src/angular/codemods/app-config.ts
Outdated
): void { | ||
const imports = originalAppConfigMod.imports; | ||
const hasErrorHandler = imports.$items.some( | ||
(item) => item.local === 'ErrorHandler', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
m: Can we narrow down here and add && item.from: '@angular/core'
? I think this might be too wide.
src/angular/codemods/app-config.ts
Outdated
const imports = originalAppConfigMod.imports; | ||
|
||
const hasProvideAppInitializer = imports.$items.some( | ||
(item) => item.local === 'provideAppInitializer', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
l: I think we could also narrow down here a bit, although the function is already very specific.
src/angular/codemods/app-config.ts
Outdated
}); | ||
} | ||
|
||
const hasInject = imports.$items.some((item) => item.local === 'inject'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
m: and here, can we narrow down?
src/angular/codemods/app-config.ts
Outdated
const imports = originalAppConfigMod.imports; | ||
|
||
const hasAppInitializer = imports.$items.some( | ||
(item) => item.local === 'APP_INITIALIZER', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
l: could also narrow here, although might be specific enough already.
src/angular/sdk-setup.ts
Outdated
const originalAppModule = await loadFile(appModulePath); | ||
|
||
if (hasSentryContent(appModulePath, originalAppModule.$code)) { | ||
return; | ||
} | ||
|
||
const updatedAppModuleMod = updateAppModuleMod( | ||
originalAppModule, | ||
dsn, | ||
selectedFeatures, | ||
); | ||
|
||
await writeFile(updatedAppModuleMod.$ast, appModulePath); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
m: I think we need a try/catch around this and gracefully abort the wizard. A single typo in user's config would crash this.
src/angular/sdk-setup.ts
Outdated
clack.log.error( | ||
`Failed to update your app config ${chalk.cyan(appConfigFilename)}`, | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
l/m: In the nuxt wizard, I specifically checked for MagicastError
here and logged out what users need to do to their config to make it work. Maybe something to consider here too if possible?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks Onur! I left some comments that we should address. One thing we still need to handle is to at least abort gracefully if we detect an NGModule-based project. Right now, I don't think the wizard would handle this very well. What we can do is to heuristically search for an app.module.ts
file. The file name is just a convention though (users could name this file however they like) but probably a good enough one.
My gut feeling tells me that a big part (if not the majority) of Angular users will still use NGModules so we should think about finding a way to support these as well. We can take care of this in a follow-up PR though. For now, let's focus on printing good fallback instructions.
Also, let's add a if we encounter a module- or app-config-based setup so that we can gather some data if it's worth investing time into handling NGModules better.
The general path through the wizard should be to always show fallback instructions (link to docs, in-wizard code snippets) of how users can manually do what the wizard fails to do.
So from my PoV let's do the following in this PR:
- address all review comments
For us to follow up in future PRs (who does it TBD):
- example page
- handle NGModules if data agrees with my gut feeling
src/angular/angular-wizard.ts
Outdated
if (!installedAngularVersion) { | ||
clack.log.warn('Could not determine installed Angular version.'); | ||
|
||
return; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
m: Given that this might be our fault rather than a user-misconfiguration or similar, can we fall back to asking users for the version?
src/angular/angular-wizard.ts
Outdated
if (!installedMinVersion) { | ||
clack.log.warn('Could not determine minimum Angular version.'); | ||
|
||
return; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given the above, we shouldn't fall into this case, so we might as well remove it
clack.log.warn( | ||
`Angular version ${MIN_SUPPORTED_ANGULAR_VERSION} or higher is required.`, | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
l: Let's print the link to our version compatibility table in the docs (https://docs.sentry.io/platforms/javascript/guides/angular/#angular-version-compatibility) and tell them to find the suitable SDK version instead.
src/angular/angular-wizard.ts
Outdated
await traceStep('Inject Sentry to Angular app config', async () => { | ||
await initalizeSentryOnAppModule(dsn, selectedFeatures); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think what we're doing here is not related to the "app config" or "app module" at all, but we rather init Sentry in main.ts
right? I'd rather call the the step and function something like app entry point or so. The "app module" makes this sound like it modifies app.module.ts
.
src/angular/angular-wizard.ts
Outdated
}); | ||
|
||
await traceStep('Setup for sourcemap uploads', async () => { | ||
addSourcemapEntryToAngularJSON(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
m: Let's log a message that we modified angular.json
. I completely missed this when running the wizard (also related to my comment below).
options.url = sentryUrl; | ||
} | ||
|
||
await runSourcemapsWizard(options, 'angular'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
l: Generally, running the source maps wizard here seems fine to me. I found the integration quite seamless. However, in the source maps wizard, there's a step where I need to verify that source maps are created. I think, given that within the wizard run, already enabled source maps emission, we might as well just skip this step. This probably requires some kind of new option in the source maps wizard. Feel free to add that as you see fit.
|
||
await runSourcemapsWizard(options, 'angular'); | ||
}); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
m: Before we print the outro, let's call runPrettierIfInstalled()
to optionally reformat the project files.
src/angular/codemods/sourcemaps.ts
Outdated
if (!angularJson) { | ||
throw new Error('Could not find in angular.json in your project'); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
l: If we end up here, let's provide users a fallback, which in this case is to manually enable source maps upload. 2 ideas:
- we link to docs
- we show the snippet that we show in the source maps wizard if there's no pre-selected project
src/angular/codemods/app-config.ts
Outdated
const errorHandlerObject = b.objectExpression([ | ||
b.objectProperty( | ||
b.identifier('provide'), | ||
b.identifier('ErrorHandler'), | ||
), | ||
b.objectProperty( | ||
b.identifier('useValue'), | ||
b.identifier('Sentry.createErrorHandler()'), | ||
), | ||
]); | ||
|
||
providers.elements.push( | ||
// @ts-expect-error - errorHandlerObject is an objectExpression | ||
errorHandlerObject, | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
m: Angular only permits one ErrorHandler per application. We need to check if users already declared one and if so, let's not add an additional one but link to our docs how to combine their handler with ours: https://docs.sentry.io/platforms/javascript/guides/angular/features/error-handler/
@andreiborza and I commented on the same line and discussed offline with which suggestion to go with. I just removed the comments to avoid confusion :) |
93acb01
to
96b4a25
Compare
96b4a25
to
e872236
Compare
7c3d91f
to
e872236
Compare
16689ac
to
e32a2d4
Compare
4e0e43b
to
cbb467f
Compare
Thanks for the reviews, @andreiborza, @Lms24! |
this.taskHandle.stdout.pipe(process.stdout); | ||
this.taskHandle.stderr.pipe(process.stderr); | ||
} | ||
// if (opts?.debug) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
l: Is this intentional? We should just remove the if
if we don't use it anymore.
|
||
if (!isSupportedAngularVersion) { | ||
clack.log.warn( | ||
`Angular version ${MIN_SUPPORTED_ANGULAR_VERSION} or higher is required.`, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
`Angular version ${MIN_SUPPORTED_ANGULAR_VERSION} or higher is required.`, | |
`Angular version ${chalk.cyan(MIN_SUPPORTED_ANGULAR_VERSION)} or higher is required.`, |
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. | ||
https://docs.sentry.io/platforms/javascript/guides/angular/features/error-handler/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
l: Let's underline the docs link like the other links.
try { | ||
fs.writeFileSync(angularJsonPath, JSON.stringify(angularJson, null, 2)); | ||
} catch (error) { | ||
clack.log.error(`Failed to write sourcemap configuration to angular.json`); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
m: What should users do in this case? Can we show them a pasteable snippet of what they could do instead?
const updatedAppEntryMod = updateAppEntryMod( | ||
originalAppEntry, | ||
dsn, | ||
selectedFeatures, | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
m: Could this not already fail? How about moving that down into the try
?
try { | ||
await writeFile(updatedAppEntryMod.$ast, appEntryPath); | ||
} catch (error: unknown) { | ||
if (error instanceof MagicastError) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
m: In the nuxt wizard I went back on specifically checking for MagicastErrors because really any error warrants instructing users how to possibly get around it. See https://github.com/getsentry/sentry-wizard/blob/master/src/nuxt/sdk-setup.ts#L122-L149.
), | ||
); | ||
|
||
await abort('Exiting Wizard'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
m: I think we don't want to abort here if we instruct users on what to do. cc @Lms24
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think we usually just continue, yes
if (error instanceof MagicastError) { | ||
clack.log.warn( | ||
`Failed to update your app config ${chalk.cyan( | ||
appConfigFilename, | ||
)} automatically. | ||
Please refer to the documentation for manual setup | ||
https://docs.sentry.io/platforms/javascript/guides/angular/#configure`, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
m: Same comment as above, I think we should refrain from checking the error type here and always log out alternatives.
), | ||
); | ||
|
||
await abort('Exiting Wizard'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
m: Not sure if we want to abort here either.
Thanks @onurtemizkan, I added my new review. Mostly LGTM, just some clarifications. |
), | ||
); | ||
|
||
await abort('Exiting Wizard'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think we usually just continue, yes
// Angular wizard handles the angular.json setup itself | ||
!preSelectedTool || preSelectedTool !== 'angular' | ||
? configureAngularSourcemapGenerationFlow | ||
: // Not leaving this as undefined to avoid default. This is expected to be a no-op. | ||
async () => Promise.resolve(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see there's a new paramter (skipValidation
). Should we use it here instead of creating the noop function?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@onurtemizkan sorry for the late notice but would you mind splitting up this PR a bit? It's getting quite large, especially with the many AST modifications we need in comparison to other wizards. I added some suggestions for steps into #672 but feel free to go with reasonable steps as you see fit.
@andreiborza did this a while ago with the Nuxt wizard where he collected the progress in one branch and made smaller PRs against the branch.
Btw, I think the groundwork is done well in this PR, so no need to rewrite things. Individual PRs just make it easier to review. Thanks!
Resolves: #672
Adds wizard for Angular projects
Notes: