@sentry/wizard
Version:
Sentry wizard helping you to configure your project
213 lines • 12.3 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.writeXcodeProject = exports.findDebugFilesUploadPhase = exports.unPatchDebugFilesUploadPhase = exports.addDebugFilesUploadPhaseWithCli = exports.addDebugFilesUploadPhaseWithBundledScripts = exports.addSentryWithCliToBundleShellScript = exports.addSentryWithBundledScriptsToBundleShellScript = exports.doesBundlePhaseIncludeSentry = exports.findBundlePhase = exports.removeSentryFromBundleShellScript = exports.unPatchBundlePhase = exports.patchBundlePhase = exports.getValidExistingBuildPhases = void 0;
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
/* eslint-disable @typescript-eslint/no-unsafe-call */
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
var fs = __importStar(require("fs"));
// @ts-ignore - clack is ESM and TS complains about that. It works though
var prompts_1 = __importDefault(require("@clack/prompts"));
var chalk_1 = __importDefault(require("chalk"));
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function getValidExistingBuildPhases(xcodeProject) {
var map = {};
var raw = xcodeProject.hash.project.objects.PBXShellScriptBuildPhase || {};
for (var key in raw) {
var val = raw[key];
val.isa && (map[key] = val);
}
return map;
}
exports.getValidExistingBuildPhases = getValidExistingBuildPhases;
function patchBundlePhase(bundlePhase, patch) {
if (!bundlePhase) {
prompts_1.default.log.warn("Could not find ".concat(chalk_1.default.cyan('Bundle React Native code and images'), " build phase."));
return;
}
var bundlePhaseIncludesSentry = doesBundlePhaseIncludeSentry(bundlePhase);
if (bundlePhaseIncludesSentry) {
prompts_1.default.log.warn("Build phase ".concat(chalk_1.default.cyan('Bundle React Native code and images'), " already includes Sentry."));
return;
}
var script = JSON.parse(bundlePhase.shellScript);
bundlePhase.shellScript = JSON.stringify(patch(script));
prompts_1.default.log.success("Patched Build phase ".concat(chalk_1.default.cyan('Bundle React Native code and images'), "."));
}
exports.patchBundlePhase = patchBundlePhase;
function unPatchBundlePhase(bundlePhase) {
if (!bundlePhase) {
prompts_1.default.log.warn("Could not find ".concat(chalk_1.default.cyan('Bundle React Native code and images'), " build phase."));
return;
}
if (!bundlePhase.shellScript.match(/sentry-cli\s+react-native\s+xcode/i) &&
!bundlePhase.shellScript.includes('sentry-xcode.sh')) {
prompts_1.default.log.success("Build phase ".concat(chalk_1.default.cyan('Bundle React Native code and images'), " does not include Sentry."));
return;
}
bundlePhase.shellScript = JSON.stringify(removeSentryFromBundleShellScript(JSON.parse(bundlePhase.shellScript)));
prompts_1.default.log.success("Build phase ".concat(chalk_1.default.cyan('Bundle React Native code and images'), " unpatched successfully."));
}
exports.unPatchBundlePhase = unPatchBundlePhase;
function removeSentryFromBundleShellScript(script) {
return (script
// remove sentry properties export
.replace(/^export SENTRY_PROPERTIES=sentry.properties\r?\n/m, '')
.replace(/^\/bin\/sh .*?..\/node_modules\/@sentry\/react-native\/scripts\/collect-modules.sh"?\r?\n/m, '')
// unwrap react-native-xcode.sh command. In case someone replaced it
// entirely with the sentry-cli command we need to put the original
// version back in.
.replace(/\.\.\/node_modules\/@sentry\/cli\/bin\/sentry-cli\s+react-native\s+xcode\s+\$REACT_NATIVE_XCODE/i, '$REACT_NATIVE_XCODE')
.replace(
// eslint-disable-next-line no-useless-escape
/\"\/bin\/sh.*?sentry-xcode.sh\s+\$REACT_NATIVE_XCODE/i,
// eslint-disable-next-line no-useless-escape
'"$REACT_NATIVE_XCODE'));
}
exports.removeSentryFromBundleShellScript = removeSentryFromBundleShellScript;
function findBundlePhase(buildPhases) {
return Object.values(buildPhases).find(function (buildPhase) {
return buildPhase.shellScript.match(/\/scripts\/react-native-xcode\.sh/i);
});
}
exports.findBundlePhase = findBundlePhase;
function doesBundlePhaseIncludeSentry(buildPhase) {
var containsSentryCliRNCommand = !!buildPhase.shellScript.match(/sentry-cli\s+react-native\s+xcode/i);
var containsBundledScript = buildPhase.shellScript.includes('sentry-xcode.sh');
return containsSentryCliRNCommand || containsBundledScript;
}
exports.doesBundlePhaseIncludeSentry = doesBundlePhaseIncludeSentry;
function addSentryWithBundledScriptsToBundleShellScript(script) {
var isLikelyPlainReactNativeScript = script.includes('$REACT_NATIVE_XCODE');
if (isLikelyPlainReactNativeScript) {
return script.replace('$REACT_NATIVE_XCODE',
// eslint-disable-next-line no-useless-escape
'\\"/bin/sh ../node_modules/@sentry/react-native/scripts/sentry-xcode.sh $REACT_NATIVE_XCODE\\"');
}
var isLikelyExpoScript = script.includes('expo');
if (isLikelyExpoScript) {
var SENTRY_REACT_NATIVE_XCODE_PATH_1 = "`\"$NODE_BINARY\" --print \"require('path').dirname(require.resolve('@sentry/react-native/package.json')) + '/scripts/sentry-xcode.sh'\"`";
return script.replace(/^.*?(packager|scripts)\/react-native-xcode\.sh\s*(\\'\\\\")?/m,
// eslint-disable-next-line no-useless-escape
function (match) { return "/bin/sh ".concat(SENTRY_REACT_NATIVE_XCODE_PATH_1, " ").concat(match); });
}
return script;
}
exports.addSentryWithBundledScriptsToBundleShellScript = addSentryWithBundledScriptsToBundleShellScript;
function addSentryWithCliToBundleShellScript(script) {
return ('export SENTRY_PROPERTIES=sentry.properties\n' +
'export EXTRA_PACKAGER_ARGS="--sourcemap-output $DERIVED_FILE_DIR/main.jsbundle.map"\n' +
script.replace('$REACT_NATIVE_XCODE', function () {
// eslint-disable-next-line no-useless-escape
return '\\"../node_modules/@sentry/cli/bin/sentry-cli react-native xcode $REACT_NATIVE_XCODE\\"';
}) +
'\n/bin/sh -c "$WITH_ENVIRONMENT ../node_modules/@sentry/react-native/scripts/collect-modules.sh"\n');
}
exports.addSentryWithCliToBundleShellScript = addSentryWithCliToBundleShellScript;
function addDebugFilesUploadPhaseWithBundledScripts(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
xcodeProject, _a) {
var debugFilesUploadPhaseExists = _a.debugFilesUploadPhaseExists;
if (debugFilesUploadPhaseExists) {
prompts_1.default.log.warn("Build phase ".concat(chalk_1.default.cyan('Upload Debug Symbols to Sentry'), " already exists."));
return;
}
xcodeProject.addBuildPhase([], 'PBXShellScriptBuildPhase', 'Upload Debug Symbols to Sentry', null, {
shellPath: '/bin/sh',
shellScript: "/bin/sh ../node_modules/@sentry/react-native/scripts/sentry-xcode-debug-files.sh",
});
prompts_1.default.log.success("Added Build phase ".concat(chalk_1.default.cyan('Upload Debug Symbols to Sentry'), "."));
}
exports.addDebugFilesUploadPhaseWithBundledScripts = addDebugFilesUploadPhaseWithBundledScripts;
function addDebugFilesUploadPhaseWithCli(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
xcodeProject, _a) {
var debugFilesUploadPhaseExists = _a.debugFilesUploadPhaseExists;
if (debugFilesUploadPhaseExists) {
prompts_1.default.log.warn("Build phase ".concat(chalk_1.default.cyan('Upload Debug Symbols to Sentry'), " already exists."));
return;
}
xcodeProject.addBuildPhase([], 'PBXShellScriptBuildPhase', 'Upload Debug Symbols to Sentry', null, {
shellPath: '/bin/sh',
shellScript: "\nWITH_ENVIRONMENT=\"../node_modules/react-native/scripts/xcode/with-environment.sh\"\nif [ -f \"$WITH_ENVIRONMENT\" ]; then\n . \"$WITH_ENVIRONMENT\"\nfi\nexport SENTRY_PROPERTIES=sentry.properties\n[ \"$SENTRY_INCLUDE_NATIVE_SOURCES\" = \"true\" ] && INCLUDE_SOURCES_FLAG=\"--include-sources\" || INCLUDE_SOURCES_FLAG=\"\"\n../node_modules/@sentry/cli/bin/sentry-cli debug-files upload \"$INCLUDE_SOURCES_FLAG\" \"$DWARF_DSYM_FOLDER_PATH\"\n",
});
prompts_1.default.log.success("Added Build phase ".concat(chalk_1.default.cyan('Upload Debug Symbols to Sentry'), "."));
}
exports.addDebugFilesUploadPhaseWithCli = addDebugFilesUploadPhaseWithCli;
function unPatchDebugFilesUploadPhase(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
xcodeProject) {
var buildPhasesMap = xcodeProject.hash.project.objects.PBXShellScriptBuildPhase || {};
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
var debugFilesUploadPhaseResult = findDebugFilesUploadPhase(buildPhasesMap);
if (!debugFilesUploadPhaseResult) {
prompts_1.default.log.success("Build phase ".concat(chalk_1.default.cyan('Upload Debug Symbols to Sentry'), " not found."));
return;
}
var debugFilesUploadPhaseKey = debugFilesUploadPhaseResult[0];
var firstTarget = xcodeProject.getFirstTarget().uuid;
var nativeTargets = xcodeProject.hash.project.objects.PBXNativeTarget;
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
delete buildPhasesMap[debugFilesUploadPhaseKey];
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
delete buildPhasesMap["".concat(debugFilesUploadPhaseKey, "_comment")];
var phases = nativeTargets[firstTarget].buildPhases;
if (phases) {
for (var i = 0; i < phases.length; i++) {
if (phases[i].value === debugFilesUploadPhaseKey) {
phases.splice(i, 1);
break;
}
}
}
prompts_1.default.log.success("Build phase ".concat(chalk_1.default.cyan('Upload Debug Symbols to Sentry'), " removed successfully."));
}
exports.unPatchDebugFilesUploadPhase = unPatchDebugFilesUploadPhase;
function findDebugFilesUploadPhase(buildPhasesMap) {
return Object.entries(buildPhasesMap).find(function (_a) {
var _ = _a[0], buildPhase = _a[1];
var containsCliDebugUpload = typeof buildPhase !== 'string' &&
!!buildPhase.shellScript.match(/sentry-cli\s+(upload-dsym|debug-files upload)\b/);
var containsBundledDebugUpload = typeof buildPhase !== 'string' &&
buildPhase.shellScript.includes('sentry-xcode-debug-files.sh');
return containsCliDebugUpload || containsBundledDebugUpload;
});
}
exports.findDebugFilesUploadPhase = findDebugFilesUploadPhase;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function writeXcodeProject(xcodeProjectPath, xcodeProject) {
var newContent = xcodeProject.writeSync();
var currentContent = fs.readFileSync(xcodeProjectPath, 'utf-8');
if (newContent === currentContent) {
return;
}
fs.writeFileSync(xcodeProjectPath, newContent, 'utf-8');
prompts_1.default.log.success(chalk_1.default.green("Xcode project ".concat(chalk_1.default.cyan(xcodeProjectPath), " changes saved.")));
}
exports.writeXcodeProject = writeXcodeProject;
//# sourceMappingURL=xcode.js.map