@sentry/wizard
Version:
Sentry wizard helping you to configure your project
100 lines • 6.02 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;
var recast = __importStar(require("recast"));
var templates_1 = require("../templates");
var utils_1 = require("../utils");
// @ts-expect-error - clack is ESM and TS complains about that. It works though
var prompts_1 = __importDefault(require("@clack/prompts"));
var chalk_1 = __importDefault(require("chalk"));
// @ts-expect-error - magicast is ESM and TS complains about that. It works though
var magicast_1 = require("magicast");
function instrumentHandleError(originalEntryServerMod, serverEntryFilename) {
var _a, _b;
var originalEntryServerModAST = originalEntryServerMod.$ast;
var handleErrorFunctionExport = originalEntryServerModAST.body.find(function (node) {
var _a, _b;
return (node.type === 'ExportNamedDeclaration' &&
((_a = node.declaration) === null || _a === void 0 ? void 0 : _a.type) === 'FunctionDeclaration' &&
((_b = node.declaration.id) === null || _b === void 0 ? void 0 : _b.name) === 'handleError');
});
var handleErrorFunctionVariableDeclarationExport = originalEntryServerModAST.body.find(function (node) {
var _a;
return node.type === 'ExportNamedDeclaration' &&
((_a = node.declaration) === null || _a === void 0 ? void 0 : _a.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 ".concat(chalk_1.default.cyan('handleError'), " in ").concat(chalk_1.default.cyan(serverEntryFilename), ". Creating one for you."));
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
var implementation = recast.parse(templates_1.HANDLE_ERROR_TEMPLATE_V2).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(function (util) {
return (0, utils_1.hasSentryContent)((0, magicast_1.generateCode)(handleErrorFunctionExport).code, originalEntryServerMod.$code, util);
})) ||
(handleErrorFunctionVariableDeclarationExport &&
['wrapHandleErrorWithSentry', 'sentryHandleError'].some(function (util) {
return (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
var implementation = recast.parse(templates_1.HANDLE_ERROR_TEMPLATE_V2).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
(_a = implementation.declarations[0].init.arguments[0].body.body).unshift.apply(_a, 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
var implementation = recast.parse(templates_1.HANDLE_ERROR_TEMPLATE_V2).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
(_b = implementation.declarations[0].init.arguments[0].body.body).unshift.apply(_b, 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