UNPKG

@sentry/wizard

Version:

Sentry wizard helping you to configure your project

106 lines 4.72 kB
"use strict"; /* eslint-disable @typescript-eslint/no-unsafe-argument */ /* eslint-disable @typescript-eslint/no-unsafe-member-access */ /* eslint-disable @typescript-eslint/no-unsafe-assignment */ /* eslint-disable @typescript-eslint/no-unsafe-call */ 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; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.addRoutesToConfig = void 0; const recast = __importStar(require("recast")); const fs = __importStar(require("fs")); // @ts-expect-error - magicast is ESM and TS complains about that. It works though const magicast_1 = require("magicast"); async function addRoutesToConfig(routesConfigPath, isTS) { // Check if file exists first if (!fs.existsSync(routesConfigPath)) { return; } const routesAst = await (0, magicast_1.loadFile)(routesConfigPath); // Check if routes are already added const routesCode = routesAst.$code; if (routesCode.includes('sentry-example-page') && routesCode.includes('sentry-example-api')) { return; } // Add route import if not already present const hasRouteImport = routesAst.imports.$items.some( // eslint-disable-next-line @typescript-eslint/no-explicit-any (item) => item.imported === 'route' && item.from === '@react-router/dev/routes'); if (!hasRouteImport) { routesAst.imports.$add({ from: '@react-router/dev/routes', imported: 'route', local: 'route', }); } // Set up the new routes const routeExtension = isTS ? 'tsx' : 'jsx'; const apiExtension = isTS ? 'ts' : 'js'; const pageRouteCode = `route("/sentry-example-page", "routes/sentry-example-page.${routeExtension}")`; const apiRouteCode = `route("/api/sentry-example-api", "routes/api.sentry-example-api.${apiExtension}")`; let foundDefaultExport = false; // Get the AST program const program = routesAst.$ast; // Find the default export for (let i = 0; i < program.body.length; i++) { const node = program.body[i]; if (node.type === 'ExportDefaultDeclaration') { foundDefaultExport = true; const declaration = node.declaration; let arrayExpression = null; if (declaration && declaration.type === 'ArrayExpression') { arrayExpression = declaration; } else if (declaration && declaration.type === 'TSSatisfiesExpression') { // Handle TypeScript satisfies expression like: [...] satisfies RouteConfig if (declaration.expression && declaration.expression.type === 'ArrayExpression') { arrayExpression = declaration.expression; } } if (arrayExpression) { // Parse and add the new route calls directly to the elements array const pageRouteCall = recast.parse(pageRouteCode).program.body[0].expression; const apiRouteCall = recast.parse(apiRouteCode).program.body[0].expression; arrayExpression.elements.push(pageRouteCall); arrayExpression.elements.push(apiRouteCall); } break; } } // If no default export found, add one if (!foundDefaultExport) { // Create a simple array export without satisfies for now const newExportCode = `export default [ ${pageRouteCode}, ${apiRouteCode}, ];`; const newExport = recast.parse(newExportCode).program.body[0]; program.body.push(newExport); } await (0, magicast_1.writeFile)(routesAst.$ast, routesConfigPath); } exports.addRoutesToConfig = addRoutesToConfig; //# sourceMappingURL=routes-config.js.map