@sentry/wizard
Version:
Sentry wizard helping you to configure your project
116 lines (115 loc) • 5.54 kB
JavaScript
;
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 __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.modifyViteConfig = void 0;
const Sentry = __importStar(require("@sentry/node"));
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const chalk_1 = __importDefault(require("chalk"));
//@ts-expect-error - clack is ESM and TS complains about that. It works though
const prompts_1 = __importDefault(require("@clack/prompts"));
// @ts-expect-error - magicast is ESM and TS complains about that. It works though
const magicast_1 = require("magicast");
// @ts-expect-error - magicast is ESM and TS complains about that. It works though
const helpers_1 = require("magicast/helpers");
const ast_utils_1 = require("../../utils/ast-utils");
const debug_1 = require("../../utils/debug");
const clack_1 = require("../../utils/clack");
const utils_1 = require("./utils");
async function modifyViteConfig(viteConfigPath, projectInfo) {
const viteConfigContent = (await fs.promises.readFile(viteConfigPath, 'utf-8')).toString();
const { org, project, url, selfHosted } = projectInfo;
const prettyViteConfigFilename = chalk_1.default.cyan(path.basename(viteConfigPath));
try {
const viteModule = (0, magicast_1.parseModule)(viteConfigContent);
if ((0, ast_utils_1.hasSentryContent)(viteModule.$ast)) {
prompts_1.default.log.warn(`File ${prettyViteConfigFilename} already contains Sentry code.
Skipping adding Sentry functionality to.`);
Sentry.setTag(`modified-vite-cfg`, 'fail');
Sentry.setTag(`vite-cfg-fail-reason`, 'has-sentry-content');
return;
}
await (0, utils_1.modifyAndRecordFail)(() => (0, helpers_1.addVitePlugin)(viteModule, {
imported: 'sentrySvelteKit',
from: '@sentry/sveltekit',
constructor: 'sentrySvelteKit',
options: {
org,
project,
...(selfHosted && { url }),
},
index: 0,
}), 'add-vite-plugin', 'vite-cfg');
await (0, utils_1.modifyAndRecordFail)(async () => {
const code = (0, magicast_1.generateCode)(viteModule.$ast).code;
await fs.promises.writeFile(viteConfigPath, code);
}, 'write-file', 'vite-cfg');
}
catch (e) {
(0, debug_1.debug)(e);
await showFallbackViteCopyPasteSnippet(viteConfigPath, getViteConfigCodeSnippet(org, project, selfHosted, url));
Sentry.captureException('Sveltekit Vite Config Modification Fail');
}
prompts_1.default.log.success(`Added Sentry code to ${prettyViteConfigFilename}`);
Sentry.setTag(`modified-vite-cfg`, 'success');
}
exports.modifyViteConfig = modifyViteConfig;
async function showFallbackViteCopyPasteSnippet(viteConfigPath, codeSnippet) {
const viteConfigFilename = path.basename(viteConfigPath);
prompts_1.default.log.warning(`Couldn't automatically modify your ${chalk_1.default.cyan(viteConfigFilename)}
${chalk_1.default.dim(`This sometimes happens when we encounter more complex vite configs.
It may not seem like it but sometimes our magical powers are limited ;)`)}`);
prompts_1.default.log.info("But don't worry - it's super easy to do this yourself!");
prompts_1.default.log.step(`Add the following code to your ${chalk_1.default.cyan(viteConfigFilename)}:`);
// Intentionally logging to console here for easier copy/pasting
// eslint-disable-next-line no-console
console.log(codeSnippet);
await (0, clack_1.abortIfCancelled)(prompts_1.default.select({
message: 'Did you copy the snippet above?',
options: [
{ label: 'Yes!', value: true, hint: "Great, that's already it!" },
],
initialValue: true,
}));
}
const getViteConfigCodeSnippet = (org, project, selfHosted, url) => chalk_1.default.gray(`
import { sveltekit } from '@sveltejs/kit/vite';
import { defineConfig } from 'vite';
${chalk_1.default.greenBright("import { sentrySvelteKit } from '@sentry/sveltekit'")}
export default defineConfig({
plugins: [
// Make sure \`sentrySvelteKit\` is registered before \`sveltekit\`
${chalk_1.default.greenBright(`sentrySvelteKit({
org: '${org}',
project: '${project}',${selfHosted ? `\n url: '${url}',` : ''}
}),`)}
sveltekit(),
]
});
`);
//# sourceMappingURL=vite.js.map