@sentry/wizard
Version:
Sentry wizard helping you to configure your project
97 lines • 6.13 kB
JavaScript
;
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
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.instrumentHandleError = void 0;
const recast = __importStar(require("recast"));
const templates_1 = require("../templates");
const utils_1 = require("../utils");
// @ts-expect-error - clack is ESM and TS complains about that. It works though
const prompts_1 = __importDefault(require("@clack/prompts"));
const chalk_1 = __importDefault(require("chalk"));
// @ts-expect-error - magicast is ESM and TS complains about that. It works though
const magicast_1 = require("magicast");
function instrumentHandleError(
// MagicAst returns `ProxifiedModule<any>` so therefore we have to use `any` here
// eslint-disable-next-line @typescript-eslint/no-explicit-any
originalEntryServerMod, serverEntryFilename) {
const originalEntryServerModAST = originalEntryServerMod.$ast;
const handleErrorFunctionExport = originalEntryServerModAST.body.find((node) => {
return (node.type === 'ExportNamedDeclaration' &&
node.declaration?.type === 'FunctionDeclaration' &&
node.declaration.id?.name === 'handleError');
});
const handleErrorFunctionVariableDeclarationExport = originalEntryServerModAST.body.find((node) => node.type === 'ExportNamedDeclaration' &&
node.declaration?.type === 'VariableDeclaration' &&
// @ts-expect-error - id should always have a name in this case
node.declaration.declarations[0].id.name === 'handleError');
if (!handleErrorFunctionExport &&
!handleErrorFunctionVariableDeclarationExport) {
prompts_1.default.log.warn(`Could not find function ${chalk_1.default.cyan('handleError')} in ${chalk_1.default.cyan(serverEntryFilename)}. Creating one for you.`);
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
const implementation = recast.parse(templates_1.HANDLE_ERROR_TEMPLATE).program.body[0];
originalEntryServerModAST.body.splice((0, utils_1.getAfterImportsInsertionIndex)(originalEntryServerModAST), 0,
// @ts-expect-error - string works here because the AST is proxified by magicast
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
recast.types.builders.exportNamedDeclaration(implementation));
}
else if ((handleErrorFunctionExport &&
['wrapHandleErrorWithSentry', 'sentryHandleError'].some((util) => (0, utils_1.hasSentryContent)((0, magicast_1.generateCode)(handleErrorFunctionExport).code, originalEntryServerMod.$code, util))) ||
(handleErrorFunctionVariableDeclarationExport &&
['wrapHandleErrorWithSentry', 'sentryHandleError'].some((util) => (0, utils_1.hasSentryContent)((0, magicast_1.generateCode)(handleErrorFunctionVariableDeclarationExport).code, originalEntryServerMod.$code, util)))) {
return false;
}
else if (handleErrorFunctionExport) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
const implementation = recast.parse(templates_1.HANDLE_ERROR_TEMPLATE).program.body[0];
// If the current handleError function has a body, we need to merge the new implementation with the existing one
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
implementation.declarations[0].init.arguments[0].body.body.unshift(
// @ts-expect-error - declaration works here because the AST is proxified by magicast
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
...handleErrorFunctionExport.declaration.body.body);
// @ts-expect-error - declaration works here because the AST is proxified by magicast
handleErrorFunctionExport.declaration = implementation;
}
else if (handleErrorFunctionVariableDeclarationExport) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
const implementation = recast.parse(templates_1.HANDLE_ERROR_TEMPLATE).program.body[0];
// If the current handleError function has a body, we need to merge the new implementation with the existing one
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
implementation.declarations[0].init.arguments[0].body.body.unshift(
// @ts-expect-error - declaration works here because the AST is proxified by magicast
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
...handleErrorFunctionVariableDeclarationExport.declaration
.declarations[0].init.body.body);
// @ts-expect-error - declaration works here because the AST is proxified by magicast
handleErrorFunctionVariableDeclarationExport.declaration = implementation;
}
return true;
}
exports.instrumentHandleError = instrumentHandleError;
//# sourceMappingURL=handle-error.js.map