@sentry/wizard
Version:
Sentry wizard helping you to configure your project
549 lines • 27.4 kB
JavaScript
"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 __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (g && (g = 0, op[0] && (_ = 0)), _) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getModuleExportsAssignmentRight = exports.getMetroConfigObject = exports.addSentryMetroRequireToMetroConfig = exports.addSentrySerializerRequireToMetroConfig = exports.addSentrySerializerToMetroConfig = exports.writeMetroConfig = exports.parseMetroConfig = exports.removeSentryRequire = exports.removeSentrySerializerFromMetroConfig = exports.unPatchMetroConfig = exports.patchMetroConfigWithSentrySerializer = exports.patchMetroWithSentryConfigInMemory = exports.patchMetroWithSentryConfig = exports.metroConfigPath = void 0;
// @ts-ignore - clack is ESM and TS complains about that. It works though
var clack = __importStar(require("@clack/prompts"));
// @ts-ignore - magicast is ESM and TS complains about that. It works though
var magicast_1 = require("magicast");
var fs = __importStar(require("fs"));
var Sentry = __importStar(require("@sentry/node"));
var ast_utils_1 = require("../utils/ast-utils");
var clack_utils_1 = require("../utils/clack-utils");
var recast = __importStar(require("recast"));
var chalk_1 = __importDefault(require("chalk"));
var b = recast.types.builders;
exports.metroConfigPath = 'metro.config.js';
function patchMetroWithSentryConfig() {
return __awaiter(this, void 0, void 0, function () {
var mod, showInstructions, success, saved;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, parseMetroConfig()];
case 1:
mod = _a.sent();
showInstructions = function () {
return (0, clack_utils_1.showCopyPasteInstructions)(exports.metroConfigPath, getMetroWithSentryConfigSnippet(true));
};
return [4 /*yield*/, patchMetroWithSentryConfigInMemory(mod, showInstructions)];
case 2:
success = _a.sent();
if (!success) {
return [2 /*return*/];
}
return [4 /*yield*/, writeMetroConfig(mod)];
case 3:
saved = _a.sent();
if (!saved) return [3 /*break*/, 4];
clack.log.success(chalk_1.default.green("".concat(chalk_1.default.cyan(exports.metroConfigPath), " changes saved.")));
return [3 /*break*/, 6];
case 4:
clack.log.warn("Could not save changes to ".concat(chalk_1.default.cyan(exports.metroConfigPath), ", please follow the manual steps."));
return [4 /*yield*/, showInstructions()];
case 5: return [2 /*return*/, _a.sent()];
case 6: return [2 /*return*/];
}
});
});
}
exports.patchMetroWithSentryConfig = patchMetroWithSentryConfig;
function patchMetroWithSentryConfigInMemory(mod, showInstructions) {
return __awaiter(this, void 0, void 0, function () {
var shouldContinue, configExpression, wrappedConfig, replacedModuleExportsRight, addedSentryMetroImport;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!(0, ast_utils_1.hasSentryContent)(mod.$ast)) return [3 /*break*/, 3];
return [4 /*yield*/, confirmPathMetroConfig()];
case 1:
shouldContinue = _a.sent();
if (!!shouldContinue) return [3 /*break*/, 3];
return [4 /*yield*/, showInstructions()];
case 2:
_a.sent();
return [2 /*return*/, false];
case 3:
configExpression = getModuleExportsAssignmentRight(mod.$ast);
if (!!configExpression) return [3 /*break*/, 5];
clack.log.warn('Could not find Metro config, please follow the manual steps.');
return [4 /*yield*/, showInstructions()];
case 4:
_a.sent();
return [2 /*return*/, false];
case 5:
wrappedConfig = wrapWithSentryConfig(configExpression);
replacedModuleExportsRight = replaceModuleExportsRight(mod.$ast, wrappedConfig);
if (!!replacedModuleExportsRight) return [3 /*break*/, 7];
clack.log.warn('Could not automatically wrap the config export, please follow the manual steps.');
return [4 /*yield*/, showInstructions()];
case 6:
_a.sent();
return [2 /*return*/, false];
case 7:
addedSentryMetroImport = addSentryMetroRequireToMetroConfig(mod.$ast);
if (!!addedSentryMetroImport) return [3 /*break*/, 9];
clack.log.warn('Could not add `@sentry/react-native/metro` import to Metro config, please follow the manual steps.');
return [4 /*yield*/, showInstructions()];
case 8:
_a.sent();
return [2 /*return*/, false];
case 9:
clack.log.success("Added Sentry Metro plugin to ".concat(chalk_1.default.cyan(exports.metroConfigPath), "."));
return [2 /*return*/, true];
}
});
});
}
exports.patchMetroWithSentryConfigInMemory = patchMetroWithSentryConfigInMemory;
function patchMetroConfigWithSentrySerializer() {
return __awaiter(this, void 0, void 0, function () {
var mod, showInstructions, shouldContinue, configObj, addedSentrySerializer, addedSentrySerializerImport, saved;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, parseMetroConfig()];
case 1:
mod = _a.sent();
showInstructions = function () {
return (0, clack_utils_1.showCopyPasteInstructions)(exports.metroConfigPath, getMetroSentrySerializerSnippet(true));
};
if (!(0, ast_utils_1.hasSentryContent)(mod.$ast)) return [3 /*break*/, 4];
return [4 /*yield*/, confirmPathMetroConfig()];
case 2:
shouldContinue = _a.sent();
if (!!shouldContinue) return [3 /*break*/, 4];
return [4 /*yield*/, showInstructions()];
case 3: return [2 /*return*/, _a.sent()];
case 4:
configObj = getMetroConfigObject(mod.$ast);
if (!configObj) {
clack.log.warn('Could not find Metro config object, please follow the manual steps.');
return [2 /*return*/, showInstructions()];
}
addedSentrySerializer = addSentrySerializerToMetroConfig(configObj);
if (!!addedSentrySerializer) return [3 /*break*/, 6];
clack.log.warn('Could not add Sentry serializer to Metro config, please follow the manual steps.');
return [4 /*yield*/, showInstructions()];
case 5: return [2 /*return*/, _a.sent()];
case 6:
addedSentrySerializerImport = addSentrySerializerRequireToMetroConfig(mod.$ast);
if (!!addedSentrySerializerImport) return [3 /*break*/, 8];
clack.log.warn('Could not add Sentry serializer import to Metro config, please follow the manual steps.');
return [4 /*yield*/, showInstructions()];
case 7: return [2 /*return*/, _a.sent()];
case 8:
clack.log.success("Added Sentry Metro plugin to ".concat(chalk_1.default.cyan(exports.metroConfigPath), "."));
return [4 /*yield*/, writeMetroConfig(mod)];
case 9:
saved = _a.sent();
if (!saved) return [3 /*break*/, 10];
clack.log.success(chalk_1.default.green("".concat(chalk_1.default.cyan(exports.metroConfigPath), " changes saved.")));
return [3 /*break*/, 12];
case 10:
clack.log.warn("Could not save changes to ".concat(chalk_1.default.cyan(exports.metroConfigPath), ", please follow the manual steps."));
return [4 /*yield*/, showInstructions()];
case 11: return [2 /*return*/, _a.sent()];
case 12: return [2 /*return*/];
}
});
});
}
exports.patchMetroConfigWithSentrySerializer = patchMetroConfigWithSentrySerializer;
function unPatchMetroConfig() {
return __awaiter(this, void 0, void 0, function () {
var mod, removedAtLeastOneRequire, removedSerializerConfig, saved;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, parseMetroConfig()];
case 1:
mod = _a.sent();
removedAtLeastOneRequire = removeSentryRequire(mod.$ast);
removedSerializerConfig = removeSentrySerializerFromMetroConfig(mod.$ast);
if (!(removedAtLeastOneRequire || removedSerializerConfig)) return [3 /*break*/, 3];
return [4 /*yield*/, writeMetroConfig(mod)];
case 2:
saved = _a.sent();
if (saved) {
clack.log.success("Removed Sentry Metro plugin from ".concat(chalk_1.default.cyan(exports.metroConfigPath), "."));
}
return [3 /*break*/, 4];
case 3:
clack.log.warn("No Sentry Metro plugin found in ".concat(chalk_1.default.cyan(exports.metroConfigPath), "."));
_a.label = 4;
case 4: return [2 /*return*/];
}
});
});
}
exports.unPatchMetroConfig = unPatchMetroConfig;
function removeSentrySerializerFromMetroConfig(program) {
var configObject = getMetroConfigObject(program);
if (!configObject) {
return false;
}
var serializerProp = getSerializerProp(configObject);
if ('invalid' === serializerProp || 'undefined' === serializerProp) {
return false;
}
var customSerializerProp = getCustomSerializerProp(serializerProp);
if ('invalid' === customSerializerProp ||
'undefined' === customSerializerProp) {
return false;
}
if (serializerProp.value.type === 'ObjectExpression' &&
customSerializerProp.value.type === 'CallExpression' &&
customSerializerProp.value.callee.type === 'Identifier' &&
customSerializerProp.value.callee.name === 'createSentryMetroSerializer') {
if (customSerializerProp.value.arguments.length === 0) {
// FROM serializer: { customSerializer: createSentryMetroSerializer() }
// TO serializer: {}
var removed_1 = false;
serializerProp.value.properties = serializerProp.value.properties.filter(function (p) {
if (p.type === 'ObjectProperty' &&
p.key.type === 'Identifier' &&
p.key.name === 'customSerializer') {
removed_1 = true;
return false;
}
return true;
});
if (removed_1) {
return true;
}
}
else {
if (customSerializerProp.value.arguments[0].type !== 'SpreadElement') {
// FROM serializer: { customSerializer: createSentryMetroSerializer(wrapperSerializer) }
// TO serializer: { customSerializer: wrapperSerializer }
customSerializerProp.value = customSerializerProp.value.arguments[0];
return true;
}
}
}
return false;
}
exports.removeSentrySerializerFromMetroConfig = removeSentrySerializerFromMetroConfig;
function removeSentryRequire(program) {
return (0, ast_utils_1.removeRequire)(program, '@sentry');
}
exports.removeSentryRequire = removeSentryRequire;
function parseMetroConfig() {
return __awaiter(this, void 0, void 0, function () {
var metroConfigContent;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, fs.promises.readFile(exports.metroConfigPath)];
case 1:
metroConfigContent = (_a.sent()).toString();
return [2 /*return*/, (0, magicast_1.parseModule)(metroConfigContent)];
}
});
});
}
exports.parseMetroConfig = parseMetroConfig;
function writeMetroConfig(mod) {
return __awaiter(this, void 0, void 0, function () {
var e_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 2, , 3]);
return [4 /*yield*/, (0, magicast_1.writeFile)(mod.$ast, exports.metroConfigPath)];
case 1:
_a.sent();
return [3 /*break*/, 3];
case 2:
e_1 = _a.sent();
clack.log.error("Failed to write to ".concat(chalk_1.default.cyan(exports.metroConfigPath), ": ").concat(JSON.stringify(e_1)));
return [2 /*return*/, false];
case 3: return [2 /*return*/, true];
}
});
});
}
exports.writeMetroConfig = writeMetroConfig;
function addSentrySerializerToMetroConfig(configObj) {
var serializerProp = getSerializerProp(configObj);
if ('invalid' === serializerProp) {
return false;
}
// case 1: serializer property doesn't exist yet, so we can just add it
if ('undefined' === serializerProp) {
configObj.properties.push(b.objectProperty(b.identifier('serializer'), b.objectExpression([
b.objectProperty(b.identifier('customSerializer'), b.callExpression(b.identifier('createSentryMetroSerializer'), [])),
])));
return true;
}
var customSerializerProp = getCustomSerializerProp(serializerProp);
// case 2: serializer.customSerializer property doesn't exist yet, so we just add it
if ('undefined' === customSerializerProp &&
serializerProp.value.type === 'ObjectExpression') {
serializerProp.value.properties.push(b.objectProperty(b.identifier('customSerializer'), b.callExpression(b.identifier('createSentryMetroSerializer'), [])));
return true;
}
return false;
}
exports.addSentrySerializerToMetroConfig = addSentrySerializerToMetroConfig;
function getCustomSerializerProp(prop) {
var customSerializerProp = prop.value.type === 'ObjectExpression' &&
prop.value.properties.find(function (p) {
return p.key.type === 'Identifier' && p.key.name === 'customSerializer';
});
if (!customSerializerProp) {
return 'undefined';
}
if (customSerializerProp.type === 'ObjectProperty') {
return customSerializerProp;
}
return 'invalid';
}
function getSerializerProp(obj) {
var serializerProp = obj.properties.find(function (p) {
return p.key.type === 'Identifier' && p.key.name === 'serializer';
});
if (!serializerProp) {
return 'undefined';
}
if (serializerProp.type === 'ObjectProperty') {
return serializerProp;
}
return 'invalid';
}
function addSentrySerializerRequireToMetroConfig(program) {
var lastRequireIndex = (0, ast_utils_1.getLastRequireIndex)(program);
var sentrySerializerRequire = createSentrySerializerRequire();
var sentryImportIndex = lastRequireIndex + 1;
if (sentryImportIndex < program.body.length) {
// insert after last require
program.body.splice(lastRequireIndex + 1, 0, sentrySerializerRequire);
}
else {
// insert at the beginning
program.body.unshift(sentrySerializerRequire);
}
return true;
}
exports.addSentrySerializerRequireToMetroConfig = addSentrySerializerRequireToMetroConfig;
function addSentryMetroRequireToMetroConfig(program) {
var lastRequireIndex = (0, ast_utils_1.getLastRequireIndex)(program);
var sentryMetroRequire = createSentryMetroRequire();
var sentryImportIndex = lastRequireIndex + 1;
if (sentryImportIndex < program.body.length) {
// insert after last require
program.body.splice(lastRequireIndex + 1, 0, sentryMetroRequire);
}
else {
// insert at the beginning
program.body.unshift(sentryMetroRequire);
}
return true;
}
exports.addSentryMetroRequireToMetroConfig = addSentryMetroRequireToMetroConfig;
function wrapWithSentryConfig(configObj) {
return b.callExpression(b.identifier('withSentryConfig'), [configObj]);
}
function replaceModuleExportsRight(program, wrappedConfig) {
var moduleExports = getModuleExports(program);
if (!moduleExports) {
return false;
}
if (moduleExports.expression.type === 'AssignmentExpression') {
moduleExports.expression.right = wrappedConfig;
return true;
}
return false;
}
/**
* Creates const {createSentryMetroSerializer} = require('@sentry/react-native/dist/js/tools/sentryMetroSerializer');
*/
function createSentrySerializerRequire() {
return b.variableDeclaration('const', [
b.variableDeclarator(b.objectPattern([
b.objectProperty.from({
key: b.identifier('createSentryMetroSerializer'),
value: b.identifier('createSentryMetroSerializer'),
shorthand: true,
}),
]), b.callExpression(b.identifier('require'), [
b.literal('@sentry/react-native/dist/js/tools/sentryMetroSerializer'),
])),
]);
}
/**
* Creates const {withSentryConfig} = require('@sentry/react-native/metro');
*/
function createSentryMetroRequire() {
return b.variableDeclaration('const', [
b.variableDeclarator(b.objectPattern([
b.objectProperty.from({
key: b.identifier('withSentryConfig'),
value: b.identifier('withSentryConfig'),
shorthand: true,
}),
]), b.callExpression(b.identifier('require'), [
b.literal('@sentry/react-native/metro'),
])),
]);
}
function confirmPathMetroConfig() {
return __awaiter(this, void 0, void 0, function () {
var shouldContinue;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, (0, clack_utils_1.abortIfCancelled)(clack.select({
message: "Metro Config already contains Sentry-related code. Should the wizard modify it anyway?",
options: [
{
label: 'Yes, add the Sentry Metro plugin',
value: true,
},
{
label: 'No, show me instructions to manually add the plugin',
value: false,
},
],
initialValue: true,
}))];
case 1:
shouldContinue = _a.sent();
if (!shouldContinue) {
Sentry.setTag('ast-mod-fail-reason', 'has-sentry-content');
}
return [2 /*return*/, shouldContinue];
}
});
});
}
/**
* Returns value from `module.exports = value` or `const config = value`
*/
function getMetroConfigObject(program) {
var _a;
// check config variable
var configVariable = program.body.find(function (s) {
if (s.type === 'VariableDeclaration' &&
s.declarations.length === 1 &&
s.declarations[0].type === 'VariableDeclarator' &&
s.declarations[0].id.type === 'Identifier' &&
s.declarations[0].id.name === 'config') {
return true;
}
return false;
});
if ((configVariable === null || configVariable === void 0 ? void 0 : configVariable.declarations[0].type) === 'VariableDeclarator' &&
((_a = configVariable === null || configVariable === void 0 ? void 0 : configVariable.declarations[0].init) === null || _a === void 0 ? void 0 : _a.type) === 'ObjectExpression') {
Sentry.setTag('metro-config', 'config-variable');
return configVariable.declarations[0].init;
}
return getModuleExportsObject(program);
}
exports.getMetroConfigObject = getMetroConfigObject;
function getModuleExportsObject(program) {
// check module.exports
var moduleExports = getModuleExportsAssignmentRight(program);
if ((moduleExports === null || moduleExports === void 0 ? void 0 : moduleExports.type) === 'ObjectExpression') {
return moduleExports;
}
Sentry.setTag('metro-config', 'not-found');
return undefined;
}
function getModuleExportsAssignmentRight(program) {
// check module.exports
var moduleExports = getModuleExports(program);
if ((moduleExports === null || moduleExports === void 0 ? void 0 : moduleExports.expression.type) === 'AssignmentExpression' &&
(moduleExports.expression.right.type === 'ObjectExpression' ||
moduleExports.expression.right.type === 'CallExpression' ||
moduleExports.expression.right.type === 'Identifier')) {
Sentry.setTag('metro-config', 'module-exports');
return moduleExports === null || moduleExports === void 0 ? void 0 : moduleExports.expression.right;
}
Sentry.setTag('metro-config', 'not-found');
return undefined;
}
exports.getModuleExportsAssignmentRight = getModuleExportsAssignmentRight;
function getModuleExports(program) {
// find module.exports
return program.body.find(function (s) {
if (s.type === 'ExpressionStatement' &&
s.expression.type === 'AssignmentExpression' &&
s.expression.left.type === 'MemberExpression' &&
s.expression.left.object.type === 'Identifier' &&
s.expression.left.object.name === 'module' &&
s.expression.left.property.type === 'Identifier' &&
s.expression.left.property.name === 'exports') {
return true;
}
return false;
});
}
function getMetroSentrySerializerSnippet(colors) {
return (0, clack_utils_1.makeCodeSnippet)(colors, function (unchanged, plus, _) {
return unchanged("const {getDefaultConfig, mergeConfig} = require('@react-native/metro-config');\";\n".concat(plus("const {createSentryMetroSerializer} = require('@sentry/react-native/dist/js/tools/sentryMetroSerializer');"), "\n\nconst config = {\n ").concat(plus("serializer: {\n customSerializer: createSentryMetroSerializer(),\n },"), "\n};\n\nmodule.exports = mergeConfig(getDefaultConfig(__dirname), config);\n"));
});
}
function getMetroWithSentryConfigSnippet(colors) {
return (0, clack_utils_1.makeCodeSnippet)(colors, function (unchanged, plus, _) {
return unchanged("const {getDefaultConfig, mergeConfig} = require('@react-native/metro-config');\";\n".concat(plus("const {withSentryConfig} = require('@sentry/react-native/metro');"), "\n\nconst config = {};\n\nmodule.exports = ").concat(plus('withSentryConfig('), "mergeConfig(getDefaultConfig(__dirname), config)").concat(plus(')'), ";\n"));
});
}
//# sourceMappingURL=metro.js.map