UNPKG

@sentry/wizard

Version:

Sentry wizard helping you to configure your project

112 lines (111 loc) 5.5 kB
"use strict"; 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.enableSourcemaps = exports.configureTscSourcemapGenerationFlow = void 0; const fs = __importStar(require("fs")); const path = __importStar(require("path")); const recast = __importStar(require("recast")); const Sentry = __importStar(require("@sentry/node")); // @ts-expect-error - clack is ESM and TS complains about that. It works though const clack = __importStar(require("@clack/prompts")); const chalk_1 = __importDefault(require("chalk")); const clack_1 = require("../../utils/clack"); const ast_utils_1 = require("../../utils/ast-utils"); const debug_1 = require("../../utils/debug"); const b = recast.types.builders; const getCodeSnippet = (colors) => (0, clack_1.makeCodeSnippet)(colors, (unchanged, plus, _) => unchanged(`{ "compilerOptions": { ${plus('"sourceMap": true,')} ${plus('"inlineSources": true,')} // Set \`sourceRoot\` to "/" to strip the build path prefix from // generated source code references. This will improve issue grouping in Sentry. ${plus('"sourceRoot": "/"')} } }`)); async function configureTscSourcemapGenerationFlow() { const tsConfigPath = (0, ast_utils_1.findFile)(path.join(process.cwd(), 'tsconfig'), ['.json']) ?? (await (0, clack_1.askForToolConfigPath)('TypeScript', 'tsconfig.json')); let successfullyAdded = false; if (tsConfigPath) { successfullyAdded = await enableSourcemaps(tsConfigPath); } else { successfullyAdded = await (0, clack_1.createNewConfigFile)(path.join(process.cwd(), 'tsconfig.json'), getCodeSnippet(false)); Sentry.setTag('created-new-config', successfullyAdded ? 'success' : 'fail'); } if (successfullyAdded) { Sentry.setTag('ast-mod', 'success'); clack.log.info(`We recommend checking the ${tsConfigPath ? 'modified' : 'added'} file after the wizard finished to ensure it works with your build setup.`); } else { Sentry.setTag('ast-mod', 'fail'); await (0, clack_1.showCopyPasteInstructions)({ filename: 'tsconfig.json', codeSnippet: getCodeSnippet(true), hint: 'This ensures that source maps are generated correctly', }); } } exports.configureTscSourcemapGenerationFlow = configureTscSourcemapGenerationFlow; /** * Modifies tsconfig.json (@param tsConfigPath) to enable source maps generation. * * Exported only for testing */ async function enableSourcemaps(tsConfigPath) { try { const tsConfig = await fs.promises.readFile(tsConfigPath, 'utf-8'); const { ast, jsonObject } = (0, ast_utils_1.parseJsonC)(tsConfig.toString()); if (!jsonObject || !ast) { // this will only happen if the input file isn't valid JSON-C Sentry.setTag('ast-mod-fail-reason', 'original-file-invalid'); return false; } const compilerOptionsProp = (0, ast_utils_1.getOrSetObjectProperty)(jsonObject, 'compilerOptions', b.objectExpression([])); const compilerOptionsObj = compilerOptionsProp.value; if (!compilerOptionsObj || compilerOptionsObj.type !== 'ObjectExpression') { // a valid compilerOptions prop should always be an object expression Sentry.setTag('ast-mod-fail-reason', 'original-file-invalid'); return false; } (0, ast_utils_1.setOrUpdateObjectProperty)(compilerOptionsObj, 'sourceMap', b.booleanLiteral(true)); (0, ast_utils_1.setOrUpdateObjectProperty)(compilerOptionsObj, 'inlineSources', b.booleanLiteral(true)); (0, ast_utils_1.setOrUpdateObjectProperty)(compilerOptionsObj, 'sourceRoot', b.stringLiteral('/'), 'Set `sourceRoot` to "/" to strip the build path prefix\nfrom generated source code references.\nThis improves issue grouping in Sentry.'); const code = (0, ast_utils_1.printJsonC)(ast); await fs.promises.writeFile(tsConfigPath, code); clack.log.success(`Enabled source maps generation in ${chalk_1.default.cyan(path.basename(tsConfigPath || 'tsconfig.json'))}.`); return true; } catch (e) { (0, debug_1.debug)(e); Sentry.setTag('ast-mod-fail-reason', 'insertion-fail'); return false; } } exports.enableSourcemaps = enableSourcemaps; //# sourceMappingURL=tsc.js.map