@sentry/wizard 3.10.0 → 3.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (131) hide show
  1. package/CHANGELOG.md +47 -7
  2. package/dist/lib/Constants.d.ts +1 -0
  3. package/dist/lib/Constants.js +5 -0
  4. package/dist/lib/Constants.js.map +1 -1
  5. package/dist/lib/Steps/ChooseIntegration.js +8 -4
  6. package/dist/lib/Steps/ChooseIntegration.js.map +1 -1
  7. package/dist/lib/Steps/Integrations/Android.d.ts +9 -0
  8. package/dist/lib/Steps/Integrations/Android.js +86 -0
  9. package/dist/lib/Steps/Integrations/Android.js.map +1 -0
  10. package/dist/lib/Steps/Integrations/ReactNative.js +3 -3
  11. package/dist/lib/Steps/Integrations/ReactNative.js.map +1 -1
  12. package/dist/lib/Steps/PromptForParameters.js +36 -3
  13. package/dist/lib/Steps/PromptForParameters.js.map +1 -1
  14. package/dist/lib/Steps/SentryProjectSelector.js +1 -1
  15. package/dist/lib/Steps/SentryProjectSelector.js.map +1 -1
  16. package/dist/package.json +4 -3
  17. package/dist/src/android/android-wizard.d.ts +2 -0
  18. package/dist/src/android/android-wizard.js +217 -0
  19. package/dist/src/android/android-wizard.js.map +1 -0
  20. package/dist/src/android/code-tools.d.ts +39 -0
  21. package/dist/src/android/code-tools.js +161 -0
  22. package/dist/src/android/code-tools.js.map +1 -0
  23. package/dist/src/android/gradle.d.ts +62 -0
  24. package/dist/src/android/gradle.js +281 -0
  25. package/dist/src/android/gradle.js.map +1 -0
  26. package/dist/src/android/manifest.d.ts +57 -0
  27. package/dist/src/android/manifest.js +183 -0
  28. package/dist/src/android/manifest.js.map +1 -0
  29. package/dist/src/android/templates.d.ts +11 -0
  30. package/dist/src/android/templates.js +34 -0
  31. package/dist/src/android/templates.js.map +1 -0
  32. package/dist/src/apple/apple-wizard.js +123 -64
  33. package/dist/src/apple/apple-wizard.js.map +1 -1
  34. package/dist/src/apple/cocoapod.js +4 -3
  35. package/dist/src/apple/cocoapod.js.map +1 -1
  36. package/dist/src/apple/code-tools.d.ts +1 -1
  37. package/dist/src/apple/code-tools.js +43 -19
  38. package/dist/src/apple/code-tools.js.map +1 -1
  39. package/dist/src/apple/fastlane.d.ts +1 -1
  40. package/dist/src/apple/fastlane.js +12 -6
  41. package/dist/src/apple/fastlane.js.map +1 -1
  42. package/dist/src/apple/templates.d.ts +2 -2
  43. package/dist/src/apple/templates.js +4 -4
  44. package/dist/src/apple/templates.js.map +1 -1
  45. package/dist/src/apple/xcode-manager.d.ts +19 -3
  46. package/dist/src/apple/xcode-manager.js +126 -24
  47. package/dist/src/apple/xcode-manager.js.map +1 -1
  48. package/dist/src/nextjs/nextjs-wizard.js +49 -11
  49. package/dist/src/nextjs/nextjs-wizard.js.map +1 -1
  50. package/dist/src/nextjs/templates.d.ts +2 -0
  51. package/dist/src/nextjs/templates.js +6 -2
  52. package/dist/src/nextjs/templates.js.map +1 -1
  53. package/dist/src/remix/remix-wizard.js +10 -20
  54. package/dist/src/remix/remix-wizard.js.map +1 -1
  55. package/dist/src/sourcemaps/sourcemaps-wizard.js +26 -13
  56. package/dist/src/sourcemaps/sourcemaps-wizard.js.map +1 -1
  57. package/dist/src/sourcemaps/tools/nextjs.js +1 -1
  58. package/dist/src/sourcemaps/tools/nextjs.js.map +1 -1
  59. package/dist/src/sourcemaps/tools/sentry-cli.js +19 -16
  60. package/dist/src/sourcemaps/tools/sentry-cli.js.map +1 -1
  61. package/dist/src/sourcemaps/tools/vite.d.ts +2 -1
  62. package/dist/src/sourcemaps/tools/vite.js +99 -12
  63. package/dist/src/sourcemaps/tools/vite.js.map +1 -1
  64. package/dist/src/sourcemaps/utils/detect-tool.d.ts +1 -1
  65. package/dist/src/sourcemaps/utils/detect-tool.js.map +1 -1
  66. package/dist/src/sveltekit/sdk-setup.js +3 -3
  67. package/dist/src/sveltekit/sdk-setup.js.map +1 -1
  68. package/dist/src/sveltekit/sveltekit-wizard.js +34 -44
  69. package/dist/src/sveltekit/sveltekit-wizard.js.map +1 -1
  70. package/dist/src/telemetry.js +1 -0
  71. package/dist/src/telemetry.js.map +1 -1
  72. package/dist/src/utils/ast-utils.d.ts +2 -2
  73. package/dist/src/utils/ast-utils.js +7 -7
  74. package/dist/src/utils/ast-utils.js.map +1 -1
  75. package/dist/src/utils/clack-utils.d.ts +22 -28
  76. package/dist/src/utils/clack-utils.js +270 -264
  77. package/dist/src/utils/clack-utils.js.map +1 -1
  78. package/dist/src/utils/package-manager.d.ts +10 -0
  79. package/dist/{lib/Helper/PackageManager.js → src/utils/package-manager.js} +42 -74
  80. package/dist/src/utils/package-manager.js.map +1 -0
  81. package/dist/src/utils/release-registry.d.ts +1 -0
  82. package/dist/src/utils/release-registry.js +68 -0
  83. package/dist/src/utils/release-registry.js.map +1 -0
  84. package/dist/src/utils/sentrycli-utils.d.ts +4 -0
  85. package/dist/src/utils/sentrycli-utils.js +41 -0
  86. package/dist/src/utils/sentrycli-utils.js.map +1 -0
  87. package/dist/test/sourcemaps/tools/vite.test.d.ts +1 -0
  88. package/dist/test/sourcemaps/tools/vite.test.js +132 -0
  89. package/dist/test/sourcemaps/tools/vite.test.js.map +1 -0
  90. package/lib/Constants.ts +5 -0
  91. package/lib/Steps/ChooseIntegration.ts +7 -3
  92. package/lib/Steps/Integrations/Android.ts +23 -0
  93. package/lib/Steps/Integrations/ReactNative.ts +9 -3
  94. package/lib/Steps/PromptForParameters.ts +48 -3
  95. package/lib/Steps/SentryProjectSelector.ts +3 -1
  96. package/package.json +4 -3
  97. package/src/android/android-wizard.ts +196 -0
  98. package/src/android/code-tools.ts +156 -0
  99. package/src/android/gradle.ts +245 -0
  100. package/src/android/manifest.ts +180 -0
  101. package/src/android/templates.ts +88 -0
  102. package/src/apple/apple-wizard.ts +113 -35
  103. package/src/apple/cocoapod.ts +6 -3
  104. package/src/apple/code-tools.ts +46 -18
  105. package/src/apple/fastlane.ts +6 -12
  106. package/src/apple/templates.ts +2 -8
  107. package/src/apple/xcode-manager.ts +167 -25
  108. package/src/nextjs/nextjs-wizard.ts +72 -8
  109. package/src/nextjs/templates.ts +16 -2
  110. package/src/remix/remix-wizard.ts +10 -15
  111. package/src/sourcemaps/sourcemaps-wizard.ts +19 -5
  112. package/src/sourcemaps/tools/nextjs.ts +2 -2
  113. package/src/sourcemaps/tools/sentry-cli.ts +8 -7
  114. package/src/sourcemaps/tools/vite.ts +136 -6
  115. package/src/sourcemaps/utils/detect-tool.ts +2 -1
  116. package/src/sveltekit/sdk-setup.ts +4 -4
  117. package/src/sveltekit/sveltekit-wizard.ts +5 -14
  118. package/src/telemetry.ts +2 -0
  119. package/src/utils/ast-utils.ts +7 -5
  120. package/src/utils/clack-utils.ts +337 -283
  121. package/src/utils/package-manager.ts +61 -0
  122. package/src/utils/release-registry.ts +19 -0
  123. package/src/utils/sentrycli-utils.ts +22 -0
  124. package/test/sourcemaps/tools/vite.test.ts +149 -0
  125. package/dist/lib/Helper/PackageManager.d.ts +0 -22
  126. package/dist/lib/Helper/PackageManager.js.map +0 -1
  127. package/dist/src/utils/vendor/clack-custom-select.d.ts +0 -21
  128. package/dist/src/utils/vendor/clack-custom-select.js +0 -137
  129. package/dist/src/utils/vendor/clack-custom-select.js.map +0 -1
  130. package/lib/Helper/PackageManager.ts +0 -59
  131. package/src/utils/vendor/clack-custom-select.ts +0 -160
package/lib/Constants.ts CHANGED
@@ -2,6 +2,7 @@
2
2
  export enum Integration {
3
3
  reactNative = 'reactNative',
4
4
  ios = 'ios',
5
+ android = 'android',
5
6
  cordova = 'cordova',
6
7
  electron = 'electron',
7
8
  nextjs = 'nextjs',
@@ -35,6 +36,8 @@ export function getPlatformDescription(type: string): string {
35
36
 
36
37
  export function getIntegrationDescription(type: string): string {
37
38
  switch (type) {
39
+ case Integration.android:
40
+ return 'Android';
38
41
  case Integration.reactNative:
39
42
  return 'React Native';
40
43
  case Integration.cordova:
@@ -58,6 +61,8 @@ export function getIntegrationDescription(type: string): string {
58
61
 
59
62
  export function mapIntegrationToPlatform(type: string): string | undefined {
60
63
  switch (type) {
64
+ case Integration.android:
65
+ return 'android';
61
66
  case Integration.reactNative:
62
67
  return 'react-native';
63
68
  case Integration.cordova:
@@ -1,6 +1,5 @@
1
1
  import type { Answers } from 'inquirer';
2
2
  import { prompt } from 'inquirer';
3
- import { dim } from 'picocolors';
4
3
 
5
4
  import {
6
5
  Args,
@@ -18,6 +17,8 @@ import { Apple } from './Integrations/Apple';
18
17
  import { SvelteKitShim } from './Integrations/SvelteKitShim';
19
18
  import { hasPackageInstalled } from '../../src/utils/package-json';
20
19
  import { Remix } from './Integrations/Remix';
20
+ import { Android } from './Integrations/Android';
21
+ import { dim } from '../Helper/Logging';
21
22
 
22
23
  let projectPackage: any = {};
23
24
 
@@ -38,6 +39,9 @@ export class ChooseIntegration extends BaseStep {
38
39
 
39
40
  let integration = null;
40
41
  switch (integrationPrompt.integration) {
42
+ case Integration.android:
43
+ integration = new Android(this._argv);
44
+ break;
41
45
  case Integration.cordova:
42
46
  integration = new Cordova(sanitizeUrl(this._argv));
43
47
  break;
@@ -97,7 +101,7 @@ export class ChooseIntegration extends BaseStep {
97
101
  return { integration: this._argv.integration };
98
102
  } else {
99
103
  if (this._argv.quiet) {
100
- throw new Error('You need to choose a integration');
104
+ throw new Error('You need to choose a platform');
101
105
  }
102
106
 
103
107
  const detectedDefaultSelection = this.tryDetectingIntegration();
@@ -106,7 +110,7 @@ export class ChooseIntegration extends BaseStep {
106
110
  {
107
111
  choices: getIntegrationChoices(),
108
112
  default: detectedDefaultSelection,
109
- message: 'What integration do you want to set up?',
113
+ message: 'What platform do you want to set up?',
110
114
  name: 'integration',
111
115
  type: 'list',
112
116
  },
@@ -0,0 +1,23 @@
1
+ import { Answers } from 'inquirer';
2
+ import { BaseIntegration } from './BaseIntegration';
3
+ import { Args } from '../../Constants';
4
+ import { runAndroidWizard } from '../../../src/android/android-wizard';
5
+
6
+ export class Android extends BaseIntegration {
7
+ public constructor(protected _argv: Args) {
8
+ super(_argv);
9
+ }
10
+
11
+ public async emit(_answers: Answers): Promise<Answers> {
12
+ await runAndroidWizard({
13
+ promoCode: this._argv.promoCode,
14
+ url: this._argv.url,
15
+ telemetryEnabled: !this._argv.disableTelemetry,
16
+ });
17
+ return {};
18
+ }
19
+
20
+ public shouldConfigure(_answers: Answers): Promise<Answers> {
21
+ return this._shouldConfigure;
22
+ }
23
+ }
@@ -16,7 +16,10 @@ import {
16
16
  } from '../../Helper/File';
17
17
  import { dim, green, l, nl, red } from '../../Helper/Logging';
18
18
  import { checkPackageVersion } from '../../Helper/Package';
19
- import { getPackageManagerChoice } from '../../Helper/PackageManager';
19
+ import {
20
+ detectPackageManger,
21
+ installPackageWithPackageManager,
22
+ } from '../../../src/utils/package-manager';
20
23
  import { SentryCli } from '../../Helper/SentryCli';
21
24
  import { MobileProject } from './MobileProject';
22
25
  import { BottomBar } from '../../Helper/BottomBar';
@@ -60,7 +63,7 @@ export class ReactNative extends MobileProject {
60
63
  nl();
61
64
 
62
65
  let userAnswers: Answers = { continue: true };
63
- const packageManager = getPackageManagerChoice();
66
+ const packageManager = detectPackageManger();
64
67
 
65
68
  const hasCompatibleReactNativeVersion = checkPackageVersion(
66
69
  this._readAppPackage(),
@@ -86,7 +89,10 @@ export class ReactNative extends MobileProject {
86
89
 
87
90
  if (packageManager) {
88
91
  BottomBar.show(`Adding ${SENTRY_REACT_NATIVE_PACKAGE}...`);
89
- await packageManager.installPackage(SENTRY_REACT_NATIVE_PACKAGE);
92
+ await installPackageWithPackageManager(
93
+ packageManager,
94
+ SENTRY_REACT_NATIVE_PACKAGE,
95
+ );
90
96
  BottomBar.hide();
91
97
  green(`✓ Added \`${SENTRY_REACT_NATIVE_PACKAGE}\``);
92
98
  }
@@ -50,11 +50,11 @@ export class PromptForParameters extends BaseStep {
50
50
  const dsn = await prompt([
51
51
  {
52
52
  message: 'DSN:',
53
- name: 'secret',
53
+ name: 'public',
54
54
  type: 'input',
55
55
  // eslint-disable-next-line @typescript-eslint/unbound-method
56
56
  validate: this._validateDSN,
57
- when: this._shouldAsk(answers, 'config.dsn.secret', () => {
57
+ when: this._shouldAsk(answers, 'config.dsn.public', () => {
58
58
  dim('Please copy/paste your DSN');
59
59
  dim(`It can be found here: ${url}`);
60
60
  }),
@@ -120,7 +120,16 @@ export class PromptForParameters extends BaseStep {
120
120
  }
121
121
 
122
122
  private _validateAuthToken(input: string): boolean | string {
123
- if (!input.match(/[0-9a-f]{64}/g)) {
123
+ const isOrgToken = input.startsWith('sntrys_');
124
+
125
+ if (isOrgToken) {
126
+ if (!isValidOrgToken(input)) {
127
+ return 'Make sure you correctly copied your auth token. It should start with "sntrys_"';
128
+ }
129
+ return true;
130
+ }
131
+
132
+ if (!input.match(/(sntrys_)?[0-9a-f]{64}/g)) {
124
133
  return 'Make sure you copied the correct auth token, it should be 64 hex chars';
125
134
  }
126
135
  return true;
@@ -149,3 +158,39 @@ export class PromptForParameters extends BaseStep {
149
158
  return true;
150
159
  }
151
160
  }
161
+
162
+ type MaybeOrgAuthToken = {
163
+ iat?: number;
164
+ url?: string;
165
+ org?: string;
166
+ region_url?: string;
167
+ };
168
+
169
+ /**
170
+ * Trying to parse and decode an org auth token. Based on:
171
+ * - https://github.com/getsentry/rfcs/blob/main/text/0091-ci-upload-tokens.md#parsing-tokens
172
+ * - https://github.com/getsentry/rfcs/blob/main/text/0091-ci-upload-tokens.md#token-facts
173
+ */
174
+ function isValidOrgToken(input: string): boolean {
175
+ if (!input.startsWith('sntrys_')) {
176
+ return false;
177
+ }
178
+
179
+ const tokenParts = input.split('_');
180
+ if (tokenParts.length < 3) {
181
+ return false;
182
+ }
183
+
184
+ try {
185
+ const payload = tokenParts[1];
186
+ const decodedPayload = Buffer.from(payload, 'base64').toString();
187
+ const jsonPayload = JSON.parse(decodedPayload) as MaybeOrgAuthToken;
188
+ if (!jsonPayload.iat || !jsonPayload.url || !jsonPayload.org) {
189
+ return false;
190
+ }
191
+ } catch {
192
+ return false;
193
+ }
194
+
195
+ return true;
196
+ }
@@ -21,7 +21,9 @@ export class SentryProjectSelector extends BaseStep {
21
21
  _.has(answers, 'wizard.projects') &&
22
22
  answers.wizard.projects.length === 0
23
23
  ) {
24
- throw new Error('no projects');
24
+ throw new Error(
25
+ 'No Projects found. Please create a new Project in Sentry and try again.',
26
+ );
25
27
  }
26
28
 
27
29
  let selectedProject = null;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sentry/wizard",
3
- "version": "3.10.0",
3
+ "version": "3.11.0",
4
4
  "homepage": "https://github.com/getsentry/sentry-wizard",
5
5
  "repository": "https://github.com/getsentry/sentry-wizard",
6
6
  "description": "Sentry wizard helping you to configure your project",
@@ -23,8 +23,8 @@
23
23
  "definition": "dist/index.d.ts"
24
24
  },
25
25
  "dependencies": {
26
- "@clack/core": "0.3.2",
27
- "@clack/prompts": "0.6.3",
26
+ "@clack/core": "0.3.3",
27
+ "@clack/prompts": "0.7.0",
28
28
  "@sentry/cli": "^1.72.0",
29
29
  "@sentry/node": "^7.57.0",
30
30
  "axios": "1.3.5",
@@ -38,6 +38,7 @@
38
38
  "read-env": "^1.3.0",
39
39
  "semver": "^7.5.3",
40
40
  "xcode": "3.0.1",
41
+ "xml-js": "^1.6.11",
41
42
  "yargs": "^16.2.0"
42
43
  },
43
44
  "devDependencies": {
@@ -0,0 +1,196 @@
1
+ /* eslint-disable @typescript-eslint/no-unsafe-assignment */
2
+ import * as fs from 'fs';
3
+ // @ts-ignore - clack is ESM and TS complains about that. It works though
4
+ import * as clack from '@clack/prompts';
5
+ import * as path from 'path';
6
+ import * as gradle from './gradle';
7
+ import * as manifest from './manifest';
8
+ import * as codetools from './code-tools';
9
+ import {
10
+ CliSetupConfig,
11
+ SENTRY_PROPERTIES_FILE,
12
+ abort,
13
+ addSentryCliConfig,
14
+ confirmContinueEvenThoughNoGitRepo,
15
+ getOrAskForProjectData,
16
+ printWelcome,
17
+ } from '../utils/clack-utils';
18
+ import { WizardOptions } from '../utils/types';
19
+ import { traceStep, withTelemetry } from '../telemetry';
20
+ import chalk from 'chalk';
21
+
22
+ const proguardMappingCliSetupConfig: CliSetupConfig = {
23
+ filename: SENTRY_PROPERTIES_FILE,
24
+ name: 'proguard mappings',
25
+ likelyAlreadyHasAuthToken(contents: string): boolean {
26
+ return !!contents.match(/auth\.token=./g);
27
+ },
28
+ tokenContent(authToken: string): string {
29
+ return `auth.token=${authToken}`;
30
+ },
31
+ likelyAlreadyHasOrgAndProject(contents: string): boolean {
32
+ return !!(
33
+ contents.match(/defaults\.org=./g) &&
34
+ contents.match(/defaults\.project=./g)
35
+ );
36
+ },
37
+ orgAndProjContent(org: string, project: string): string {
38
+ return `defaults.org=${org}\ndefaults.project=${project}`;
39
+ },
40
+ };
41
+
42
+ export async function runAndroidWizard(options: WizardOptions): Promise<void> {
43
+ return withTelemetry(
44
+ {
45
+ enabled: options.telemetryEnabled,
46
+ integration: 'android',
47
+ },
48
+ () => runAndroidWizardWithTelemetry(options),
49
+ );
50
+ }
51
+
52
+ async function runAndroidWizardWithTelemetry(
53
+ options: WizardOptions,
54
+ ): Promise<void> {
55
+ printWelcome({
56
+ wizardName: 'Sentry Android Wizard',
57
+ promoCode: options.promoCode,
58
+ });
59
+
60
+ await confirmContinueEvenThoughNoGitRepo();
61
+
62
+ const projectDir = process.cwd();
63
+ const buildGradleFiles = findFilesWithExtensions(projectDir, [
64
+ '.gradle',
65
+ 'gradle.kts',
66
+ ]);
67
+
68
+ if (!buildGradleFiles || buildGradleFiles.length === 0) {
69
+ clack.log.error(
70
+ 'No Gradle project found. Please run this command from the root of your project.',
71
+ );
72
+ await abort();
73
+ return;
74
+ }
75
+
76
+ const appFile = await traceStep('Select App File', () =>
77
+ gradle.selectAppFile(buildGradleFiles),
78
+ );
79
+
80
+ const { selectedProject, authToken } = await getOrAskForProjectData(
81
+ options,
82
+ 'android',
83
+ );
84
+
85
+ // ======== STEP 1. Add Sentry Gradle Plugin to build.gradle(.kts) ============
86
+ clack.log.step(
87
+ `Adding ${chalk.bold('Sentry Gradle plugin')} to your app's ${chalk.cyan(
88
+ 'build.gradle',
89
+ )} file.`,
90
+ );
91
+ const pluginAdded = await traceStep('Add Gradle Plugin', () =>
92
+ gradle.addGradlePlugin(
93
+ appFile,
94
+ selectedProject.organization.slug,
95
+ selectedProject.slug,
96
+ ),
97
+ );
98
+ if (!pluginAdded) {
99
+ clack.log.warn(
100
+ "Could not add Sentry Gradle plugin to your app's build.gradle file. You'll have to add it manually.\nPlease follow the instructions at https://docs.sentry.io/platforms/android/#install",
101
+ );
102
+ }
103
+
104
+ // ======== STEP 2. Configure Sentry SDK via AndroidManifest ============
105
+ clack.log.step(
106
+ `Configuring Sentry SDK via ${chalk.cyan('AndroidManifest.xml')}`,
107
+ );
108
+ const appDir = path.dirname(appFile);
109
+ const manifestFile = path.join(appDir, 'src', 'main', 'AndroidManifest.xml');
110
+
111
+ const manifestUpdated = traceStep('Update Android Manifest', () =>
112
+ manifest.addManifestSnippet(
113
+ manifestFile,
114
+ selectedProject.keys[0].dsn.public,
115
+ ),
116
+ );
117
+ if (!manifestUpdated) {
118
+ clack.log.warn(
119
+ "Could not configure the Sentry SDK. You'll have to do it manually.\nPlease follow the instructions at https://docs.sentry.io/platforms/android/#configure",
120
+ );
121
+ }
122
+
123
+ // ======== STEP 3. Patch Main Activity with a test error snippet ============
124
+ clack.log.step(
125
+ `Patching ${chalk.bold('Main Activity')} with a test error snippet.`,
126
+ );
127
+ const mainActivity = traceStep('Find Main Activity', () =>
128
+ manifest.getMainActivity(manifestFile),
129
+ );
130
+ let packageName = mainActivity.packageName;
131
+ if (!packageName) {
132
+ // if no package name in AndroidManifest, look into gradle script
133
+ packageName = gradle.getNamespace(appFile);
134
+ }
135
+ const activityName = mainActivity.activityName;
136
+ if (!activityName || !packageName) {
137
+ clack.log.warn(
138
+ "Could not find Activity with intent action MAIN. You'll have to manually verify the setup.\nPlease follow the instructions at https://docs.sentry.io/platforms/android/#verify",
139
+ );
140
+ } else {
141
+ const packageNameStable = packageName;
142
+ const activityFile = traceStep('Find Main Activity Source File', () =>
143
+ codetools.findActivitySourceFile(appDir, packageNameStable, activityName),
144
+ );
145
+
146
+ const activityPatched = traceStep('Patch Main Activity', () =>
147
+ codetools.patchMainActivity(activityFile),
148
+ );
149
+ if (!activityPatched) {
150
+ clack.log.warn(
151
+ "Could not patch main activity. You'll have to manually verify the setup.\nPlease follow the instructions at https://docs.sentry.io/platforms/android/#verify",
152
+ );
153
+ }
154
+ }
155
+
156
+ // ======== STEP 4. Add sentry-cli config file ============
157
+ clack.log.step(
158
+ `Configuring ${chalk.bold('proguard mappings upload')} via the ${chalk.cyan(
159
+ 'sentry.properties',
160
+ )} file.`,
161
+ );
162
+ await traceStep('Add SentryCli Config', () =>
163
+ addSentryCliConfig(authToken, proguardMappingCliSetupConfig),
164
+ );
165
+
166
+ // ======== OUTRO ========
167
+ clack.outro(`
168
+ ${chalk.greenBright('Successfully installed the Sentry Android SDK!')}
169
+
170
+ ${chalk.cyan(
171
+ 'You can validate your setup by launching your application and checking Sentry issues page afterwards',
172
+ )}
173
+
174
+ Check out the SDK documentation for further configuration:
175
+ https://docs.sentry.io/platforms/android/
176
+ `);
177
+ }
178
+
179
+ //find files with the given extension
180
+ function findFilesWithExtensions(
181
+ dir: string,
182
+ extensions: string[],
183
+ filesWithExtensions: string[] = [],
184
+ ): string[] {
185
+ const cwd = process.cwd();
186
+ const files = fs.readdirSync(dir, { withFileTypes: true });
187
+ for (const file of files) {
188
+ if (file.isDirectory()) {
189
+ const childDir = path.join(dir, file.name);
190
+ findFilesWithExtensions(childDir, extensions, filesWithExtensions);
191
+ } else if (extensions.some((ext) => file.name.endsWith(ext))) {
192
+ filesWithExtensions.push(path.relative(cwd, path.join(dir, file.name)));
193
+ }
194
+ }
195
+ return filesWithExtensions;
196
+ }
@@ -0,0 +1,156 @@
1
+ import * as fs from 'fs';
2
+ import * as path from 'path';
3
+ import * as Sentry from '@sentry/node';
4
+ // @ts-ignore - clack is ESM and TS complains about that. It works though
5
+ import * as clack from '@clack/prompts';
6
+ import chalk from 'chalk';
7
+ import {
8
+ sentryImport,
9
+ sentryImportKt,
10
+ testErrorSnippet,
11
+ testErrorSnippetKt,
12
+ } from './templates';
13
+ import { findFile } from '../utils/ast-utils';
14
+
15
+ /**
16
+ * Looks in src/main/java or src/main/kotlin for the specified {@link packageName} and
17
+ * {@link activityName} by concatenating them. For example:
18
+ *
19
+ * src/
20
+ * main/
21
+ * java/ or kotlin/
22
+ * my.package.name/
23
+ * ui/
24
+ * MainActivity.kt
25
+ *
26
+ * src/main/java can contain both .java and .kt sources, whilst src/main/kotlin only .kt
27
+ *
28
+ * @param appDir
29
+ * @param packageName
30
+ * @param activityName
31
+ * @returns path to the Main Activity
32
+ */
33
+ export function findActivitySourceFile(
34
+ appDir: string,
35
+ packageName: string,
36
+ activityName: string,
37
+ ): string | undefined {
38
+ const javaSrcDir = path.join(appDir, 'src', 'main', 'java');
39
+ let possibleActivityPath;
40
+ // if activity name starts with a dot, this means we need to concat packagename with it, otherwise
41
+ // the package name is already specified in the activity name itself
42
+ const packageNameParts = activityName.startsWith('.')
43
+ ? packageName.split('.')
44
+ : [];
45
+ const activityNameParts = activityName.split('.');
46
+
47
+ if (fs.existsSync(javaSrcDir)) {
48
+ possibleActivityPath = findFile(
49
+ path.join(javaSrcDir, ...packageNameParts, ...activityNameParts),
50
+ ['.kt', '.java'],
51
+ );
52
+ }
53
+
54
+ if (!possibleActivityPath || !fs.existsSync(possibleActivityPath)) {
55
+ const kotlinSrcDir = path.join(appDir, 'src', 'main', 'kotlin');
56
+ if (fs.existsSync(kotlinSrcDir)) {
57
+ possibleActivityPath = findFile(
58
+ path.join(kotlinSrcDir, ...packageNameParts, ...activityNameParts),
59
+ ['.kt'],
60
+ );
61
+ }
62
+ }
63
+ return possibleActivityPath;
64
+ }
65
+
66
+ /**
67
+ * Patches Main Activity with the test error code snippet by the specified path {@link activityFile}.
68
+ * Finds activity's `onCreate` method, adds the snippet and necessary imports.
69
+ *
70
+ * ```kotlin
71
+ * import something
72
+ * import something.something
73
+ * import io.sentry.Sentry <-- this is added by us
74
+ *
75
+ * override fun onCreate(savedInstanceState: Bundle?) {
76
+ * super.onCreate(savedInstanceState)
77
+ * // the snippet goes here <--
78
+ * doSomething()
79
+ * }
80
+ * ```
81
+ *
82
+ * @param activityFile
83
+ * @returns true if successfully patched, false otherwise
84
+ */
85
+ export function patchMainActivity(activityFile: string | undefined): boolean {
86
+ if (!activityFile || !fs.existsSync(activityFile)) {
87
+ clack.log.warn('No main activity source file found in filesystem.');
88
+ Sentry.captureException('No main activity source file');
89
+ return false;
90
+ }
91
+
92
+ const activityContent = fs.readFileSync(activityFile, 'utf8');
93
+
94
+ if (/import\s+io\.sentry\.Sentry;?/i.test(activityContent)) {
95
+ // sentry is already configured
96
+ clack.log.success(
97
+ chalk.greenBright(
98
+ `${chalk.bold(
99
+ 'Main Activity',
100
+ )} is already patched with test error snippet.`,
101
+ ),
102
+ );
103
+ return true;
104
+ }
105
+
106
+ const importRegex = /import\s+[\w.]+;?/gim;
107
+ let importsMatch = importRegex.exec(activityContent);
108
+ let importIndex = 0;
109
+ while (importsMatch) {
110
+ importIndex = importsMatch.index + importsMatch[0].length + 1;
111
+ importsMatch = importRegex.exec(activityContent);
112
+ }
113
+ let newActivityContent;
114
+ if (activityFile.endsWith('.kt')) {
115
+ newActivityContent =
116
+ activityContent.slice(0, importIndex) +
117
+ sentryImportKt +
118
+ activityContent.slice(importIndex);
119
+ } else {
120
+ newActivityContent =
121
+ activityContent.slice(0, importIndex) +
122
+ sentryImport +
123
+ activityContent.slice(importIndex);
124
+ }
125
+
126
+ const onCreateMatch = /super\.onCreate\(.*?\);?/i.exec(newActivityContent);
127
+ if (!onCreateMatch) {
128
+ clack.log.warn('No onCreate method found in main activity.');
129
+ Sentry.captureException('No onCreate method');
130
+ return false;
131
+ }
132
+
133
+ const onCreateIndex = onCreateMatch.index + onCreateMatch[0].length;
134
+ if (activityFile.endsWith('.kt')) {
135
+ newActivityContent =
136
+ newActivityContent.slice(0, onCreateIndex) +
137
+ testErrorSnippetKt +
138
+ newActivityContent.slice(onCreateIndex);
139
+ } else {
140
+ newActivityContent =
141
+ newActivityContent.slice(0, onCreateIndex) +
142
+ testErrorSnippet +
143
+ newActivityContent.slice(onCreateIndex);
144
+ }
145
+ fs.writeFileSync(activityFile, newActivityContent, 'utf8');
146
+
147
+ clack.log.success(
148
+ chalk.greenBright(
149
+ `Patched ${chalk.bold(
150
+ 'Main Activity',
151
+ )} with the Sentry test error snippet.`,
152
+ ),
153
+ );
154
+
155
+ return true;
156
+ }