@sentry/wizard 3.7.1 → 3.9.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 (113) hide show
  1. package/CHANGELOG.md +21 -0
  2. package/bin.ts +14 -0
  3. package/dist/bin.js +9 -0
  4. package/dist/bin.js.map +1 -1
  5. package/dist/lib/Helper/Logging.d.ts +1 -0
  6. package/dist/lib/Helper/Logging.js +2 -1
  7. package/dist/lib/Helper/Logging.js.map +1 -1
  8. package/dist/lib/Helper/__tests__/MergeConfig.js.map +1 -1
  9. package/dist/lib/Setup.js +4 -0
  10. package/dist/lib/Setup.js.map +1 -1
  11. package/dist/lib/Steps/ChooseIntegration.js +12 -26
  12. package/dist/lib/Steps/ChooseIntegration.js.map +1 -1
  13. package/dist/lib/Steps/Integrations/Cordova.js.map +1 -1
  14. package/dist/lib/Steps/Integrations/Electron.js.map +1 -1
  15. package/dist/lib/Steps/Integrations/MobileProject.js.map +1 -1
  16. package/dist/lib/Steps/Integrations/ReactNative.js +5 -5
  17. package/dist/lib/Steps/Integrations/ReactNative.js.map +1 -1
  18. package/dist/lib/Steps/Integrations/__tests__/ReactNative.js +5 -6
  19. package/dist/lib/Steps/Integrations/__tests__/ReactNative.js.map +1 -1
  20. package/dist/package.json +11 -7
  21. package/dist/src/apple/apple-wizard.js +31 -2
  22. package/dist/src/apple/apple-wizard.js.map +1 -1
  23. package/dist/src/apple/cocoapod.d.ts +2 -0
  24. package/dist/src/apple/cocoapod.js +122 -0
  25. package/dist/src/apple/cocoapod.js.map +1 -0
  26. package/dist/src/apple/code-tools.js +22 -12
  27. package/dist/src/apple/code-tools.js.map +1 -1
  28. package/dist/src/apple/fastlane.d.ts +2 -0
  29. package/dist/src/apple/fastlane.js +179 -0
  30. package/dist/src/apple/fastlane.js.map +1 -0
  31. package/dist/src/apple/templates.d.ts +1 -0
  32. package/dist/src/apple/templates.js +7 -3
  33. package/dist/src/apple/templates.js.map +1 -1
  34. package/dist/src/apple/xcode-manager.d.ts +1 -1
  35. package/dist/src/apple/xcode-manager.js +35 -28
  36. package/dist/src/apple/xcode-manager.js.map +1 -1
  37. package/dist/src/nextjs/nextjs-wizard.js +71 -81
  38. package/dist/src/nextjs/nextjs-wizard.js.map +1 -1
  39. package/dist/src/sourcemaps/sourcemaps-wizard.js +61 -46
  40. package/dist/src/sourcemaps/sourcemaps-wizard.js.map +1 -1
  41. package/dist/src/sourcemaps/tools/nextjs.d.ts +3 -0
  42. package/dist/src/sourcemaps/tools/nextjs.js +135 -0
  43. package/dist/src/sourcemaps/tools/nextjs.js.map +1 -0
  44. package/dist/src/sourcemaps/tools/sentry-cli.js +120 -16
  45. package/dist/src/sourcemaps/tools/sentry-cli.js.map +1 -1
  46. package/dist/src/sourcemaps/tools/vite.js +102 -12
  47. package/dist/src/sourcemaps/tools/vite.js.map +1 -1
  48. package/dist/src/sourcemaps/utils/detect-tool.d.ts +1 -1
  49. package/dist/src/sourcemaps/utils/detect-tool.js +1 -0
  50. package/dist/src/sourcemaps/utils/detect-tool.js.map +1 -1
  51. package/dist/src/sourcemaps/utils/other-wizards.js +35 -12
  52. package/dist/src/sourcemaps/utils/other-wizards.js.map +1 -1
  53. package/dist/src/sveltekit/sdk-setup.d.ts +9 -1
  54. package/dist/src/sveltekit/sdk-setup.js +73 -29
  55. package/dist/src/sveltekit/sdk-setup.js.map +1 -1
  56. package/dist/src/sveltekit/sveltekit-wizard.js +23 -13
  57. package/dist/src/sveltekit/sveltekit-wizard.js.map +1 -1
  58. package/dist/src/utils/ast-utils.d.ts +8 -0
  59. package/dist/src/utils/ast-utils.js +45 -0
  60. package/dist/src/utils/ast-utils.js.map +1 -0
  61. package/dist/src/utils/bash.d.ts +2 -1
  62. package/dist/src/utils/bash.js +14 -2
  63. package/dist/src/utils/bash.js.map +1 -1
  64. package/dist/src/utils/clack-utils.d.ts +7 -14
  65. package/dist/src/utils/clack-utils.js +46 -2
  66. package/dist/src/utils/clack-utils.js.map +1 -1
  67. package/dist/src/utils/debug.d.ts +2 -0
  68. package/dist/src/utils/debug.js +51 -0
  69. package/dist/src/utils/debug.js.map +1 -0
  70. package/dist/src/utils/package-json.d.ts +1 -1
  71. package/dist/src/utils/package-json.js.map +1 -1
  72. package/dist/src/utils/types.d.ts +24 -0
  73. package/dist/src/utils/types.js.map +1 -1
  74. package/dist/test/utils/ast-utils.test.d.ts +1 -0
  75. package/dist/test/utils/ast-utils.test.js +21 -0
  76. package/dist/test/utils/ast-utils.test.js.map +1 -0
  77. package/lib/Helper/Logging.ts +1 -1
  78. package/lib/Helper/__tests__/MergeConfig.ts +9 -4
  79. package/lib/Setup.ts +5 -0
  80. package/lib/Steps/ChooseIntegration.ts +13 -3
  81. package/lib/Steps/Integrations/Cordova.ts +3 -3
  82. package/lib/Steps/Integrations/Electron.ts +1 -2
  83. package/lib/Steps/Integrations/MobileProject.ts +1 -1
  84. package/lib/Steps/Integrations/ReactNative.ts +23 -17
  85. package/lib/Steps/Integrations/__tests__/ReactNative.ts +24 -15
  86. package/package.json +11 -7
  87. package/src/apple/apple-wizard.ts +35 -3
  88. package/src/apple/cocoapod.ts +57 -0
  89. package/src/apple/code-tools.ts +80 -57
  90. package/src/apple/fastlane.ts +160 -0
  91. package/src/apple/templates.ts +26 -10
  92. package/src/apple/xcode-manager.ts +137 -120
  93. package/src/nextjs/nextjs-wizard.ts +4 -13
  94. package/src/sourcemaps/sourcemaps-wizard.ts +40 -28
  95. package/src/sourcemaps/tools/nextjs.ts +114 -0
  96. package/src/sourcemaps/tools/sentry-cli.ts +134 -8
  97. package/src/sourcemaps/tools/vite.ts +101 -12
  98. package/src/sourcemaps/utils/detect-tool.ts +3 -1
  99. package/src/sourcemaps/utils/other-wizards.ts +32 -13
  100. package/src/sveltekit/sdk-setup.ts +122 -43
  101. package/src/sveltekit/sveltekit-wizard.ts +15 -6
  102. package/src/utils/ast-utils.ts +20 -0
  103. package/src/utils/bash.ts +43 -30
  104. package/src/utils/clack-utils.ts +42 -14
  105. package/src/utils/debug.ts +20 -0
  106. package/src/utils/package-json.ts +1 -1
  107. package/src/utils/types.ts +22 -0
  108. package/test/utils/ast-utils.test.ts +44 -0
  109. package/dist/src/sveltekit/sentry-cli-setup.d.ts +0 -2
  110. package/dist/src/sveltekit/sentry-cli-setup.js +0 -71
  111. package/dist/src/sveltekit/sentry-cli-setup.js.map +0 -1
  112. package/package-lock.json +0 -8910
  113. package/src/sveltekit/sentry-cli-setup.ts +0 -27
@@ -11,11 +11,9 @@ import {
11
11
  abort,
12
12
  abortIfCancelled,
13
13
  addSentryCliRc,
14
- askForProjectSelection,
15
- askForSelfHosted,
16
- askForWizardLogin,
17
14
  confirmContinueEvenThoughNoGitRepo,
18
15
  ensurePackageIsInstalled,
16
+ getOrAskForProjectData,
19
17
  getPackageDotJson,
20
18
  installPackage,
21
19
  isUsingTypeScript,
@@ -45,15 +43,8 @@ export async function runNextjsWizard(options: WizardOptions): Promise<void> {
45
43
  const packageJson = await getPackageDotJson();
46
44
  await ensurePackageIsInstalled(packageJson, 'next', 'Next.js');
47
45
 
48
- const { url: sentryUrl, selfHosted } = await askForSelfHosted(options.url);
49
-
50
- const { projects, apiKeys } = await askForWizardLogin({
51
- promoCode: options.promoCode,
52
- url: sentryUrl,
53
- platform: 'javascript-nextjs',
54
- });
55
-
56
- const selectedProject = await askForProjectSelection(projects);
46
+ const { selectedProject, authToken, selfHosted, sentryUrl } =
47
+ await getOrAskForProjectData(options);
57
48
 
58
49
  await installPackage({
59
50
  packageName: '@sentry/nextjs',
@@ -349,7 +340,7 @@ export async function runNextjsWizard(options: WizardOptions): Promise<void> {
349
340
  );
350
341
  }
351
342
 
352
- await addSentryCliRc(apiKeys.token);
343
+ await addSentryCliRc(authToken);
353
344
 
354
345
  const mightBeUsingVercel = fs.existsSync(
355
346
  path.join(process.cwd(), 'vercel.json'),
@@ -5,14 +5,12 @@ import * as Sentry from '@sentry/node';
5
5
 
6
6
  import {
7
7
  abortIfCancelled,
8
- askForProjectSelection,
9
- askForSelfHosted,
10
- askForWizardLogin,
11
8
  confirmContinueEvenThoughNoGitRepo,
12
9
  detectPackageManager,
13
10
  SENTRY_DOT_ENV_FILE,
14
11
  printWelcome,
15
12
  SENTRY_CLI_RC_FILE,
13
+ getOrAskForProjectData,
16
14
  } from '../utils/clack-utils';
17
15
  import { isUnicodeSupported } from '../utils/vendor/is-unicorn-supported';
18
16
  import { SourceMapUploadToolConfigurationOptions } from './tools/types';
@@ -30,6 +28,7 @@ import { URL } from 'url';
30
28
  import { checkIfMoreSuitableWizardExistsAndAskForRedirect } from './utils/other-wizards';
31
29
  import { configureAngularSourcemapGenerationFlow } from './tools/angular';
32
30
  import { detectUsedTool, SupportedTools } from './utils/detect-tool';
31
+ import { configureNextJsSourceMapsUpload } from './tools/nextjs';
33
32
 
34
33
  export async function runSourcemapsWizard(
35
34
  options: WizardOptions,
@@ -73,37 +72,37 @@ You can turn this off by running the wizard with the '--disable-telemetry' flag.
73
72
 
74
73
  await traceStep('check-sdk-version', ensureMinimumSdkVersionIsInstalled);
75
74
 
76
- const { url: sentryUrl, selfHosted } = await traceStep(
77
- 'ask-self-hosted',
78
- () => askForSelfHosted(options.url),
79
- );
80
-
81
- const { projects, apiKeys } = await traceStep('login', () =>
82
- askForWizardLogin({
83
- promoCode: options.promoCode,
84
- url: sentryUrl,
85
- }),
86
- );
75
+ const { selfHosted, selectedProject, sentryUrl, authToken } =
76
+ await getOrAskForProjectData(options);
87
77
 
88
- const selectedProject = await traceStep('select-project', () =>
89
- askForProjectSelection(projects),
90
- );
78
+ const wizardOptionsWithPreSelectedProject = {
79
+ ...options,
80
+ preSelectedProject: {
81
+ project: selectedProject,
82
+ authToken,
83
+ selfHosted,
84
+ },
85
+ };
91
86
 
92
87
  const selectedTool = await traceStep('select-tool', askForUsedBundlerTool);
93
88
 
94
89
  Sentry.setTag('selected-tool', selectedTool);
95
90
 
96
91
  await traceStep('tool-setup', () =>
97
- startToolSetupFlow(selectedTool, {
98
- selfHosted,
99
- orgSlug: selectedProject.organization.slug,
100
- projectSlug: selectedProject.slug,
101
- url: sentryUrl,
102
- authToken: apiKeys.token,
103
- }),
92
+ startToolSetupFlow(
93
+ selectedTool,
94
+ {
95
+ orgSlug: selectedProject.organization.slug,
96
+ projectSlug: selectedProject.slug,
97
+ selfHosted,
98
+ url: sentryUrl,
99
+ authToken,
100
+ },
101
+ wizardOptionsWithPreSelectedProject,
102
+ ),
104
103
  );
105
104
 
106
- await traceStep('ci-setup', () => configureCI(selectedTool, apiKeys.token));
105
+ await traceStep('ci-setup', () => configureCI(selectedTool, authToken));
107
106
 
108
107
  traceStep('outro', () =>
109
108
  printOutro(
@@ -129,6 +128,11 @@ async function askForUsedBundlerTool(): Promise<SupportedTools> {
129
128
  value: 'create-react-app',
130
129
  hint: 'Select this option if you set up your app with Create React App.',
131
130
  },
131
+ {
132
+ label: 'Next.js',
133
+ value: 'nextjs',
134
+ hint: 'Select this option if you want to set up source maps in a NextJS project.',
135
+ },
132
136
  {
133
137
  label: 'Webpack',
134
138
  value: 'webpack',
@@ -170,6 +174,7 @@ async function askForUsedBundlerTool(): Promise<SupportedTools> {
170
174
  async function startToolSetupFlow(
171
175
  selctedTool: SupportedTools,
172
176
  options: SourceMapUploadToolConfigurationOptions,
177
+ wizardOptions: WizardOptions,
173
178
  ): Promise<void> {
174
179
  switch (selctedTool) {
175
180
  case 'webpack':
@@ -196,6 +201,9 @@ async function startToolSetupFlow(
196
201
  configureAngularSourcemapGenerationFlow,
197
202
  );
198
203
  break;
204
+ case 'nextjs':
205
+ await configureNextJsSourceMapsUpload(options, wizardOptions);
206
+ break;
199
207
  default:
200
208
  await configureSentryCLI(options);
201
209
  break;
@@ -234,9 +242,13 @@ async function configureCI(
234
242
  'create-react-app',
235
243
  ].includes(selectedTool);
236
244
 
237
- const authTokenFile = isCliBasedFlowTool
238
- ? SENTRY_CLI_RC_FILE
239
- : SENTRY_DOT_ENV_FILE;
245
+ // some non-cli-based flows also use the .sentryclirc file
246
+ const usesSentryCliRc = selectedTool === 'nextjs';
247
+
248
+ const authTokenFile =
249
+ isCliBasedFlowTool || usesSentryCliRc
250
+ ? SENTRY_CLI_RC_FILE
251
+ : SENTRY_DOT_ENV_FILE;
240
252
 
241
253
  if (!isUsingCI) {
242
254
  clack.log.info(
@@ -0,0 +1,114 @@
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 { runNextjsWizard } from '../../nextjs/nextjs-wizard';
5
+ import { traceStep } from '../../telemetry';
6
+ import { abortIfCancelled, addSentryCliRc } from '../../utils/clack-utils';
7
+ import { WizardOptions } from '../../utils/types';
8
+
9
+ import { SourceMapUploadToolConfigurationOptions } from './types';
10
+
11
+ import * as Sentry from '@sentry/node';
12
+
13
+ const getCodeSnippet = (options: SourceMapUploadToolConfigurationOptions) =>
14
+ chalk.grey(`
15
+ ${chalk.greenBright(
16
+ 'const { withSentryConfig } = require("@sentry/nextjs");',
17
+ )}
18
+
19
+ const nextConfig = {
20
+ // your existing next config
21
+ };
22
+
23
+ ${chalk.greenBright(`const sentryWebpackPluginOptions = {
24
+ org: "${options.orgSlug}",
25
+ project: "${options.projectSlug}",${
26
+ options.selfHosted ? `\n url: "${options.url}",` : ''
27
+ }
28
+ };`)}
29
+
30
+ ${chalk.greenBright(`const sentryOptions = {
31
+ // Upload additional client files (increases upload size)
32
+ widenClientFileUpload: true,
33
+
34
+ // Hides source maps from generated client bundles
35
+ hideSourceMaps: true,
36
+ };`)}
37
+
38
+ ${chalk.greenBright(`module.exports = withSentryConfig(
39
+ nextConfig,
40
+ sentryWebpackPluginOptions,
41
+ sentryOptions
42
+ );`)}
43
+ `);
44
+
45
+ export const configureNextJsSourceMapsUpload = async (
46
+ options: SourceMapUploadToolConfigurationOptions,
47
+ wizardOptions: WizardOptions,
48
+ ) => {
49
+ clack.log
50
+ .info(`Source Maps upload for Next.js is configured automatically by default if you run the Sentry Wizard for Next.JS.
51
+ But don't worry, we can redirect you to the wizard now!
52
+
53
+ In case you already tried the wizard, we can also show you how to configure your ${chalk.cyan(
54
+ 'next.config.js',
55
+ )} file manually instead.`);
56
+
57
+ const shouldRedirect: boolean = await abortIfCancelled(
58
+ clack.select({
59
+ message: 'Do you want to run the Sentry Wizard for Next.JS now?',
60
+ options: [
61
+ {
62
+ label: 'Yes, run the wizard!',
63
+ value: true,
64
+ hint: 'The wizard can also configure your SDK setup',
65
+ },
66
+ {
67
+ label: 'No, show me how to configure it manually',
68
+ value: false,
69
+ },
70
+ ],
71
+ }),
72
+ );
73
+
74
+ Sentry.setTag('redirect-nextjs-wizard', shouldRedirect);
75
+
76
+ if (shouldRedirect) {
77
+ await traceStep('run-nextjs-wizard', () => runNextjsWizard(wizardOptions));
78
+ clack.intro('Sentry Source Maps Upload Configuration Wizard');
79
+ clack.log.info(
80
+ "Welcome back to the Source Maps wizard - we're almost done ;)",
81
+ );
82
+ } else {
83
+ clack.log.info(
84
+ `Add the following code to your ${chalk.cyan('next.config.js')}:`,
85
+ );
86
+
87
+ // Intentionally logging directly to console here
88
+ // eslint-disable-next-line no-console
89
+ console.log(getCodeSnippet(options));
90
+
91
+ await traceStep('nextjs-manual-nextconfigjs', () =>
92
+ abortIfCancelled(
93
+ clack.select({
94
+ message: 'Did you copy the code above?',
95
+ options: [{ label: 'Yes, continue!', value: true }],
96
+ initialValue: true,
97
+ }),
98
+ ),
99
+ );
100
+
101
+ await traceStep('nextjs-manual-sentryclirc', () =>
102
+ addSentryCliRc(options.authToken),
103
+ );
104
+ }
105
+
106
+ clack.log
107
+ .info(`In case you still run into problems, check out our docs to further debug your setup:
108
+
109
+ Uploading Source Maps:
110
+ https://docs.sentry.io/platforms/javascript/guides/nextjs/manual-setup/#configure-source-maps
111
+
112
+ Troubleshooting Source Maps:
113
+ https://docs.sentry.io/platforms/javascript/guides/nextjs/troubleshooting/`);
114
+ };
@@ -7,14 +7,18 @@ import * as fs from 'fs';
7
7
  import {
8
8
  abortIfCancelled,
9
9
  addSentryCliRc,
10
+ detectPackageManager,
10
11
  getPackageDotJson,
11
12
  installPackage,
12
13
  } from '../../utils/clack-utils';
13
14
 
14
15
  import { SourceMapUploadToolConfigurationOptions } from './types';
15
16
  import { hasPackageInstalled, PackageDotJson } from '../../utils/package-json';
17
+ import { traceStep } from '../../telemetry';
16
18
 
17
- const NPM_SCRIPT_NAME = 'sentry:sourcemaps';
19
+ const SENTRY_NPM_SCRIPT_NAME = 'sentry:sourcemaps';
20
+
21
+ let addedToBuildCommand = false;
18
22
 
19
23
  export async function configureSentryCLI(
20
24
  options: SourceMapUploadToolConfigurationOptions,
@@ -80,14 +84,32 @@ export async function configureSentryCLI(
80
84
  relativePosixArtifactPath,
81
85
  );
82
86
 
87
+ if (await askShouldAddToBuildCommand()) {
88
+ await traceStep('sentry-cli-add-to-build-cmd', () =>
89
+ addSentryCommandToBuildCommand(packageDotJson),
90
+ );
91
+ } else {
92
+ clack.log.info(
93
+ `No problem, just make sure to run this script ${chalk.bold(
94
+ 'after',
95
+ )} building your application but ${chalk.bold('before')} deploying!`,
96
+ );
97
+ }
98
+
83
99
  await addSentryCliRc(options.authToken);
84
100
  }
85
101
 
86
102
  export async function setupNpmScriptInCI(): Promise<void> {
103
+ if (addedToBuildCommand) {
104
+ // No need to tell users to add it manually to their CI
105
+ // if the script is already added to the build command
106
+ return;
107
+ }
108
+
87
109
  const addedToCI = await abortIfCancelled(
88
110
  clack.select({
89
111
  message: `Add a step to your CI pipeline that runs the ${chalk.cyan(
90
- NPM_SCRIPT_NAME,
112
+ SENTRY_NPM_SCRIPT_NAME,
91
113
  )} script ${chalk.bold('right after')} building your application.`,
92
114
  options: [
93
115
  { label: 'I did, continue!', value: true },
@@ -95,7 +117,9 @@ export async function setupNpmScriptInCI(): Promise<void> {
95
117
  label: "I'll do it later...",
96
118
  value: false,
97
119
  hint: chalk.yellow(
98
- 'You need to run the command after each build for source maps to work properly.',
120
+ `You need to run ${chalk.cyan(
121
+ SENTRY_NPM_SCRIPT_NAME,
122
+ )} after each build for source maps to work properly.`,
99
123
  ),
100
124
  },
101
125
  ],
@@ -126,7 +150,7 @@ async function createAndAddNpmScript(
126
150
  } ${relativePosixArtifactPath}`;
127
151
 
128
152
  packageDotJson.scripts = packageDotJson.scripts || {};
129
- packageDotJson.scripts[NPM_SCRIPT_NAME] = sentryCliNpmScript;
153
+ packageDotJson.scripts[SENTRY_NPM_SCRIPT_NAME] = sentryCliNpmScript;
130
154
 
131
155
  await fs.promises.writeFile(
132
156
  path.join(process.cwd(), 'package.json'),
@@ -134,11 +158,113 @@ async function createAndAddNpmScript(
134
158
  );
135
159
 
136
160
  clack.log.info(
137
- `Added a ${chalk.cyan(NPM_SCRIPT_NAME)} script to your ${chalk.cyan(
161
+ `Added a ${chalk.cyan(SENTRY_NPM_SCRIPT_NAME)} script to your ${chalk.cyan(
138
162
  'package.json',
139
- )}. Make sure to run this script ${chalk.bold(
140
- 'after',
141
- )} building your application but ${chalk.bold('before')} deploying!`,
163
+ )}.`,
164
+ );
165
+ }
166
+
167
+ async function askShouldAddToBuildCommand(): Promise<boolean> {
168
+ const shouldAddToBuildCommand = await abortIfCancelled(
169
+ clack.select({
170
+ message: `Do you want to automatically run the ${chalk.cyan(
171
+ SENTRY_NPM_SCRIPT_NAME,
172
+ )} script after each production build?`,
173
+ options: [
174
+ {
175
+ label: 'Yes',
176
+ value: true,
177
+ hint: 'This will modify your prod build command',
178
+ },
179
+ { label: 'No', value: false },
180
+ ],
181
+ initialValue: true,
182
+ }),
183
+ );
184
+
185
+ Sentry.setTag('modify-build-command', shouldAddToBuildCommand);
186
+
187
+ return shouldAddToBuildCommand;
188
+ }
189
+
190
+ /**
191
+ * Add the sentry:sourcemaps command to the prod build command in the package.json
192
+ * - Detect the user's build command
193
+ * - Append the sentry:sourcemaps command to it
194
+ *
195
+ * @param packageDotJson The package.json which will be modified.
196
+ */
197
+ async function addSentryCommandToBuildCommand(
198
+ packageDotJson: PackageDotJson,
199
+ ): Promise<void> {
200
+ // This usually shouldn't happen because earlier we added the
201
+ // SENTRY_NPM_SCRIPT_NAME script but just to be sure
202
+ packageDotJson.scripts = packageDotJson.scripts || {};
203
+
204
+ const allNpmScripts = Object.keys(packageDotJson.scripts).filter(
205
+ (s) => s !== SENTRY_NPM_SCRIPT_NAME,
206
+ );
207
+
208
+ const pacMan = detectPackageManager() || 'npm';
209
+
210
+ // Heuristic to pre-select the build command:
211
+ // Often, 'build' is the prod build command, so we favour it.
212
+ // If it's not there, commands that include 'build' might be the prod build command.
213
+ let buildCommand =
214
+ packageDotJson.scripts.build ||
215
+ allNpmScripts.find((s) => s.toLocaleLowerCase().includes('build'));
216
+
217
+ const isProdBuildCommand =
218
+ !!buildCommand &&
219
+ (await abortIfCancelled(
220
+ clack.confirm({
221
+ message: `Is ${chalk.cyan(
222
+ `${pacMan} run ${buildCommand}`,
223
+ )} your production build command?`,
224
+ }),
225
+ ));
226
+
227
+ if (allNpmScripts.length && (!buildCommand || !isProdBuildCommand)) {
228
+ buildCommand = await abortIfCancelled(
229
+ clack.select({
230
+ message: `Which ${pacMan} command in your ${chalk.cyan(
231
+ 'package.json',
232
+ )} builds your application for production?`,
233
+ options: allNpmScripts
234
+ .map((script) => ({
235
+ label: script,
236
+ value: script,
237
+ }))
238
+ .concat({ label: 'None of the above', value: 'none' }),
239
+ }),
240
+ );
241
+ }
242
+
243
+ if (!buildCommand || buildCommand === 'none') {
244
+ clack.log.warn(
245
+ `We can only add the ${chalk.cyan(
246
+ SENTRY_NPM_SCRIPT_NAME,
247
+ )} script to another \`script\` in your ${chalk.cyan('package.json')}.
248
+ Please add it manually to your prod build command.`,
249
+ );
250
+ return;
251
+ }
252
+
253
+ packageDotJson.scripts[
254
+ buildCommand
255
+ ] = `${buildCommand} && ${pacMan} run ${SENTRY_NPM_SCRIPT_NAME}`;
256
+
257
+ await fs.promises.writeFile(
258
+ path.join(process.cwd(), 'package.json'),
259
+ JSON.stringify(packageDotJson, null, 2),
260
+ );
261
+
262
+ addedToBuildCommand = true;
263
+
264
+ clack.log.info(
265
+ `Added ${chalk.cyan(SENTRY_NPM_SCRIPT_NAME)} script to your ${chalk.cyan(
266
+ buildCommand,
267
+ )} command.`,
142
268
  );
143
269
  }
144
270
 
@@ -1,5 +1,12 @@
1
1
  // @ts-ignore - clack is ESM and TS complains about that. It works though
2
2
  import clack, { select } from '@clack/prompts';
3
+ // @ts-ignore - magicast is ESM and TS complains about that. It works though
4
+ import { generateCode, parseModule } from 'magicast';
5
+ // @ts-ignore - magicast is ESM and TS complains about that. It works though
6
+ import { addVitePlugin } from 'magicast/helpers';
7
+
8
+ import * as Sentry from '@sentry/node';
9
+
3
10
  import chalk from 'chalk';
4
11
  import {
5
12
  abortIfCancelled,
@@ -13,6 +20,11 @@ import {
13
20
  SourceMapUploadToolConfigurationFunction,
14
21
  SourceMapUploadToolConfigurationOptions,
15
22
  } from './types';
23
+ import { findScriptFile, hasSentryContent } from '../../utils/ast-utils';
24
+
25
+ import * as path from 'path';
26
+ import * as fs from 'fs';
27
+ import { debug } from '../../utils/debug';
16
28
 
17
29
  const getCodeSnippet = (options: SourceMapUploadToolConfigurationOptions) =>
18
30
  chalk.gray(`
@@ -48,21 +60,98 @@ export const configureVitePlugin: SourceMapUploadToolConfigurationFunction =
48
60
  ),
49
61
  });
50
62
 
51
- clack.log.step(
52
- `Add the following code to your ${chalk.bold('vite.config.js')} file:`,
63
+ const viteConfigPath = findScriptFile(
64
+ path.resolve(process.cwd(), 'vite.config'),
53
65
  );
54
66
 
55
- // Intentially logging directly to console here so that the code can be copied/pasted directly
56
- // eslint-disable-next-line no-console
57
- console.log(getCodeSnippet(options));
67
+ let successfullyAdded = false;
68
+ if (viteConfigPath) {
69
+ successfullyAdded = await addVitePluginToConfig(viteConfigPath, options);
70
+ } else {
71
+ Sentry.setTag('ast-mod-fail-reason', 'config-not-found');
72
+ }
58
73
 
59
- await abortIfCancelled(
60
- select({
61
- message: 'Did you copy the snippet above?',
62
- options: [{ label: 'Yes, continue!', value: true }],
63
- initialValue: true,
64
- }),
65
- );
74
+ if (successfullyAdded) {
75
+ Sentry.setTag('ast-mod', 'success');
76
+ } else {
77
+ Sentry.setTag('ast-mod', 'fail');
78
+ await showCopyPasteInstructions(
79
+ path.basename(viteConfigPath || 'vite.config.js'),
80
+ options,
81
+ );
82
+ }
66
83
 
67
84
  await addDotEnvSentryBuildPluginFile(options.authToken);
68
85
  };
86
+
87
+ async function addVitePluginToConfig(
88
+ viteConfigPath: string,
89
+ options: SourceMapUploadToolConfigurationOptions,
90
+ ): Promise<boolean> {
91
+ try {
92
+ const prettyViteConfigFilename = chalk.cyan(path.basename(viteConfigPath));
93
+
94
+ const viteConfigContent = (
95
+ await fs.promises.readFile(viteConfigPath)
96
+ ).toString();
97
+
98
+ const mod = parseModule(viteConfigContent);
99
+
100
+ if (hasSentryContent(mod)) {
101
+ clack.log.warn(
102
+ `File ${prettyViteConfigFilename} already contains Sentry code.
103
+ Please follow the instruction below`,
104
+ );
105
+ Sentry.setTag('ast-mod-fail-reason', 'has-sentry-content');
106
+ return false;
107
+ }
108
+
109
+ const { orgSlug: org, projectSlug: project, selfHosted, url } = options;
110
+
111
+ addVitePlugin(mod, {
112
+ imported: 'sentryVitePlugin',
113
+ from: '@sentry/vite-plugin',
114
+ constructor: 'sentryVitePlugin',
115
+ options: {
116
+ org,
117
+ project,
118
+ ...(selfHosted && { url }),
119
+ },
120
+ });
121
+
122
+ const code = generateCode(mod.$ast).code;
123
+
124
+ await fs.promises.writeFile(viteConfigPath, code);
125
+
126
+ clack.log.success(
127
+ `Added the Sentry Vite plugin to ${prettyViteConfigFilename}`,
128
+ );
129
+
130
+ return true;
131
+ } catch (e) {
132
+ debug(e);
133
+ Sentry.setTag('ast-mod-fail-reason', 'insertion-fail');
134
+ return false;
135
+ }
136
+ }
137
+
138
+ async function showCopyPasteInstructions(
139
+ viteConfigFilename: string,
140
+ options: SourceMapUploadToolConfigurationOptions,
141
+ ) {
142
+ clack.log.step(
143
+ `Add the following code to your ${chalk.cyan(viteConfigFilename)} file:`,
144
+ );
145
+
146
+ // Intentionally logging directly to console here so that the code can be copied/pasted directly
147
+ // eslint-disable-next-line no-console
148
+ console.log(getCodeSnippet(options));
149
+
150
+ await abortIfCancelled(
151
+ select({
152
+ message: 'Did you copy the snippet above?',
153
+ options: [{ label: 'Yes, continue!', value: true }],
154
+ initialValue: true,
155
+ }),
156
+ );
157
+ }
@@ -9,7 +9,8 @@ export type SupportedTools =
9
9
  | 'tsc'
10
10
  | 'sentry-cli'
11
11
  | 'create-react-app'
12
- | 'angular';
12
+ | 'angular'
13
+ | 'nextjs';
13
14
 
14
15
  // A map of package names pointing to the tool slug.
15
16
  // The order is important, because we want to detect the most specific tool first.
@@ -18,6 +19,7 @@ export type SupportedTools =
18
19
  export const TOOL_PACKAGE_MAP: Record<string, SupportedTools> = {
19
20
  '@angular/core': 'angular',
20
21
  'create-react-app': 'create-react-app',
22
+ next: 'nextjs',
21
23
  webpack: 'webpack',
22
24
  vite: 'vite',
23
25
  esbuild: 'esbuild',