@sentry/wizard
Version:
Sentry wizard helping you to configure your project
195 lines • 9.39 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.instrumentRoot = exports.isWithSentryAlreadyUsed = exports.wrapAppWithSentry = void 0;
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
/* eslint-disable @typescript-eslint/no-unsafe-call */
/* eslint-disable @typescript-eslint/no-unsafe-argument */
const recast = __importStar(require("recast"));
const path = __importStar(require("path"));
const magicast_1 = require("magicast");
const templates_1 = require("../templates");
const utils_1 = require("../utils");
const chalk_1 = __importDefault(require("chalk"));
// @ts-expect-error - clack is ESM and TS complains about that. It works though
const clack = __importStar(require("@clack/prompts"));
function wrapAppWithSentry(rootRouteAst, rootFileName) {
rootRouteAst.imports.$add({
from: '@sentry/remix',
imported: 'withSentry',
local: 'withSentry',
});
recast.visit(rootRouteAst.$ast, {
visitExportDefaultDeclaration(path) {
if (path.value.declaration.type === 'FunctionDeclaration') {
// Move the function declaration just before the default export
path.insertBefore(path.value.declaration);
// Get the name of the function to be wrapped
const functionName = path.value.declaration.id.name;
// Create the wrapped function call
const functionCall = recast.types.builders.callExpression(recast.types.builders.identifier('withSentry'), [recast.types.builders.identifier(functionName)]);
// Replace the default export with the wrapped function call
path.value.declaration = functionCall;
}
else if (path.value.declaration.type === 'Identifier') {
const rootRouteExport = rootRouteAst.exports.default;
const expressionToWrap = (0, magicast_1.generateCode)(rootRouteExport.$ast).code;
rootRouteAst.exports.default = magicast_1.builders.raw(`withSentry(${expressionToWrap})`);
}
else {
clack.log.warn(chalk_1.default.yellow(`Couldn't instrument ${chalk_1.default.bold(rootFileName)} automatically. Wrap your default export with: ${chalk_1.default.dim('withSentry()')}\n`));
}
this.traverse(path);
},
});
}
exports.wrapAppWithSentry = wrapAppWithSentry;
function isWithSentryAlreadyUsed(rootRouteAst) {
// Check if withSentry is called anywhere in the code
let isUsed = false;
recast.visit(rootRouteAst.$ast, {
visitCallExpression(path) {
if (path.value.callee.type === 'Identifier' &&
path.value.callee.name === 'withSentry') {
isUsed = true;
return false; // Stop traversal
}
this.traverse(path);
},
});
return isUsed;
}
exports.isWithSentryAlreadyUsed = isWithSentryAlreadyUsed;
async function instrumentRoot(rootFileName) {
const rootRouteAst = await (0, magicast_1.loadFile)(path.join(process.cwd(), 'app', rootFileName));
const exportsAst = rootRouteAst.exports.$ast;
const namedExports = exportsAst.body.filter((node) => node.type === 'ExportNamedDeclaration');
let foundErrorBoundary = false;
const withSentryAlreadyUsed = isWithSentryAlreadyUsed(rootRouteAst);
namedExports.forEach((namedExport) => {
const declaration = namedExport.declaration;
if (!declaration) {
return;
}
if (declaration.type === 'FunctionDeclaration') {
if (declaration.id?.name === 'ErrorBoundary') {
foundErrorBoundary = true;
}
}
else if (declaration.type === 'VariableDeclaration') {
const declarations = declaration.declarations;
declarations.forEach((declaration) => {
// @ts-expect-error - id should always have a name in this case
if (declaration.id?.name === 'ErrorBoundary') {
foundErrorBoundary = true;
}
});
}
});
if (!foundErrorBoundary) {
rootRouteAst.imports.$add({
from: '@sentry/remix',
imported: 'captureRemixErrorBoundaryError',
local: 'captureRemixErrorBoundaryError',
});
rootRouteAst.imports.$add({
from: '@remix-run/react',
imported: 'useRouteError',
local: 'useRouteError',
});
// Call wrapAppWithSentry if withSentry is not already used
if (!withSentryAlreadyUsed) {
wrapAppWithSentry(rootRouteAst, rootFileName);
}
recast.visit(rootRouteAst.$ast, {
visitExportDefaultDeclaration(path) {
const implementation = recast.parse(templates_1.ERROR_BOUNDARY_TEMPLATE).program
.body[0];
path.insertBefore(recast.types.builders.exportDeclaration(false, implementation));
this.traverse(path);
},
});
// If there is already a ErrorBoundary export, and it doesn't have Sentry content
}
else if (!(0, utils_1.hasSentryContent)(rootFileName, rootRouteAst.$code)) {
rootRouteAst.imports.$add({
from: '@sentry/remix',
imported: 'captureRemixErrorBoundaryError',
local: 'captureRemixErrorBoundaryError',
});
// Call wrapAppWithSentry if withSentry is not already used
if (!withSentryAlreadyUsed) {
wrapAppWithSentry(rootRouteAst, rootFileName);
}
recast.visit(rootRouteAst.$ast, {
visitExportNamedDeclaration(path) {
// Find ErrorBoundary export
if (path.value.declaration?.id?.name === 'ErrorBoundary') {
const errorBoundaryExport = path.value.declaration;
let errorIdentifier;
// check if useRouteError is called
recast.visit(errorBoundaryExport, {
visitVariableDeclaration(path) {
const variableDeclaration = path.value.declarations[0];
const initializer = variableDeclaration.init;
if (initializer.type === 'CallExpression' &&
initializer.callee.name === 'useRouteError') {
errorIdentifier = variableDeclaration.id.name;
}
this.traverse(path);
},
});
// We don't have an errorIdentifier, which means useRouteError is not called / imported
// We need to add it and capture the error
if (!errorIdentifier) {
rootRouteAst.imports.$add({
from: '@remix-run/react',
imported: 'useRouteError',
local: 'useRouteError',
});
const useRouteErrorCall = recast.parse(`const error = useRouteError();`).program.body[0];
// Insert at the top of ErrorBoundary body
errorBoundaryExport.body.body.splice(0, 0, useRouteErrorCall);
}
const captureErrorCall = recast.parse(`captureRemixErrorBoundaryError(error);`).program.body[0];
// Insert just before the the fallback page is returned
errorBoundaryExport.body.body.splice(errorBoundaryExport.body.body.length - 1, 0, captureErrorCall);
}
this.traverse(path);
},
});
}
else if (!withSentryAlreadyUsed) {
// Even if we have Sentry content but withSentry is not used, we should still wrap the app
wrapAppWithSentry(rootRouteAst, rootFileName);
}
await (0, magicast_1.writeFile)(rootRouteAst.$ast, path.join(process.cwd(), 'app', rootFileName));
}
exports.instrumentRoot = instrumentRoot;
//# sourceMappingURL=root.js.map