@sentry/wizard 3.24.0 → 3.25.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 (58) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/dist/package.json +1 -1
  3. package/dist/src/nextjs/templates.js +1 -1
  4. package/dist/src/nextjs/templates.js.map +1 -1
  5. package/dist/src/react-native/expo-env-file.d.ts +2 -0
  6. package/dist/src/react-native/expo-env-file.js +127 -0
  7. package/dist/src/react-native/expo-env-file.js.map +1 -0
  8. package/dist/src/react-native/expo-metro.d.ts +7 -0
  9. package/dist/src/react-native/expo-metro.js +236 -0
  10. package/dist/src/react-native/expo-metro.js.map +1 -0
  11. package/dist/src/react-native/expo.d.ts +16 -0
  12. package/dist/src/react-native/expo.js +195 -0
  13. package/dist/src/react-native/expo.js.map +1 -0
  14. package/dist/src/react-native/git.d.ts +1 -0
  15. package/dist/src/react-native/git.js +85 -0
  16. package/dist/src/react-native/git.js.map +1 -0
  17. package/dist/src/react-native/javascript.d.ts +3 -0
  18. package/dist/src/react-native/javascript.js +119 -1
  19. package/dist/src/react-native/javascript.js.map +1 -1
  20. package/dist/src/react-native/metro.d.ts +3 -0
  21. package/dist/src/react-native/metro.js +17 -15
  22. package/dist/src/react-native/metro.js.map +1 -1
  23. package/dist/src/react-native/react-native-wizard.d.ts +12 -0
  24. package/dist/src/react-native/react-native-wizard.js +91 -78
  25. package/dist/src/react-native/react-native-wizard.js.map +1 -1
  26. package/dist/src/react-native/xcode.js +14 -3
  27. package/dist/src/react-native/xcode.js.map +1 -1
  28. package/dist/src/sourcemaps/sourcemaps-wizard.js +1 -1
  29. package/dist/src/sourcemaps/sourcemaps-wizard.js.map +1 -1
  30. package/dist/src/utils/clack-utils.d.ts +2 -1
  31. package/dist/src/utils/clack-utils.js +2 -2
  32. package/dist/src/utils/clack-utils.js.map +1 -1
  33. package/dist/test/nextjs/templates.test.js +1 -1
  34. package/dist/test/nextjs/templates.test.js.map +1 -1
  35. package/dist/test/react-native/expo-metro.test.d.ts +1 -0
  36. package/dist/test/react-native/expo-metro.test.js +26 -0
  37. package/dist/test/react-native/expo-metro.test.js.map +1 -0
  38. package/dist/test/react-native/expo.test.d.ts +1 -0
  39. package/dist/test/react-native/expo.test.js +57 -0
  40. package/dist/test/react-native/expo.test.js.map +1 -0
  41. package/dist/test/react-native/xcode.test.js +5 -0
  42. package/dist/test/react-native/xcode.test.js.map +1 -1
  43. package/package.json +1 -1
  44. package/src/nextjs/templates.ts +1 -1
  45. package/src/react-native/expo-env-file.ts +55 -0
  46. package/src/react-native/expo-metro.ts +212 -0
  47. package/src/react-native/expo.ts +175 -0
  48. package/src/react-native/git.ts +25 -0
  49. package/src/react-native/javascript.ts +68 -1
  50. package/src/react-native/metro.ts +3 -3
  51. package/src/react-native/react-native-wizard.ts +72 -76
  52. package/src/react-native/xcode.ts +21 -5
  53. package/src/sourcemaps/sourcemaps-wizard.ts +1 -1
  54. package/src/utils/clack-utils.ts +4 -4
  55. package/test/nextjs/templates.test.ts +1 -1
  56. package/test/react-native/expo-metro.test.ts +81 -0
  57. package/test/react-native/expo.test.ts +86 -0
  58. package/test/react-native/xcode.test.ts +90 -0
@@ -0,0 +1,175 @@
1
+ // @ts-ignore - clack is ESM and TS complains about that. It works though
2
+ import * as clack from '@clack/prompts';
3
+ import chalk from 'chalk';
4
+ import * as fs from 'fs';
5
+ import { EOL } from 'os';
6
+
7
+ import { isPlainObject } from '@sentry/utils';
8
+ import * as Sentry from '@sentry/node';
9
+ import {
10
+ makeCodeSnippet,
11
+ showCopyPasteInstructions,
12
+ } from '../utils/clack-utils';
13
+ import { RNCliSetupConfigContent } from './react-native-wizard';
14
+ import { traceStep } from '../telemetry';
15
+
16
+ export const SENTRY_EXPO_PLUGIN_NAME = '@sentry/react-native/expo';
17
+ export const DEPRECATED_SENTRY_EXPO_PLUGIN_NAME = 'sentry-expo';
18
+
19
+ export const SENTRY_PLUGIN_FUNCTION_NAME = 'withSentry';
20
+
21
+ const APP_CONFIG_JSON = `app.json`;
22
+
23
+ export interface AppConfigJson {
24
+ expo?: {
25
+ plugins?: Array<[string, undefined | Record<string, unknown>]>;
26
+ };
27
+ }
28
+
29
+ export function printSentryExpoMigrationOutro(): void {
30
+ clack.outro(
31
+ `Deprecated ${chalk.cyan(
32
+ 'sentry-expo',
33
+ )} package installed in your dependencies. Please follow the migration guide at ${chalk.cyan(
34
+ 'https://docs.sentry.io/platforms/react-native/migration/sentry-expo/',
35
+ )}`,
36
+ );
37
+ }
38
+
39
+ /**
40
+ * Finds app.json in the project root and add Sentry Expo `withSentry` plugin.
41
+ */
42
+ export async function patchExpoAppConfig(options: RNCliSetupConfigContent) {
43
+ function showInstructions() {
44
+ return showCopyPasteInstructions(
45
+ APP_CONFIG_JSON,
46
+ getSentryAppConfigJsonCodeSnippet(options),
47
+ 'This ensures auto upload of source maps during native app build.',
48
+ );
49
+ }
50
+
51
+ const appConfigJsonExists = fs.existsSync(APP_CONFIG_JSON);
52
+
53
+ Sentry.setTag(
54
+ 'app-config-file-status',
55
+ appConfigJsonExists ? 'found' : 'not-found',
56
+ );
57
+ if (!appConfigJsonExists) {
58
+ return await showInstructions();
59
+ }
60
+
61
+ const patched = await patchAppConfigJson(APP_CONFIG_JSON, options);
62
+ if (!patched) {
63
+ return await showInstructions();
64
+ }
65
+ }
66
+
67
+ async function patchAppConfigJson(
68
+ path: string,
69
+ options: RNCliSetupConfigContent,
70
+ ): Promise<boolean> {
71
+ const appConfigContent = (
72
+ await fs.promises.readFile(path, { encoding: 'utf-8' })
73
+ ).toString();
74
+ const patchedContent = traceStep('app-config-json-patch', () =>
75
+ addWithSentryToAppConfigJson(appConfigContent, options),
76
+ );
77
+ if (patchedContent === null) {
78
+ return false;
79
+ }
80
+
81
+ try {
82
+ await fs.promises.writeFile(path, patchedContent);
83
+ } catch (error) {
84
+ Sentry.setTag('app-config-file-status', 'json-write-error');
85
+ clack.log.error(`Unable to write ${chalk.cyan('app.config.json')}.`);
86
+ return false;
87
+ }
88
+ Sentry.setTag('app-config-file-status', 'json-write-success');
89
+ clack.log.success(
90
+ `Added Sentry Expo plugin to ${chalk.cyan('app.config.json')}.`,
91
+ );
92
+ return true;
93
+ }
94
+
95
+ export function addWithSentryToAppConfigJson(
96
+ appConfigContent: string,
97
+ options: RNCliSetupConfigContent,
98
+ ): string | null {
99
+ try {
100
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
101
+ const parsedAppConfig: AppConfigJson = JSON.parse(appConfigContent);
102
+ const includesWithSentry =
103
+ appConfigContent.includes(SENTRY_EXPO_PLUGIN_NAME) ||
104
+ appConfigContent.includes(DEPRECATED_SENTRY_EXPO_PLUGIN_NAME);
105
+
106
+ if (includesWithSentry) {
107
+ Sentry.setTag('app-config-file-status', 'already-patched');
108
+ clack.log.warn(
109
+ `Your ${chalk.cyan(
110
+ 'app.config.json',
111
+ )} already includes the Sentry Expo plugin.`,
112
+ );
113
+ return null;
114
+ }
115
+
116
+ if (
117
+ parsedAppConfig.expo !== undefined &&
118
+ !isPlainObject(parsedAppConfig.expo)
119
+ ) {
120
+ Sentry.setTag('app-config-file-status', 'invalid-json');
121
+ return null;
122
+ }
123
+ if (
124
+ parsedAppConfig.expo &&
125
+ parsedAppConfig.expo.plugins !== undefined &&
126
+ !Array.isArray(parsedAppConfig.expo.plugins)
127
+ ) {
128
+ Sentry.setTag('app-config-file-status', 'invalid-json');
129
+ return null;
130
+ }
131
+
132
+ parsedAppConfig.expo = parsedAppConfig.expo ?? {};
133
+ parsedAppConfig.expo.plugins = parsedAppConfig.expo.plugins ?? [];
134
+ parsedAppConfig.expo.plugins.push([
135
+ SENTRY_EXPO_PLUGIN_NAME,
136
+ {
137
+ url: options.url,
138
+ project: options.project,
139
+ organization: options.org,
140
+ },
141
+ ]);
142
+
143
+ return JSON.stringify(parsedAppConfig, null, 2) + EOL;
144
+ } catch (error) {
145
+ Sentry.setTag('app-config-file-status', 'invalid-json');
146
+ clack.log.error(
147
+ `Unable to parse your ${chalk.cyan(
148
+ 'app.config.json',
149
+ )}. Make sure it has a valid format!`,
150
+ );
151
+ return null;
152
+ }
153
+ }
154
+
155
+ export function getSentryAppConfigJsonCodeSnippet({
156
+ url,
157
+ project,
158
+ org,
159
+ }: Omit<RNCliSetupConfigContent, 'authToken'>) {
160
+ return makeCodeSnippet(true, (unchanged, plus, _minus) => {
161
+ return unchanged(`{
162
+ "name": "my app",
163
+ "plugins": [
164
+ ${plus(`[
165
+ "@sentry/react-native/expo",
166
+ {
167
+ "url": "${url}",
168
+ "project": "${project}",
169
+ "organization": "${org}"
170
+ }
171
+ ]`)}
172
+ ],
173
+ }`);
174
+ });
175
+ }
@@ -0,0 +1,25 @@
1
+ import * as fs from 'fs';
2
+
3
+ const GITIGNORE_FILENAME = '.gitignore';
4
+
5
+ export async function addToGitignore(filepath: string): Promise<boolean> {
6
+ /**
7
+ * Don't check whether the given file is ignored because:
8
+ * 1. It's tricky to check it without git.
9
+ * 2. Git might not be installed or accessible.
10
+ * 3. It's convenient to use a module to interact with git, but it would
11
+ * increase the size x2 approximately. Docs say to run the Wizard without
12
+ * installing it, and duplicating the size would slow the set-up down.
13
+ * 4. The Wizard is meant to be run once.
14
+ * 5. A message is logged informing users it's been added to the gitignore.
15
+ * 6. It will be added to the gitignore as many times as it runs - not a big
16
+ * deal.
17
+ * 7. It's straightforward to remove it from the gitignore.
18
+ */
19
+ try {
20
+ await fs.promises.appendFile(GITIGNORE_FILENAME, `\n${filepath}\n`);
21
+ return true;
22
+ } catch {
23
+ return false;
24
+ }
25
+ }
@@ -1,4 +1,71 @@
1
- import { makeCodeSnippet } from '../utils/clack-utils';
1
+ /* eslint-disable max-lines */
2
+ // @ts-ignore - clack is ESM and TS complains about that. It works though
3
+ import clack from '@clack/prompts';
4
+ import chalk from 'chalk';
5
+ import * as path from 'path';
6
+ import * as process from 'process';
7
+ import * as fs from 'fs';
8
+ import * as Sentry from '@sentry/node';
9
+
10
+ import { traceStep } from '../telemetry';
11
+ import {
12
+ makeCodeSnippet,
13
+ showCopyPasteInstructions,
14
+ } from '../utils/clack-utils';
15
+ import { getFirstMatchedPath } from './glob';
16
+ import { RN_SDK_PACKAGE } from './react-native-wizard';
17
+
18
+ export async function addSentryInit({ dsn }: { dsn: string }) {
19
+ const prefixGlob = '{.,./src,./app}';
20
+ const suffixGlob = '@(j|t|cj|mj)s?(x)';
21
+ const universalGlob = `@(App|_layout).${suffixGlob}`;
22
+ const jsFileGlob = `${prefixGlob}/+(${universalGlob})`;
23
+ const jsPath = traceStep('find-app-js-file', () =>
24
+ getFirstMatchedPath(jsFileGlob),
25
+ );
26
+ Sentry.setTag('app-js-file-status', jsPath ? 'found' : 'not-found');
27
+ if (!jsPath) {
28
+ clack.log.warn(
29
+ `Could not find main App file. Place the following code snippet close to the Apps Root component.`,
30
+ );
31
+ await showCopyPasteInstructions(
32
+ 'App.js or _layout.tsx',
33
+ getSentryInitColoredCodeSnippet(dsn),
34
+ 'This ensures the Sentry SDK is ready to capture errors.',
35
+ );
36
+ return;
37
+ }
38
+ const jsRelativePath = path.relative(process.cwd(), jsPath);
39
+
40
+ const js = fs.readFileSync(jsPath, 'utf-8');
41
+ const includesSentry = doesJsCodeIncludeSdkSentryImport(js, {
42
+ sdkPackageName: RN_SDK_PACKAGE,
43
+ });
44
+ if (includesSentry) {
45
+ Sentry.setTag('app-js-file-status', 'already-includes-sentry');
46
+ clack.log.warn(
47
+ `${chalk.cyan(
48
+ jsRelativePath,
49
+ )} already includes Sentry. We wont't add it again.`,
50
+ );
51
+ return;
52
+ }
53
+
54
+ traceStep('add-sentry-init', () => {
55
+ const newContent = addSentryInitWithSdkImport(js, { dsn });
56
+
57
+ clack.log.success(
58
+ `Added ${chalk.cyan('Sentry.init')} to ${chalk.cyan(jsRelativePath)}.`,
59
+ );
60
+
61
+ fs.writeFileSync(jsPath, newContent, 'utf-8');
62
+ });
63
+
64
+ Sentry.setTag('app-js-file-status', 'added-sentry-init');
65
+ clack.log.success(
66
+ chalk.green(`${chalk.cyan(jsRelativePath)} changes saved.`),
67
+ );
68
+ }
2
69
 
3
70
  export function addSentryInitWithSdkImport(
4
71
  js: string,
@@ -23,7 +23,7 @@ import chalk from 'chalk';
23
23
 
24
24
  const b = recast.types.builders;
25
25
 
26
- const metroConfigPath = 'metro.config.js';
26
+ export const metroConfigPath = 'metro.config.js';
27
27
 
28
28
  export async function patchMetroWithSentryConfig() {
29
29
  const mod = await parseMetroConfig();
@@ -259,7 +259,7 @@ export function removeSentryRequire(program: t.Program): boolean {
259
259
  return removeRequire(program, '@sentry');
260
260
  }
261
261
 
262
- async function parseMetroConfig(): Promise<ProxifiedModule> {
262
+ export async function parseMetroConfig(): Promise<ProxifiedModule> {
263
263
  const metroConfigContent = (
264
264
  await fs.promises.readFile(metroConfigPath)
265
265
  ).toString();
@@ -267,7 +267,7 @@ async function parseMetroConfig(): Promise<ProxifiedModule> {
267
267
  return parseModule(metroConfigContent);
268
268
  }
269
269
 
270
- async function writeMetroConfig(mod: ProxifiedModule): Promise<boolean> {
270
+ export async function writeMetroConfig(mod: ProxifiedModule): Promise<boolean> {
271
271
  try {
272
272
  await writeFile(mod.$ast, metroConfigPath);
273
273
  } catch (e) {
@@ -3,8 +3,7 @@
3
3
  import clack from '@clack/prompts';
4
4
  import chalk from 'chalk';
5
5
  import * as fs from 'fs';
6
- import * as path from 'path';
7
- import * as process from 'process';
6
+
8
7
  import {
9
8
  CliSetupConfigContent,
10
9
  abortIfCancelled,
@@ -17,7 +16,6 @@ import {
17
16
  installPackage,
18
17
  printWelcome,
19
18
  propertiesCliSetupConfig,
20
- showCopyPasteInstructions,
21
19
  } from '../utils/clack-utils';
22
20
  import { getPackageVersion, hasPackageInstalled } from '../utils/package-json';
23
21
  import { podInstall } from '../apple/cocoapod';
@@ -41,11 +39,7 @@ import {
41
39
  import { runReactNativeUninstall } from './uninstall';
42
40
  import { APP_BUILD_GRADLE, XCODE_PROJECT, getFirstMatchedPath } from './glob';
43
41
  import { ReactNativeWizardOptions } from './options';
44
- import {
45
- addSentryInitWithSdkImport,
46
- doesJsCodeIncludeSdkSentryImport,
47
- getSentryInitColoredCodeSnippet,
48
- } from './javascript';
42
+ import { addSentryInit } from './javascript';
49
43
  import { traceStep, withTelemetry } from '../telemetry';
50
44
  import * as Sentry from '@sentry/node';
51
45
  import { fulfillsVersionRange } from '../utils/semver';
@@ -54,6 +48,9 @@ import {
54
48
  patchMetroConfigWithSentrySerializer,
55
49
  patchMetroWithSentryConfig,
56
50
  } from './metro';
51
+ import { patchExpoAppConfig, printSentryExpoMigrationOutro } from './expo';
52
+ import { addSentryToExpoMetroConfig } from './expo-metro';
53
+ import { addExpoEnvLocal } from './expo-env-file';
57
54
 
58
55
  // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
59
56
  const xcode = require('xcode');
@@ -64,14 +61,24 @@ export const RN_PACKAGE = 'react-native';
64
61
  export const RN_HUMAN_NAME = 'React Native';
65
62
 
66
63
  export const SUPPORTED_RN_RANGE = '>=0.69.0';
64
+ export const SUPPORTED_EXPO_RANGE = '>=50.0.0';
67
65
 
68
- // The following SDK version ship with bundled Xcode scripts
69
- // which simplifies the Xcode Build Phases setup.
66
+ /**
67
+ * The following SDK version ship with bundled Xcode scripts
68
+ * which simplifies the Xcode Build Phases setup.
69
+ */
70
70
  export const SDK_XCODE_SCRIPTS_SUPPORTED_SDK_RANGE = '>=5.11.0';
71
71
 
72
- // The following SDK version ship with Sentry Metro plugin
72
+ /**
73
+ * The following SDK version ship with Sentry Metro plugin
74
+ */
73
75
  export const SDK_SENTRY_METRO_PLUGIN_SUPPORTED_SDK_RANGE = '>=5.11.0';
74
76
 
77
+ /**
78
+ * The following SDK version ship with bundled Expo plugin
79
+ */
80
+ export const SDK_EXPO_SUPPORTED_SDK_RANGE = `>=5.16.0`;
81
+
75
82
  // The following SDK version shipped `withSentryConfig`
76
83
  export const SDK_SENTRY_METRO_WITH_SENTRY_CONFIG_SUPPORTED_SDK_RANGE =
77
84
  '>=5.17.0';
@@ -110,6 +117,13 @@ export async function runReactNativeWizardWithTelemetry(
110
117
  await confirmContinueIfNoOrDirtyGitRepo();
111
118
 
112
119
  const packageJson = await getPackageDotJson();
120
+ const hasInstalled = (dep: string) => hasPackageInstalled(dep, packageJson);
121
+
122
+ if (hasInstalled('sentry-expo')) {
123
+ Sentry.setTag('has-sentry-expo-installed', true);
124
+ printSentryExpoMigrationOutro();
125
+ return;
126
+ }
113
127
 
114
128
  await ensurePackageIsInstalled(packageJson, RN_PACKAGE, RN_HUMAN_NAME);
115
129
 
@@ -120,6 +134,38 @@ export async function runReactNativeWizardWithTelemetry(
120
134
  packageVersion: rnVersion,
121
135
  packageId: RN_PACKAGE,
122
136
  acceptableVersions: SUPPORTED_RN_RANGE,
137
+ note: `Please upgrade to ${SUPPORTED_RN_RANGE} if you wish to use the Sentry Wizard.
138
+ Or setup using ${chalk.cyan(
139
+ 'https://docs.sentry.io/platforms/react-native/manual-setup/manual-setup/',
140
+ )}`,
141
+ });
142
+ }
143
+
144
+ await installPackage({
145
+ packageName: RN_SDK_PACKAGE,
146
+ alreadyInstalled: hasPackageInstalled(RN_SDK_PACKAGE, packageJson),
147
+ });
148
+ const sdkVersion = getPackageVersion(
149
+ RN_SDK_PACKAGE,
150
+ await getPackageDotJson(),
151
+ );
152
+
153
+ const expoVersion = getPackageVersion('expo', packageJson);
154
+ const isExpo = !!expoVersion;
155
+ if (expoVersion && sdkVersion) {
156
+ await confirmContinueIfPackageVersionNotSupported({
157
+ packageName: 'Sentry React Native SDK',
158
+ packageVersion: sdkVersion,
159
+ packageId: RN_SDK_PACKAGE,
160
+ acceptableVersions: SDK_EXPO_SUPPORTED_SDK_RANGE,
161
+ note: `Please upgrade to ${SDK_EXPO_SUPPORTED_SDK_RANGE} to continue with the wizard in this Expo project.`,
162
+ });
163
+ await confirmContinueIfPackageVersionNotSupported({
164
+ packageName: 'Expo SDK',
165
+ packageVersion: expoVersion,
166
+ packageId: 'expo',
167
+ acceptableVersions: SUPPORTED_EXPO_RANGE,
168
+ note: `Please upgrade to ${SUPPORTED_EXPO_RANGE} to continue with the wizard in this Expo project.`,
123
169
  });
124
170
  }
125
171
 
@@ -135,22 +181,24 @@ export async function runReactNativeWizardWithTelemetry(
135
181
  url: sentryUrl,
136
182
  };
137
183
 
138
- await installPackage({
139
- packageName: RN_SDK_PACKAGE,
140
- alreadyInstalled: hasPackageInstalled(RN_SDK_PACKAGE, packageJson),
141
- });
142
- const sdkVersion = getPackageVersion(
143
- RN_SDK_PACKAGE,
144
- await getPackageDotJson(),
145
- );
146
-
147
- await traceStep('patch-js', () =>
184
+ await traceStep('patch-app-js', () =>
148
185
  addSentryInit({ dsn: selectedProject.keys[0].dsn.public }),
149
186
  );
150
187
 
151
- await traceStep('patch-metro-config', () =>
152
- addSentryToMetroConfig({ sdkVersion }),
153
- );
188
+ if (isExpo) {
189
+ await traceStep('patch-expo-app-config', () =>
190
+ patchExpoAppConfig(cliConfig),
191
+ );
192
+ await traceStep('add-expo-env-local', () => addExpoEnvLocal(cliConfig));
193
+ }
194
+
195
+ if (isExpo) {
196
+ await traceStep('patch-metro-config', addSentryToExpoMetroConfig);
197
+ } else {
198
+ await traceStep('patch-metro-config', () =>
199
+ addSentryToMetroConfig({ sdkVersion }),
200
+ );
201
+ }
154
202
 
155
203
  if (fs.existsSync('ios')) {
156
204
  Sentry.setTag('patch-ios', true);
@@ -217,58 +265,6 @@ function addSentryToMetroConfig({
217
265
  }
218
266
  }
219
267
 
220
- async function addSentryInit({ dsn }: { dsn: string }) {
221
- const prefixGlob = '{.,./src}';
222
- const suffixGlob = '@(j|t|cj|mj)s?(x)';
223
- const universalGlob = `App.${suffixGlob}`;
224
- const jsFileGlob = `${prefixGlob}/+(${universalGlob})`;
225
- const jsPath = traceStep('find-app-js-file', () =>
226
- getFirstMatchedPath(jsFileGlob),
227
- );
228
- Sentry.setTag('app-js-file-status', jsPath ? 'found' : 'not-found');
229
- if (!jsPath) {
230
- clack.log.warn(
231
- `Could not find main App file using ${chalk.cyan(jsFileGlob)}.`,
232
- );
233
- await showCopyPasteInstructions(
234
- 'App.js',
235
- getSentryInitColoredCodeSnippet(dsn),
236
- 'This ensures the Sentry SDK is ready to capture errors.',
237
- );
238
- return;
239
- }
240
- const jsRelativePath = path.relative(process.cwd(), jsPath);
241
-
242
- const js = fs.readFileSync(jsPath, 'utf-8');
243
- const includesSentry = doesJsCodeIncludeSdkSentryImport(js, {
244
- sdkPackageName: RN_SDK_PACKAGE,
245
- });
246
- if (includesSentry) {
247
- Sentry.setTag('app-js-file-status', 'already-includes-sentry');
248
- clack.log.warn(
249
- `${chalk.cyan(
250
- jsRelativePath,
251
- )} already includes Sentry. We wont't add it again.`,
252
- );
253
- return;
254
- }
255
-
256
- traceStep('add-sentry-init', () => {
257
- const newContent = addSentryInitWithSdkImport(js, { dsn });
258
-
259
- clack.log.success(
260
- `Added ${chalk.cyan('Sentry.init')} to ${chalk.cyan(jsRelativePath)}.`,
261
- );
262
-
263
- fs.writeFileSync(jsPath, newContent, 'utf-8');
264
- });
265
-
266
- Sentry.setTag('app-js-file-status', 'added-sentry-init');
267
- clack.log.success(
268
- chalk.green(`${chalk.cyan(jsRelativePath)} changes saved.`),
269
- );
270
- }
271
-
272
268
  async function confirmFirstSentryException(
273
269
  url: string,
274
270
  orgSlug: string,
@@ -128,11 +128,27 @@ export function doesBundlePhaseIncludeSentry(buildPhase: BuildPhase) {
128
128
  export function addSentryWithBundledScriptsToBundleShellScript(
129
129
  script: string,
130
130
  ): string {
131
- return script.replace(
132
- '$REACT_NATIVE_XCODE',
133
- // eslint-disable-next-line no-useless-escape
134
- '\\"/bin/sh ../node_modules/@sentry/react-native/scripts/sentry-xcode.sh $REACT_NATIVE_XCODE\\"',
135
- );
131
+ const isLikelyPlainReactNativeScript = script.includes('$REACT_NATIVE_XCODE');
132
+ if (isLikelyPlainReactNativeScript) {
133
+ return script.replace(
134
+ '$REACT_NATIVE_XCODE',
135
+ // eslint-disable-next-line no-useless-escape
136
+ '\\"/bin/sh ../node_modules/@sentry/react-native/scripts/sentry-xcode.sh $REACT_NATIVE_XCODE\\"',
137
+ );
138
+ }
139
+
140
+ const isLikelyExpoScript = script.includes('expo');
141
+ if (isLikelyExpoScript) {
142
+ const SENTRY_REACT_NATIVE_XCODE_PATH =
143
+ "`\"$NODE_BINARY\" --print \"require('path').dirname(require.resolve('@sentry/react-native/package.json')) + '/scripts/sentry-xcode.sh'\"`";
144
+ return script.replace(
145
+ /^.*?(packager|scripts)\/react-native-xcode\.sh\s*(\\'\\\\")?/m,
146
+ // eslint-disable-next-line no-useless-escape
147
+ (match: string) => `/bin/sh ${SENTRY_REACT_NATIVE_XCODE_PATH} ${match}`,
148
+ );
149
+ }
150
+
151
+ return script;
136
152
  }
137
153
 
138
154
  export function addSentryWithCliToBundleShellScript(script: string): string {
@@ -243,7 +243,7 @@ export async function configureCI(
243
243
  options: [
244
244
  {
245
245
  label: 'Yes',
246
- hint: 'I use a tool like Github Actions, Gitlab, CircleCI, TravisCI, Jenkins, Vercel, ...',
246
+ hint: 'I use a tool like GitHub Actions, GitLab, CircleCI, TravisCI, Jenkins, Vercel, ...',
247
247
  value: true,
248
248
  },
249
249
  {
@@ -295,11 +295,13 @@ export async function confirmContinueIfPackageVersionNotSupported({
295
295
  packageName,
296
296
  packageVersion,
297
297
  acceptableVersions,
298
+ note,
298
299
  }: {
299
300
  packageId: string;
300
301
  packageName: string;
301
302
  packageVersion: string;
302
303
  acceptableVersions: string;
304
+ note?: string;
303
305
  }): Promise<void> {
304
306
  return traceStep(`check-package-version`, async () => {
305
307
  Sentry.setTag(`${packageName.toLowerCase()}-version`, packageVersion);
@@ -321,10 +323,8 @@ export async function confirmContinueIfPackageVersionNotSupported({
321
323
  );
322
324
 
323
325
  clack.note(
324
- `Please upgrade to ${acceptableVersions} if you wish to use the Sentry Wizard.
325
- Or setup using ${chalk.cyan(
326
- 'https://docs.sentry.io/platforms/react-native/manual-setup/manual-setup/',
327
- )}`,
326
+ note ??
327
+ `Please upgrade to ${acceptableVersions} if you wish to use the Sentry Wizard.`,
328
328
  );
329
329
  const continueWithUnsupportedVersion = await abortIfCancelled(
330
330
  clack.confirm({
@@ -65,7 +65,7 @@ describe('NextJS code templates', () => {
65
65
 
66
66
  org: "my-org",
67
67
  project: "my-project",
68
- sentryUrl: "https://my-sentry.com"
68
+ sentryUrl: "https://my-sentry.com",
69
69
 
70
70
  // Only print logs for uploading source maps in CI
71
71
  silent: !process.env.CI,
@@ -0,0 +1,81 @@
1
+ // @ts-ignore - magicast is ESM and TS complains about that. It works though
2
+ import { generateCode, parseModule } from 'magicast';
3
+ import { patchMetroInMemory } from '../../src/react-native/expo-metro';
4
+
5
+ describe('expo-metro config', () => {
6
+ test('patches minimal expo config', () => {
7
+ const mod = parseModule(`
8
+ const { getDefaultConfig } = require("expo/metro-config");
9
+
10
+ /** @type {import('expo/metro-config').MetroConfig} */
11
+ const config = getDefaultConfig(__dirname);
12
+
13
+ config.resolver.assetExts.push(
14
+ // Adds support for .db files for SQLite databases
15
+ 'db'
16
+ );
17
+
18
+ module.exports = config;
19
+ `);
20
+
21
+ const result = patchMetroInMemory(mod);
22
+ expect(result).toBe(true);
23
+ expect(generateCode(mod.$ast).code).toBe(
24
+ `
25
+ const {
26
+ getSentryExpoConfig
27
+ } = require("@sentry/react-native/metro");
28
+
29
+ /** @type {import('expo/metro-config').MetroConfig} */
30
+ const config = getSentryExpoConfig(__dirname);
31
+
32
+ config.resolver.assetExts.push(
33
+ // Adds support for .db files for SQLite databases
34
+ 'db'
35
+ );
36
+
37
+ module.exports = config;
38
+ `.trim(),
39
+ );
40
+ });
41
+
42
+ test('keeps expo metro config if other imports are present', () => {
43
+ const mod = parseModule(`
44
+ const { getDefaultConfig, otherExport } = require("expo/metro-config");
45
+
46
+ const config = getDefaultConfig(__dirname);
47
+
48
+ module.exports = config;
49
+ `);
50
+
51
+ const result = patchMetroInMemory(mod);
52
+ expect(result).toBe(true);
53
+ expect(generateCode(mod.$ast).code).toBe(
54
+ `
55
+ const { getDefaultConfig, otherExport } = require("expo/metro-config");
56
+
57
+ const {
58
+ getSentryExpoConfig
59
+ } = require("@sentry/react-native/metro");
60
+
61
+ const config = getSentryExpoConfig(__dirname);
62
+
63
+ module.exports = config;
64
+ `.trim(),
65
+ );
66
+ });
67
+
68
+ test('does not modify when sentry already present', () => {
69
+ const mod = parseModule(`
70
+ const { getSentryExpoConfig } = require("@sentry/react-native/metro");
71
+ `);
72
+
73
+ const result = patchMetroInMemory(mod);
74
+ expect(result).toBe(false);
75
+ expect(generateCode(mod.$ast).code).toBe(
76
+ `
77
+ const { getSentryExpoConfig } = require("@sentry/react-native/metro");
78
+ `.trim(),
79
+ );
80
+ });
81
+ });