@sentry/wizard
Version:
Sentry wizard helping you to configure your project
99 lines (97 loc) • 4.92 kB
JavaScript
;
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.getSentryInitPlainTextSnippet = exports.getSentryInitColoredCodeSnippet = exports.doesJsCodeIncludeSdkSentryImport = exports.addSentryInitWithSdkImport = exports.addSentryInit = void 0;
/* eslint-disable max-lines */
// @ts-ignore - clack is ESM and TS complains about that. It works though
const prompts_1 = __importDefault(require("@clack/prompts"));
const chalk_1 = __importDefault(require("chalk"));
const path = __importStar(require("path"));
const process = __importStar(require("process"));
const fs = __importStar(require("fs"));
const Sentry = __importStar(require("@sentry/node"));
const telemetry_1 = require("../telemetry");
const clack_utils_1 = require("../utils/clack-utils");
const glob_1 = require("./glob");
const react_native_wizard_1 = require("./react-native-wizard");
async function addSentryInit({ dsn }) {
const prefixGlob = '{.,./src,./app}';
const suffixGlob = '@(j|t|cj|mj)s?(x)';
const universalGlob = `@(App|_layout).${suffixGlob}`;
const jsFileGlob = `${prefixGlob}/+(${universalGlob})`;
const jsPath = (0, telemetry_1.traceStep)('find-app-js-file', () => (0, glob_1.getFirstMatchedPath)(jsFileGlob));
Sentry.setTag('app-js-file-status', jsPath ? 'found' : 'not-found');
if (!jsPath) {
prompts_1.default.log.warn(`Could not find main App file. Place the following code snippet close to the Apps Root component.`);
await (0, clack_utils_1.showCopyPasteInstructions)('App.js or _layout.tsx', getSentryInitColoredCodeSnippet(dsn), 'This ensures the Sentry SDK is ready to capture errors.');
return;
}
const jsRelativePath = path.relative(process.cwd(), jsPath);
const js = fs.readFileSync(jsPath, 'utf-8');
const includesSentry = doesJsCodeIncludeSdkSentryImport(js, {
sdkPackageName: react_native_wizard_1.RN_SDK_PACKAGE,
});
if (includesSentry) {
Sentry.setTag('app-js-file-status', 'already-includes-sentry');
prompts_1.default.log.warn(`${chalk_1.default.cyan(jsRelativePath)} already includes Sentry. We wont't add it again.`);
return;
}
(0, telemetry_1.traceStep)('add-sentry-init', () => {
const newContent = addSentryInitWithSdkImport(js, { dsn });
prompts_1.default.log.success(`Added ${chalk_1.default.cyan('Sentry.init')} to ${chalk_1.default.cyan(jsRelativePath)}.`);
fs.writeFileSync(jsPath, newContent, 'utf-8');
});
Sentry.setTag('app-js-file-status', 'added-sentry-init');
prompts_1.default.log.success(chalk_1.default.green(`${chalk_1.default.cyan(jsRelativePath)} changes saved.`));
}
exports.addSentryInit = addSentryInit;
function addSentryInitWithSdkImport(js, { dsn }) {
return js.replace(/^([^]*)(import\s+[^;]*?;$)/m, (match) => `${match}
${getSentryInitPlainTextSnippet(dsn)}`);
}
exports.addSentryInitWithSdkImport = addSentryInitWithSdkImport;
function doesJsCodeIncludeSdkSentryImport(js, { sdkPackageName }) {
return !!js.match(sdkPackageName);
}
exports.doesJsCodeIncludeSdkSentryImport = doesJsCodeIncludeSdkSentryImport;
function getSentryInitColoredCodeSnippet(dsn) {
return (0, clack_utils_1.makeCodeSnippet)(true, (_unchanged, plus, _minus) => {
return plus(getSentryInitPlainTextSnippet(dsn));
});
}
exports.getSentryInitColoredCodeSnippet = getSentryInitColoredCodeSnippet;
function getSentryInitPlainTextSnippet(dsn) {
return `import * as Sentry from '@sentry/react-native';
Sentry.init({
dsn: '${dsn}',
// uncomment the line below to enable Spotlight (https://spotlightjs.com)
// spotlight: __DEV__,
});`;
}
exports.getSentryInitPlainTextSnippet = getSentryInitPlainTextSnippet;
//# sourceMappingURL=javascript.js.map