UNPKG

@sentry/wizard

Version:

Sentry wizard helping you to configure your project

643 lines 52.7 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (g && (g = 0, op[0] && (_ = 0)), _) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { if (ar || !(i in from)) { if (!ar) ar = Array.prototype.slice.call(from, 0, i); ar[i] = from[i]; } } return to.concat(ar || Array.prototype.slice.call(from)); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.runNextjsWizardWithTelemetry = exports.runNextjsWizard = void 0; /* eslint-disable max-lines */ // @ts-ignore - clack is ESM and TS complains about that. It works though var prompts_1 = __importDefault(require("@clack/prompts")); var chalk_1 = __importDefault(require("chalk")); var fs = __importStar(require("fs")); // @ts-ignore - magicast is ESM and TS complains about that. It works though var magicast_1 = require("magicast"); var path = __importStar(require("path")); var Sentry = __importStar(require("@sentry/node")); var clack_utils_1 = require("../utils/clack-utils"); var templates_1 = require("./templates"); var telemetry_1 = require("../telemetry"); var package_json_1 = require("../utils/package-json"); var utils_1 = require("./utils"); var sourcemaps_wizard_1 = require("../sourcemaps/sourcemaps-wizard"); function runNextjsWizard(options) { return (0, telemetry_1.withTelemetry)({ enabled: options.telemetryEnabled, integration: 'nextjs', }, function () { return runNextjsWizardWithTelemetry(options); }); } exports.runNextjsWizard = runNextjsWizard; function runNextjsWizardWithTelemetry(options) { var _a; return __awaiter(this, void 0, void 0, function () { var typeScriptDetected, packageJson, nextVersion, _b, selectedProject, authToken, selfHosted, sentryUrl, sdkAlreadyInstalled, shouldCreateExamplePage, mightBeUsingVercel; var _this = this; return __generator(this, function (_c) { switch (_c.label) { case 0: (0, clack_utils_1.printWelcome)({ wizardName: 'Sentry Next.js Wizard', promoCode: options.promoCode, telemetryEnabled: options.telemetryEnabled, }); typeScriptDetected = (0, clack_utils_1.isUsingTypeScript)(); return [4 /*yield*/, (0, clack_utils_1.confirmContinueIfNoOrDirtyGitRepo)()]; case 1: _c.sent(); return [4 /*yield*/, (0, clack_utils_1.getPackageDotJson)()]; case 2: packageJson = _c.sent(); return [4 /*yield*/, (0, clack_utils_1.ensurePackageIsInstalled)(packageJson, 'next', 'Next.js')]; case 3: _c.sent(); nextVersion = (0, package_json_1.getPackageVersion)('next', packageJson); Sentry.setTag('nextjs-version', (0, utils_1.getNextJsVersionBucket)(nextVersion)); return [4 /*yield*/, (0, clack_utils_1.getOrAskForProjectData)(options, 'javascript-nextjs')]; case 4: _b = _c.sent(), selectedProject = _b.selectedProject, authToken = _b.authToken, selfHosted = _b.selfHosted, sentryUrl = _b.sentryUrl; sdkAlreadyInstalled = (0, package_json_1.hasPackageInstalled)('@sentry/nextjs', packageJson); Sentry.setTag('sdk-already-installed', sdkAlreadyInstalled); return [4 /*yield*/, (0, clack_utils_1.installPackage)({ packageName: '@sentry/nextjs@^8', alreadyInstalled: !!((_a = packageJson === null || packageJson === void 0 ? void 0 : packageJson.dependencies) === null || _a === void 0 ? void 0 : _a['@sentry/nextjs']), })]; case 5: _c.sent(); return [4 /*yield*/, (0, telemetry_1.traceStep)('configure-sdk', function () { return __awaiter(_this, void 0, void 0, function () { var tunnelRoute; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, askShouldSetTunnelRoute()]; case 1: tunnelRoute = _a.sent(); return [4 /*yield*/, createOrMergeNextJsFiles(selectedProject, selfHosted, sentryUrl, { tunnelRoute: tunnelRoute, })]; case 2: _a.sent(); return [2 /*return*/]; } }); }); })]; case 6: _c.sent(); return [4 /*yield*/, (0, telemetry_1.traceStep)('create-underscoreerror-page', function () { return __awaiter(_this, void 0, void 0, function () { var srcDir, maybePagesDirPath, maybeSrcPagesDirPath, pagesLocation, underscoreErrorPageFile, shouldContinue, shouldContinue; return __generator(this, function (_a) { switch (_a.label) { case 0: srcDir = path.join(process.cwd(), 'src'); maybePagesDirPath = path.join(process.cwd(), 'pages'); maybeSrcPagesDirPath = path.join(srcDir, 'pages'); pagesLocation = fs.existsSync(maybePagesDirPath) && fs.lstatSync(maybePagesDirPath).isDirectory() ? ['pages'] : fs.existsSync(maybeSrcPagesDirPath) && fs.lstatSync(maybeSrcPagesDirPath).isDirectory() ? ['src', 'pages'] : undefined; if (!pagesLocation) { return [2 /*return*/]; } underscoreErrorPageFile = fs.existsSync(path.join.apply(path, __spreadArray(__spreadArray([process.cwd()], pagesLocation, false), ['_error.tsx'], false))) ? '_error.tsx' : fs.existsSync(path.join.apply(path, __spreadArray(__spreadArray([process.cwd()], pagesLocation, false), ['_error.ts'], false))) ? '_error.ts' : fs.existsSync(path.join.apply(path, __spreadArray(__spreadArray([process.cwd()], pagesLocation, false), ['_error.jsx'], false))) ? '_error.jsx' : fs.existsSync(path.join.apply(path, __spreadArray(__spreadArray([process.cwd()], pagesLocation, false), ['_error.js'], false))) ? '_error.js' : undefined; if (!!underscoreErrorPageFile) return [3 /*break*/, 2]; return [4 /*yield*/, fs.promises.writeFile(path.join.apply(path, __spreadArray(__spreadArray([process.cwd()], pagesLocation, false), ['_error.jsx'], false)), (0, templates_1.getSentryDefaultUnderscoreErrorPage)(), { encoding: 'utf8', flag: 'w' })]; case 1: _a.sent(); prompts_1.default.log.success("Created ".concat(chalk_1.default.cyan(path.join.apply(path, __spreadArray(__spreadArray([], pagesLocation, false), ['_error.jsx'], false))), ".")); return [3 /*break*/, 9]; case 2: if (!fs .readFileSync(path.join.apply(path, __spreadArray(__spreadArray([process.cwd()], pagesLocation, false), [underscoreErrorPageFile], false)), 'utf8') .includes('getInitialProps')) return [3 /*break*/, 6]; prompts_1.default.log.info("It seems like you already have a custom error page.\n\nPlease put the following function call in the ".concat(chalk_1.default.bold('getInitialProps'), "\nmethod of your custom error page at ").concat(chalk_1.default.bold(path.join.apply(path, __spreadArray(__spreadArray([], pagesLocation, false), [underscoreErrorPageFile], false))), ":")); // eslint-disable-next-line no-console console.log((0, templates_1.getSimpleUnderscoreErrorCopyPasteSnippet)()); return [4 /*yield*/, (0, clack_utils_1.abortIfCancelled)(prompts_1.default.confirm({ message: "Did you modify your ".concat(chalk_1.default.cyan(path.join.apply(path, __spreadArray(__spreadArray([], pagesLocation, false), [underscoreErrorPageFile], false))), " file as described above?"), active: 'Yes', inactive: 'No, get me out of here', }))]; case 3: shouldContinue = _a.sent(); if (!!shouldContinue) return [3 /*break*/, 5]; return [4 /*yield*/, (0, clack_utils_1.abort)()]; case 4: _a.sent(); _a.label = 5; case 5: return [3 /*break*/, 9]; case 6: prompts_1.default.log.info("It seems like you already have a custom error page.\n\nPlease add the following code to your custom error page\nat ".concat(chalk_1.default.cyan(path.join.apply(path, __spreadArray(__spreadArray([], pagesLocation, false), [underscoreErrorPageFile], false))), ":")); // eslint-disable-next-line no-console console.log((0, templates_1.getFullUnderscoreErrorCopyPasteSnippet)(underscoreErrorPageFile === '_error.ts' || underscoreErrorPageFile === '_error.tsx')); return [4 /*yield*/, (0, clack_utils_1.abortIfCancelled)(prompts_1.default.confirm({ message: "Did add the code to your ".concat(chalk_1.default.cyan(path.join.apply(path, __spreadArray(__spreadArray([], pagesLocation, false), [underscoreErrorPageFile], false))), " file as described above?"), active: 'Yes', inactive: 'No, get me out of here', }))]; case 7: shouldContinue = _a.sent(); if (!!shouldContinue) return [3 /*break*/, 9]; return [4 /*yield*/, (0, clack_utils_1.abort)()]; case 8: _a.sent(); _a.label = 9; case 9: return [2 /*return*/]; } }); }); })]; case 7: _c.sent(); return [4 /*yield*/, (0, telemetry_1.traceStep)('create-global-error-page', function () { return __awaiter(_this, void 0, void 0, function () { var maybeAppDirPath, maybeSrcAppDirPath, appDirLocation, globalErrorPageFile, newGlobalErrorFileName, shouldContinue; return __generator(this, function (_a) { switch (_a.label) { case 0: maybeAppDirPath = path.join(process.cwd(), 'app'); maybeSrcAppDirPath = path.join(process.cwd(), 'src', 'app'); appDirLocation = fs.existsSync(maybeAppDirPath) && fs.lstatSync(maybeAppDirPath).isDirectory() ? ['app'] : fs.existsSync(maybeSrcAppDirPath) && fs.lstatSync(maybeSrcAppDirPath).isDirectory() ? ['src', 'app'] : undefined; if (!appDirLocation) { return [2 /*return*/]; } globalErrorPageFile = fs.existsSync(path.join.apply(path, __spreadArray(__spreadArray([process.cwd()], appDirLocation, false), ['global-error.tsx'], false))) ? 'global-error.tsx' : fs.existsSync(path.join.apply(path, __spreadArray(__spreadArray([process.cwd()], appDirLocation, false), ['global-error.ts'], false))) ? 'global-error.ts' : fs.existsSync(path.join.apply(path, __spreadArray(__spreadArray([process.cwd()], appDirLocation, false), ['global-error.jsx'], false))) ? 'global-error.jsx' : fs.existsSync(path.join.apply(path, __spreadArray(__spreadArray([process.cwd()], appDirLocation, false), ['global-error.js'], false))) ? 'global-error.js' : undefined; if (!!globalErrorPageFile) return [3 /*break*/, 2]; newGlobalErrorFileName = "global-error.".concat(typeScriptDetected ? 'tsx' : 'jsx'); return [4 /*yield*/, fs.promises.writeFile(path.join.apply(path, __spreadArray(__spreadArray([process.cwd()], appDirLocation, false), [newGlobalErrorFileName], false)), (0, templates_1.getSentryDefaultGlobalErrorPage)(typeScriptDetected), { encoding: 'utf8', flag: 'w' })]; case 1: _a.sent(); prompts_1.default.log.success("Created ".concat(chalk_1.default.cyan(path.join.apply(path, __spreadArray(__spreadArray([], appDirLocation, false), [newGlobalErrorFileName], false))), ".")); return [3 /*break*/, 5]; case 2: prompts_1.default.log.info("It seems like you already have a custom error page for your app directory.\n\nPlease add the following code to your custom error page\nat ".concat(chalk_1.default.cyan(path.join.apply(path, __spreadArray(__spreadArray([], appDirLocation, false), [globalErrorPageFile], false))), ":\n")); // eslint-disable-next-line no-console console.log((0, templates_1.getGlobalErrorCopyPasteSnippet)(globalErrorPageFile === 'global-error.ts' || globalErrorPageFile === 'global-error.tsx')); return [4 /*yield*/, (0, clack_utils_1.abortIfCancelled)(prompts_1.default.confirm({ message: "Did add the code to your ".concat(chalk_1.default.cyan(path.join.apply(path, __spreadArray(__spreadArray([], appDirLocation, false), [globalErrorPageFile], false))), " file as described above?"), active: 'Yes', inactive: 'No, get me out of here', }))]; case 3: shouldContinue = _a.sent(); if (!!shouldContinue) return [3 /*break*/, 5]; return [4 /*yield*/, (0, clack_utils_1.abort)()]; case 4: _a.sent(); _a.label = 5; case 5: return [2 /*return*/]; } }); }); })]; case 8: _c.sent(); return [4 /*yield*/, (0, clack_utils_1.askShouldCreateExamplePage)()]; case 9: shouldCreateExamplePage = _c.sent(); if (!shouldCreateExamplePage) return [3 /*break*/, 11]; return [4 /*yield*/, (0, telemetry_1.traceStep)('create-example-page', function () { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, createExamplePage(selfHosted, selectedProject, sentryUrl)]; }); }); })]; case 10: _c.sent(); _c.label = 11; case 11: return [4 /*yield*/, (0, clack_utils_1.addDotEnvSentryBuildPluginFile)(authToken)]; case 12: _c.sent(); mightBeUsingVercel = fs.existsSync(path.join(process.cwd(), 'vercel.json')); if (!mightBeUsingVercel) return [3 /*break*/, 13]; prompts_1.default.log.info("▲ It seems like you're using Vercel. We recommend using the Sentry Vercel integration to set up an auth token for Vercel deployments: https://vercel.com/integrations/sentry"); return [3 /*break*/, 15]; case 13: return [4 /*yield*/, (0, telemetry_1.traceStep)('configure-ci', function () { return (0, sourcemaps_wizard_1.configureCI)('nextjs', authToken); })]; case 14: _c.sent(); _c.label = 15; case 15: prompts_1.default.outro("\n".concat(chalk_1.default.green('Successfully installed the Sentry Next.js SDK!'), " ").concat(shouldCreateExamplePage ? "\n\nYou can validate your setup by restarting your dev environment (".concat(chalk_1.default.cyan("next dev"), ") and visiting ").concat(chalk_1.default.cyan('"/sentry-example-page"')) : '', "\n\n").concat(chalk_1.default.dim('If you encounter any issues, let us know here: https://github.com/getsentry/sentry-javascript/issues'))); return [2 /*return*/]; } }); }); } exports.runNextjsWizardWithTelemetry = runNextjsWizardWithTelemetry; function createOrMergeNextJsFiles(selectedProject, selfHosted, sentryUrl, sdkConfigOptions) { return __awaiter(this, void 0, void 0, function () { var typeScriptDetected, configVariants, _loop_1, _i, configVariants_1, configVariant; var _this = this; return __generator(this, function (_a) { switch (_a.label) { case 0: typeScriptDetected = (0, clack_utils_1.isUsingTypeScript)(); configVariants = ['server', 'client', 'edge']; _loop_1 = function (configVariant) { return __generator(this, function (_b) { switch (_b.label) { case 0: return [4 /*yield*/, (0, telemetry_1.traceStep)("create-sentry-".concat(configVariant, "-config"), function () { return __awaiter(_this, void 0, void 0, function () { var jsConfig, tsConfig, jsConfigExists, tsConfigExists, shouldWriteFile, existingConfigs, overwriteExistingConfigs; return __generator(this, function (_a) { switch (_a.label) { case 0: jsConfig = "sentry.".concat(configVariant, ".config.js"); tsConfig = "sentry.".concat(configVariant, ".config.ts"); jsConfigExists = fs.existsSync(path.join(process.cwd(), jsConfig)); tsConfigExists = fs.existsSync(path.join(process.cwd(), tsConfig)); shouldWriteFile = true; if (!(jsConfigExists || tsConfigExists)) return [3 /*break*/, 2]; existingConfigs = []; if (jsConfigExists) { existingConfigs.push(jsConfig); } if (tsConfigExists) { existingConfigs.push(tsConfig); } return [4 /*yield*/, (0, clack_utils_1.abortIfCancelled)(prompts_1.default.confirm({ message: "Found existing Sentry ".concat(configVariant, " config (").concat(existingConfigs.join(', '), "). Overwrite ").concat(existingConfigs.length > 1 ? 'them' : 'it', "?"), }))]; case 1: overwriteExistingConfigs = _a.sent(); Sentry.setTag("overwrite-".concat(configVariant, "-config"), overwriteExistingConfigs); shouldWriteFile = overwriteExistingConfigs; if (overwriteExistingConfigs) { if (jsConfigExists) { fs.unlinkSync(path.join(process.cwd(), jsConfig)); prompts_1.default.log.warn("Removed existing ".concat(chalk_1.default.cyan(jsConfig), ".")); } if (tsConfigExists) { fs.unlinkSync(path.join(process.cwd(), tsConfig)); prompts_1.default.log.warn("Removed existing ".concat(chalk_1.default.cyan(tsConfig), ".")); } } _a.label = 2; case 2: if (!shouldWriteFile) return [3 /*break*/, 4]; return [4 /*yield*/, fs.promises.writeFile(path.join(process.cwd(), typeScriptDetected ? tsConfig : jsConfig), (0, templates_1.getSentryConfigContents)(selectedProject.keys[0].dsn.public, configVariant), { encoding: 'utf8', flag: 'w' })]; case 3: _a.sent(); prompts_1.default.log.success("Created fresh ".concat(chalk_1.default.cyan(typeScriptDetected ? tsConfig : jsConfig), ".")); Sentry.setTag("created-".concat(configVariant, "-config"), true); _a.label = 4; case 4: return [2 /*return*/]; } }); }); })]; case 1: _b.sent(); return [2 /*return*/]; } }); }; _i = 0, configVariants_1 = configVariants; _a.label = 1; case 1: if (!(_i < configVariants_1.length)) return [3 /*break*/, 4]; configVariant = configVariants_1[_i]; return [5 /*yield**/, _loop_1(configVariant)]; case 2: _a.sent(); _a.label = 3; case 3: _i++; return [3 /*break*/, 1]; case 4: return [4 /*yield*/, (0, telemetry_1.traceStep)('setup-instrumentation-hook', function () { return __awaiter(_this, void 0, void 0, function () { var srcInstrumentationTsExists, srcInstrumentationJsExists, instrumentationTsExists, instrumentationJsExists, instrumentationHookLocation, newInstrumentationFileName, srcFolderExists, instrumentationHookPath, successfullyCreated; return __generator(this, function (_a) { switch (_a.label) { case 0: srcInstrumentationTsExists = fs.existsSync(path.join(process.cwd(), 'src', 'instrumentation.ts')); srcInstrumentationJsExists = fs.existsSync(path.join(process.cwd(), 'src', 'instrumentation.js')); instrumentationTsExists = fs.existsSync(path.join(process.cwd(), 'instrumentation.ts')); instrumentationJsExists = fs.existsSync(path.join(process.cwd(), 'instrumentation.js')); if (srcInstrumentationTsExists || srcInstrumentationJsExists) { instrumentationHookLocation = 'src'; } else if (instrumentationTsExists || instrumentationJsExists) { instrumentationHookLocation = 'root'; } else { instrumentationHookLocation = 'does-not-exist'; } if (!(instrumentationHookLocation === 'does-not-exist')) return [3 /*break*/, 4]; newInstrumentationFileName = "instrumentation.".concat(typeScriptDetected ? 'ts' : 'js'); srcFolderExists = fs.existsSync(path.join(process.cwd(), 'src')); instrumentationHookPath = srcFolderExists ? path.join(process.cwd(), 'src', newInstrumentationFileName) : path.join(process.cwd(), newInstrumentationFileName); return [4 /*yield*/, (0, clack_utils_1.createNewConfigFile)(instrumentationHookPath, (0, templates_1.getInstrumentationHookContent)(srcFolderExists ? 'src' : 'root'))]; case 1: successfullyCreated = _a.sent(); if (!!successfullyCreated) return [3 /*break*/, 3]; return [4 /*yield*/, (0, clack_utils_1.showCopyPasteInstructions)(newInstrumentationFileName, (0, templates_1.getInstrumentationHookCopyPasteSnippet)(srcFolderExists ? 'src' : 'root'))]; case 2: _a.sent(); _a.label = 3; case 3: return [3 /*break*/, 6]; case 4: return [4 /*yield*/, (0, clack_utils_1.showCopyPasteInstructions)(srcInstrumentationTsExists ? 'instrumentation.ts' : srcInstrumentationJsExists ? 'instrumentation.js' : instrumentationTsExists ? 'instrumentation.ts' : 'instrumentation.js', (0, templates_1.getInstrumentationHookCopyPasteSnippet)(instrumentationHookLocation))]; case 5: _a.sent(); _a.label = 6; case 6: return [2 /*return*/]; } }); }); })]; case 5: _a.sent(); return [4 /*yield*/, (0, telemetry_1.traceStep)('setup-next-config', function () { return __awaiter(_this, void 0, void 0, function () { var withSentryConfigOptionsTemplate, nextConfigJs, nextConfigMjs, nextConfigJsExists, nextConfigMjsExists, nextConfigJsContent, probablyIncludesSdk, shouldInject, injectAnyhow, nextConfigMjsContent, probablyIncludesSdk, shouldInject, injectAnyhow, mod, expressionToWrap, newCode, _a, shouldContinue; return __generator(this, function (_b) { switch (_b.label) { case 0: withSentryConfigOptionsTemplate = (0, templates_1.getWithSentryConfigOptionsTemplate)({ orgSlug: selectedProject.organization.slug, projectSlug: selectedProject.slug, selfHosted: selfHosted, sentryUrl: sentryUrl, tunnelRoute: sdkConfigOptions.tunnelRoute, }); nextConfigJs = 'next.config.js'; nextConfigMjs = 'next.config.mjs'; nextConfigJsExists = fs.existsSync(path.join(process.cwd(), nextConfigJs)); nextConfigMjsExists = fs.existsSync(path.join(process.cwd(), nextConfigMjs)); if (!(!nextConfigJsExists && !nextConfigMjsExists)) return [3 /*break*/, 2]; Sentry.setTag('next-config-strategy', 'create'); return [4 /*yield*/, fs.promises.writeFile(path.join(process.cwd(), nextConfigJs), (0, templates_1.getNextjsConfigCjsTemplate)(withSentryConfigOptionsTemplate), { encoding: 'utf8', flag: 'w' })]; case 1: _b.sent(); prompts_1.default.log.success("Created ".concat(chalk_1.default.cyan('next.config.js'), " with Sentry configuration.")); _b.label = 2; case 2: if (!nextConfigJsExists) return [3 /*break*/, 7]; Sentry.setTag('next-config-strategy', 'modify'); nextConfigJsContent = fs.readFileSync(path.join(process.cwd(), nextConfigJs), 'utf8'); probablyIncludesSdk = nextConfigJsContent.includes('@sentry/nextjs') && nextConfigJsContent.includes('withSentryConfig'); shouldInject = true; if (!probablyIncludesSdk) return [3 /*break*/, 4]; return [4 /*yield*/, (0, clack_utils_1.abortIfCancelled)(prompts_1.default.confirm({ message: "".concat(chalk_1.default.cyan(nextConfigJs), " already contains Sentry SDK configuration. Should the wizard modify it anyways?"), }))]; case 3: injectAnyhow = _b.sent(); shouldInject = injectAnyhow; _b.label = 4; case 4: if (!shouldInject) return [3 /*break*/, 6]; return [4 /*yield*/, fs.promises.appendFile(path.join(process.cwd(), nextConfigJs), (0, templates_1.getNextjsConfigCjsAppendix)(withSentryConfigOptionsTemplate), 'utf8')]; case 5: _b.sent(); prompts_1.default.log.success("Added Sentry configuration to ".concat(chalk_1.default.cyan(nextConfigJs), ". ").concat(chalk_1.default.dim('(you probably want to clean this up a bit!)'))); _b.label = 6; case 6: Sentry.setTag('next-config-mod-result', 'success'); _b.label = 7; case 7: if (!nextConfigMjsExists) return [3 /*break*/, 16]; nextConfigMjsContent = fs.readFileSync(path.join(process.cwd(), nextConfigMjs), 'utf8'); probablyIncludesSdk = nextConfigMjsContent.includes('@sentry/nextjs') && nextConfigMjsContent.includes('withSentryConfig'); shouldInject = true; if (!probablyIncludesSdk) return [3 /*break*/, 9]; return [4 /*yield*/, (0, clack_utils_1.abortIfCancelled)(prompts_1.default.confirm({ message: "".concat(chalk_1.default.cyan(nextConfigMjs), " already contains Sentry SDK configuration. Should the wizard modify it anyways?"), }))]; case 8: injectAnyhow = _b.sent(); shouldInject = injectAnyhow; _b.label = 9; case 9: _b.trys.push([9, 12, , 16]); if (!shouldInject) return [3 /*break*/, 11]; mod = (0, magicast_1.parseModule)(nextConfigMjsContent); mod.imports.$add({ from: '@sentry/nextjs', imported: 'withSentryConfig', local: 'withSentryConfig', }); expressionToWrap = (0, magicast_1.generateCode)(mod.exports.default.$ast).code; // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment mod.exports.default = magicast_1.builders.raw("withSentryConfig(\n ".concat(expressionToWrap, ",\n ").concat(withSentryConfigOptionsTemplate, "\n)")); newCode = mod.generate().code; return [4 /*yield*/, fs.promises.writeFile(path.join(process.cwd(), nextConfigMjs), newCode, { encoding: 'utf8', flag: 'w', })]; case 10: _b.sent(); prompts_1.default.log.success("Added Sentry configuration to ".concat(chalk_1.default.cyan(nextConfigMjs), ". ").concat(chalk_1.default.dim('(you probably want to clean this up a bit!)'))); Sentry.setTag('next-config-mod-result', 'success'); _b.label = 11; case 11: return [3 /*break*/, 16]; case 12: _a = _b.sent(); Sentry.setTag('next-config-mod-result', 'fail'); prompts_1.default.log.warn(chalk_1.default.yellow("Something went wrong writing to ".concat(chalk_1.default.cyan(nextConfigMjs)))); prompts_1.default.log.info("Please put the following code snippet into ".concat(chalk_1.default.cyan(nextConfigMjs), ": ").concat(chalk_1.default.dim('You probably have to clean it up a bit.'), "\n")); // eslint-disable-next-line no-console console.log((0, templates_1.getNextjsConfigEsmCopyPasteSnippet)(withSentryConfigOptionsTemplate)); return [4 /*yield*/, (0, clack_utils_1.abortIfCancelled)(prompts_1.default.confirm({ message: "Are you done putting the snippet above into ".concat(chalk_1.default.cyan(nextConfigMjs), "?"), active: 'Yes', inactive: 'No, get me out of here', }))]; case 13: shouldContinue = _b.sent(); if (!!shouldContinue) return [3 /*break*/, 15]; return [4 /*yield*/, (0, clack_utils_1.abort)()]; case 14: _b.sent(); _b.label = 15; case 15: return [3 /*break*/, 16]; case 16: return [2 /*return*/]; } }); }); })]; case 6: _a.sent(); return [2 /*return*/]; } }); }); } function createExamplePage(selfHosted, selectedProject, sentryUrl) { return __awaiter(this, void 0, void 0, function () { var srcDir, maybePagesDirPath, maybeSrcPagesDirPath, maybeAppDirPath, maybeSrcAppDirPath, typeScriptDetected, pagesLocation, appLocation, examplePageContents, newPageFileName, newRouteFileName, examplePageContents; return __generator(this, function (_a) { switch (_a.label) { case 0: srcDir = path.join(process.cwd(), 'src'); maybePagesDirPath = path.join(process.cwd(), 'pages'); maybeSrcPagesDirPath = path.join(srcDir, 'pages'); maybeAppDirPath = path.join(process.cwd(), 'app'); maybeSrcAppDirPath = path.join(srcDir, 'app'); typeScriptDetected = (0, clack_utils_1.isUsingTypeScript)(); pagesLocation = fs.existsSync(maybePagesDirPath) && fs.lstatSync(maybePagesDirPath).isDirectory() ? ['pages'] : fs.existsSync(maybeSrcPagesDirPath) && fs.lstatSync(maybeSrcPagesDirPath).isDirectory() ? ['src', 'pages'] : undefined; appLocation = fs.existsSync(maybeAppDirPath) && fs.lstatSync(maybeAppDirPath).isDirectory() ? ['app'] : fs.existsSync(maybeSrcAppDirPath) && fs.lstatSync(maybeSrcAppDirPath).isDirectory() ? ['src', 'app'] : undefined; if (!pagesLocation && !appLocation) { pagesLocation = fs.existsSync(srcDir) && fs.lstatSync(srcDir).isDirectory() ? ['src', 'pages'] : ['pages']; fs.mkdirSync(path.join.apply(path, __spreadArray([process.cwd()], pagesLocation, false)), { recursive: true, }); } Sentry.setTag('nextjs-app-dir', !!appLocation); if (!appLocation) return [3 /*break*/, 3]; examplePageContents = (0, templates_1.getSentryExamplePageContents)({ selfHosted: selfHosted, orgSlug: selectedProject.organization.slug, projectId: selectedProject.id, sentryUrl: sentryUrl, useClient: true, }); fs.mkdirSync(path.join.apply(path, __spreadArray(__spreadArray([process.cwd()], appLocation, false), ['sentry-example-page'], false)), { recursive: true, }); newPageFileName = "page.".concat(typeScriptDetected ? 'tsx' : 'jsx'); return [4 /*yield*/, fs.promises.writeFile(path.join.apply(path, __spreadArray(__spreadArray([process.cwd()], appLocation, false), ['sentry-example-page', newPageFileName], false)), examplePageContents, { encoding: 'utf8', flag: 'w' })]; case 1: _a.sent(); prompts_1.default.log.success("Created ".concat(chalk_1.default.cyan(path.join.apply(path, __spreadArray(__spreadArray([], appLocation, false), ['sentry-example-page', newPageFileName], false))), ".")); fs.mkdirSync(path.join.apply(path, __spreadArray(__spreadArray([process.cwd()], appLocation, false), ['api', 'sentry-example-api'], false)), { recursive: true, }); newRouteFileName = "route.".concat(typeScriptDetected ? 'ts' : 'js'); return [4 /*yield*/, fs.promises.writeFile(path.join.apply(path, __spreadArray(__spreadArray([process.cwd()], appLocation, false), ['api', 'sentry-example-api', newRouteFileName], false)), (0, templates_1.getSentryExampleAppDirApiRoute)(), { encoding: 'utf8', flag: 'w' })]; case 2: _a.sent(); prompts_1.default.log.success("Created ".concat(chalk_1.default.cyan(path.join.apply(path, __spreadArray(__spreadArray([], appLocation, false), ['api', 'sentry-example-api', newRouteFileName], false))), ".")); return [3 /*break*/, 6]; case 3: if (!pagesLocation) return [3 /*break*/, 6]; examplePageContents = (0, templates_1.getSentryExamplePageContents)({ selfHosted: selfHosted, orgSlug: selectedProject.organization.slug, projectId: selectedProject.id, sentryUrl: sentryUrl, useClient: false, }); return [4 /*yield*/, fs.promises.writeFile(path.join.apply(path, __spreadArray(__spreadArray([process.cwd()], pagesLocation, false), ['sentry-example-page.jsx'], false)), examplePageContents, { encoding: 'utf8', flag: 'w' })]; case 4: _a.sent(); prompts_1.default.log.success("Created ".concat(chalk_1.default.cyan(path.join.apply(path, __spreadArray(__spreadArray([], pagesLocation, false), ['sentry-example-page.js'], false))), ".")); fs.mkdirSync(path.join.apply(path, __spreadArray(__spreadArray([process.cwd()], pagesLocation, false), ['api'], false)), { recursive: true, }); return [4 /*yield*/, fs.promises.writeFile(path.join.apply(path, __spreadArray(__spreadArray([process.cwd()], pagesLocation, false), ['api', 'sentry-example-api.js'], false)), (0, templates_1.getSentryExampleApiRoute)(), { encoding: 'utf8', flag: 'w' })]; case 5: _a.sent(); prompts_1.default.log.success("Created ".concat(chalk_1.default.cyan(path.join.apply(path, __spreadArray(__spreadArray([], pagesLocation, false), ['api',