-
-
Notifications
You must be signed in to change notification settings - Fork 57
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(angular): Add Sentry setup in main.ts
#768
Merged
Lms24
merged 4 commits into
onur/angular-wizard-base
from
onur/angular-wizard-sentry-setup-main
Feb 3, 2025
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* eslint-disable @typescript-eslint/no-unsafe-assignment */ | ||
import type { Program } from '@babel/types'; | ||
|
||
// @ts-expect-error - magicast is ESM and TS complains about that. It works though | ||
import { builders, generateCode, type ProxifiedModule } from 'magicast'; | ||
|
||
export function updateAppEntryMod( | ||
originalAppModuleMod: ProxifiedModule<any>, | ||
dsn: string, | ||
selectedFeatures: { | ||
performance: boolean; | ||
replay: boolean; | ||
}, | ||
): ProxifiedModule<any> { | ||
originalAppModuleMod.imports.$add({ | ||
from: '@sentry/angular', | ||
imported: '*', | ||
local: 'Sentry', | ||
}); | ||
|
||
insertInitCall(originalAppModuleMod, dsn, selectedFeatures); | ||
|
||
return originalAppModuleMod; | ||
} | ||
|
||
export function insertInitCall( | ||
originalAppModuleMod: ProxifiedModule<any>, | ||
dsn: string, | ||
selectedFeatures: { | ||
performance: boolean; | ||
replay: boolean; | ||
}, | ||
): void { | ||
const initCallArgs = getInitCallArgs(dsn, selectedFeatures); | ||
const initCall = builders.functionCall('Sentry.init', initCallArgs); | ||
const originalAppModuleModAst = originalAppModuleMod.$ast as Program; | ||
|
||
const initCallInsertionIndex = getAfterImportsInsertionIndex( | ||
originalAppModuleModAst, | ||
); | ||
|
||
originalAppModuleModAst.body.splice( | ||
initCallInsertionIndex, | ||
0, | ||
// @ts-expect-error - string works here because the AST is proxified by magicast | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument | ||
generateCode(initCall).code, | ||
); | ||
} | ||
|
||
export function getInitCallArgs( | ||
dsn: string, | ||
selectedFeatures: { | ||
performance: boolean; | ||
replay: boolean; | ||
}, | ||
): Record<string, unknown> { | ||
const initCallArgs = { | ||
dsn, | ||
} as Record<string, unknown>; | ||
|
||
if (selectedFeatures.replay || selectedFeatures.performance) { | ||
initCallArgs.integrations = []; | ||
|
||
if (selectedFeatures.performance) { | ||
// @ts-expect-error - Adding Proxified AST node to the array | ||
initCallArgs.integrations.push( | ||
builders.functionCall('Sentry.browserTracingIntegration'), | ||
); | ||
initCallArgs.tracesSampleRate = 1.0; | ||
} | ||
|
||
if (selectedFeatures.replay) { | ||
// @ts-expect-error - Adding Proxified AST node to the array | ||
initCallArgs.integrations.push( | ||
builders.functionCall('Sentry.replayIntegration'), | ||
); | ||
|
||
initCallArgs.replaysSessionSampleRate = 0.1; | ||
initCallArgs.replaysOnErrorSampleRate = 1.0; | ||
} | ||
} | ||
|
||
return initCallArgs; | ||
} | ||
|
||
/** | ||
* We want to insert the handleError function just after all imports | ||
*/ | ||
export function getAfterImportsInsertionIndex( | ||
originalEntryServerModAST: Program, | ||
): number { | ||
for (let x = originalEntryServerModAST.body.length - 1; x >= 0; x--) { | ||
if (originalEntryServerModAST.body[x].type === 'ImportDeclaration') { | ||
return x + 1; | ||
} | ||
} | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* eslint-disable @typescript-eslint/no-unsafe-assignment */ | ||
|
||
// @ts-expect-error - magicast is ESM and TS complains about that. It works though | ||
import { loadFile, writeFile } from 'magicast'; | ||
|
||
import * as path from 'path'; | ||
|
||
// @ts-expect-error - clack is ESM and TS complains about that. It works though | ||
import clack from '@clack/prompts'; | ||
import chalk from 'chalk'; | ||
import { updateAppEntryMod } from './codemods/main'; | ||
|
||
export function hasSentryContent( | ||
fileName: string, | ||
fileContent: string, | ||
expectedContent = '@sentry/angular', | ||
): boolean { | ||
const includesContent = fileContent.includes(expectedContent); | ||
|
||
if (includesContent) { | ||
clack.log.warn( | ||
`File ${chalk.cyan( | ||
path.basename(fileName), | ||
)} already contains ${expectedContent}. | ||
Skipping adding Sentry functionality to ${chalk.cyan( | ||
path.basename(fileName), | ||
)}.`, | ||
); | ||
} | ||
|
||
return includesContent; | ||
} | ||
|
||
export async function initalizeSentryOnApplicationEntry( | ||
dsn: string, | ||
selectedFeatures: { | ||
performance: boolean; | ||
replay: boolean; | ||
}, | ||
): Promise<void> { | ||
const appEntryFilename = 'main.ts'; | ||
const appEntryPath = path.join(process.cwd(), 'src', appEntryFilename); | ||
|
||
const originalAppEntry = await loadFile(appEntryPath); | ||
|
||
if (hasSentryContent(appEntryPath, originalAppEntry.$code)) { | ||
return; | ||
} | ||
|
||
try { | ||
const updatedAppEntryMod = updateAppEntryMod( | ||
originalAppEntry, | ||
dsn, | ||
selectedFeatures, | ||
); | ||
|
||
await writeFile(updatedAppEntryMod.$ast, appEntryPath); | ||
} catch (error: unknown) { | ||
clack.log.error( | ||
`Error while adding Sentry to ${chalk.cyan(appEntryFilename)}`, | ||
); | ||
|
||
clack.log.info( | ||
chalk.dim( | ||
typeof error === 'object' && error != null && 'toString' in error | ||
? error.toString() | ||
: typeof error === 'string' | ||
? error | ||
: '', | ||
), | ||
); | ||
|
||
clack.log.warn( | ||
`Please refer to the documentation for manual setup: | ||
${chalk.underline( | ||
'https://docs.sentry.io/platforms/javascript/guides/angular/#configure', | ||
)}`, | ||
); | ||
|
||
return; | ||
} | ||
|
||
clack.log.success( | ||
`Successfully initialized Sentry on ${chalk.cyan(appEntryFilename)}`, | ||
); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 use the
hasSentryContent
function fromast-utils.ts
instead of this one here?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.
Updated 👍