UNPKG

@hug/ngx-sentry

Version:

Angular wrapper for the official Sentry JavaScript SDK

57 lines 3.18 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.initSentry = void 0; const core_1 = require("@angular-devkit/core"); const schematics_1 = require("@angular-devkit/schematics"); const ngx_schematics_utilities_1 = require("@hug/ngx-schematics-utilities"); const initSentry = ({ tree, project }, options) => { const mainTsContent = tree.read(project.mainFilePath)?.toString('utf-8') ?? ''; const rules = []; // Initialize Sentry in main.ts if (!mainTsContent.includes('initSentry(')) { let insertAtPosition = 0; const imports = mainTsContent.match(/import .*;/gm); if (imports?.length) { const lastImport = imports[imports.length - 1]; insertAtPosition = mainTsContent.indexOf(lastImport) + lastImport.length; } const initOptions = core_1.tags.stripIndents ` dsn: '${options?.dsnUrl ?? 'replace it with your own value'}', environment: 'DEV', // replace it with your own value release: packageJson.version, enabled: !isDevMode() `; let newContent = mainTsContent.substring(0, insertAtPosition); newContent += `\n\ninitSentry({\n${core_1.tags.indentBy(2) `${initOptions}`}\n});`; newContent += mainTsContent.substring(insertAtPosition); rules.push((0, ngx_schematics_utilities_1.createOrUpdateFile)(project.mainFilePath, newContent), (0, ngx_schematics_utilities_1.logAction)('Have a look at `main.ts` file and update Sentry configuration according to your needs')); } rules.push((0, ngx_schematics_utilities_1.addImportToFile)(project.mainFilePath, 'isDevMode', '@angular/core'), (0, ngx_schematics_utilities_1.addImportToFile)(project.mainFilePath, 'packageJson', '../package.json', true)); // Provide library rules.push((0, ngx_schematics_utilities_1.addImportToFile)(project.mainFilePath, 'initSentry', '@hug/ngx-sentry')); if (project.isStandalone) { rules.push((0, ngx_schematics_utilities_1.addProviderToBootstrapApplication)(project.mainFilePath, 'provideSentry()', '@hug/ngx-sentry')); } else { const appModulePath = project.pathFromSourceRoot('app/app.module.ts'); rules.push((0, ngx_schematics_utilities_1.addImportToNgModule)(appModulePath, 'NgxSentryModule.forRoot()', '@hug/ngx-sentry')); } return (0, schematics_1.chain)(rules); }; exports.initSentry = initSentry; exports.default = (options) => (0, ngx_schematics_utilities_1.schematic)('sentry', [ (0, ngx_schematics_utilities_1.workspace)() // deploy files .deployFiles({ ...options, originUrl: new URL(options.dsnUrl).origin }) // tsconfig.json (required to extract the version in package.json) .modifyJsonFile('tsconfig.json', ['compilerOptions', 'allowSyntheticDefaultImports'], true) .modifyJsonFile('tsconfig.json', ['compilerOptions', 'resolveJsonModule'], true) .toRule(), (0, ngx_schematics_utilities_1.application)(options.project) .rule(context => (0, exports.initSentry)(context, options)) .toRule() ], options); //# sourceMappingURL=index.js.map