Skip to content
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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

feat: Add Angular Wizard #741

wants to merge 2 commits into from

Conversation

onurtemizkan
Copy link
Collaborator

@onurtemizkan onurtemizkan commented Dec 12, 2024

Resolves: #672

Adds wizard for Angular projects

Notes:

  • Does not support projects using NGModules, as they are not enabled by default anymore
  • Uses the internal sourcemaps wizard for Sentry CLI configuration
  • Does not create example pages, we can add that in a follow-up PR.

@onurtemizkan onurtemizkan requested a review from Lms24 December 12, 2024 19:26
Copy link

github-actions bot commented Dec 12, 2024

Messages
📖 Do not forget to update Sentry-docs with your feature once the pull request gets approved.

Generated by 🚫 dangerJS against e872236

@onurtemizkan onurtemizkan force-pushed the onur/angular-wizard branch 6 times, most recently from 8fc1561 to 542ea8a Compare December 13, 2024 12:12
@Lms24 Lms24 requested a review from andreiborza December 16, 2024 14:26
CHANGELOG.md Outdated
@@ -2,6 +2,7 @@

## Unreleased

- feat: Add Angular Wizard ([#741](https://github.com/getsentry/sentry-wizard/pull/741))
Copy link
Member

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.

options: WizardOptions,
): Promise<void> {
printWelcome({
wizardName: 'Sentry Remix Wizard',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
wizardName: 'Sentry Remix Wizard',
wizardName: 'Sentry Angular Wizard',

await installPackage({
packageName: '@sentry/angular@^8',
packageNameDisplayLabel: '@sentry/angular',
alreadyInstalled: hasPackageInstalled('@sentry/angular', packageJson),
Copy link
Member

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);

Comment on lines 30 to 31
item.imported === '*' &&
item.local === 'Sentry',
Copy link
Member

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.

): void {
const imports = originalAppConfigMod.imports;
const hasErrorHandler = imports.$items.some(
(item) => item.local === 'ErrorHandler',
Copy link
Member

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.

const imports = originalAppConfigMod.imports;

const hasProvideAppInitializer = imports.$items.some(
(item) => item.local === 'provideAppInitializer',
Copy link
Member

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.

});
}

const hasInject = imports.$items.some((item) => item.local === 'inject');
Copy link
Member

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?

const imports = originalAppConfigMod.imports;

const hasAppInitializer = imports.$items.some(
(item) => item.local === 'APP_INITIALIZER',
Copy link
Member

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.

Comment on lines 46 to 58
const originalAppModule = await loadFile(appModulePath);

if (hasSentryContent(appModulePath, originalAppModule.$code)) {
return;
}

const updatedAppModuleMod = updateAppModuleMod(
originalAppModule,
dsn,
selectedFeatures,
);

await writeFile(updatedAppModuleMod.$ast, appModulePath);
Copy link
Member

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.

Comment on lines +95 to +97
clack.log.error(
`Failed to update your app config ${chalk.cyan(appConfigFilename)}`,
);
Copy link
Member

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?

Copy link
Member

@Lms24 Lms24 left a 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

Comment on lines 57 to 77
if (!installedAngularVersion) {
clack.log.warn('Could not determine installed Angular version.');

return;
}
Copy link
Member

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?

Comment on lines 65 to 69
if (!installedMinVersion) {
clack.log.warn('Could not determine minimum Angular version.');

return;
}
Copy link
Member

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

Comment on lines +77 to +89
clack.log.warn(
`Angular version ${MIN_SUPPORTED_ANGULAR_VERSION} or higher is required.`,
);
Copy link
Member

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.

Comment on lines 112 to 114
await traceStep('Inject Sentry to Angular app config', async () => {
await initalizeSentryOnAppModule(dsn, selectedFeatures);
});
Copy link
Member

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.

});

await traceStep('Setup for sourcemap uploads', async () => {
addSourcemapEntryToAngularJSON();
Copy link
Member

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');
Copy link
Member

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');
});

Copy link
Member

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.

Comment on lines 13 to 17
if (!angularJson) {
throw new Error('Could not find in angular.json in your project');
}
Copy link
Member

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:

  1. we link to docs
  2. we show the snippet that we show in the source maps wizard if there's no pre-selected project

@getsentry getsentry deleted a comment from andreiborza Dec 17, 2024
@getsentry getsentry deleted a comment from andreiborza Dec 17, 2024
Comment on lines +153 to +169
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,
);
Copy link
Member

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/

@Lms24
Copy link
Member

Lms24 commented Dec 17, 2024

Just FYI:
image

@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 :)

message: 'Please enter the installed Angular version:',
validate(value) {
if (!value) {
return 'Please enter the installed Angular version.';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

m: As a user, I'd wonder what kind of answer format you want here. Could you add en example here, for example: Please enter your installed Angular version (e.g. chalk.cyan(18.4.5))

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The major is enough though, right? Finding the specific minor/patch might be tricky

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, if we're fine with major that's ok too but I think the point still stands that I'd want some guideline on what kind of answer you expect from me as a user. So we could just say (e.g. chalk.cyan(18) for version 18)? Something like that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup, sounds good!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add Wizard for Angular
3 participants