@sentry/wizard
Version:
Sentry wizard helping you to configure your project
134 lines • 5.94 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.addCodeSnippetToProject = void 0;
var fs = __importStar(require("fs"));
var path = __importStar(require("path"));
var templates = __importStar(require("./templates"));
var Sentry = __importStar(require("@sentry/node"));
// @ts-ignore - clack is ESM and TS complains about that. It works though
var prompts_1 = __importDefault(require("@clack/prompts"));
var swiftAppLaunchRegex = /(func\s+application\s*\(_\sapplication:[^,]+,\s*didFinishLaunchingWithOptions[^,]+:[^)]+\)\s+->\s+Bool\s+{)|func\s+applicationDidFinishLaunching\(_\s+aNotification:\s+Notification\)\s+{/im;
var objcAppLaunchRegex = /-\s*\(BOOL\)\s*application:\s*\(UIApplication\s*\*\)\s*application\s+didFinishLaunchingWithOptions:\s*\(NSDictionary\s*\*\)\s*launchOptions\s*{/im;
var swiftUIRegex = /@main\s+struct[^:]+:\s*App\s*{/im;
function isAppDelegateFile(filePath) {
var appLaunchRegex = filePath.toLowerCase().endsWith('.swift')
? swiftAppLaunchRegex
: objcAppLaunchRegex;
var fileContent = fs.readFileSync(filePath, 'utf8');
return appLaunchRegex.test(fileContent) || swiftUIRegex.test(fileContent);
}
function findAppDidFinishLaunchingWithOptions(dir, files) {
if (files === void 0) { files = undefined; }
if (!files) {
files = fs.readdirSync(dir);
files = files.map(function (f) { return path.join(dir, f); });
}
//iterate over subdirectories later,
//the appdelegate usually is in the top level
var dirs = [];
for (var _i = 0, files_1 = files; _i < files_1.length; _i++) {
var filePath = files_1[_i];
if (filePath.endsWith('.swift') ||
filePath.endsWith('.m') ||
filePath.endsWith('.mm')) {
if (fs.existsSync(filePath) && isAppDelegateFile(filePath)) {
return filePath;
}
}
else if (!filePath.startsWith('.') &&
!filePath.endsWith('.xcodeproj') &&
!filePath.endsWith('.xcassets') &&
fs.existsSync(filePath) &&
fs.lstatSync(filePath).isDirectory()) {
dirs.push(filePath);
}
}
for (var _a = 0, dirs_1 = dirs; _a < dirs_1.length; _a++) {
var dr = dirs_1[_a];
var result = findAppDidFinishLaunchingWithOptions(dr);
if (result)
return result;
}
return null;
}
function addCodeSnippetToProject(projPath, files, dsn) {
var appDelegate = findAppDidFinishLaunchingWithOptions(projPath, files);
if (!appDelegate) {
return false;
}
var fileContent = fs.readFileSync(appDelegate, 'utf8');
var isSwift = appDelegate.toLowerCase().endsWith('.swift');
var appLaunchRegex = isSwift ? swiftAppLaunchRegex : objcAppLaunchRegex;
var importStatement = isSwift ? 'import Sentry\n' : '@import Sentry;\n';
var checkForSentryInit = isSwift ? 'SentrySDK.start' : '[SentrySDK start';
var codeSnippet = isSwift
? templates.getSwiftSnippet(dsn)
: templates.getObjcSnippet(dsn);
Sentry.setTag('code-language', isSwift ? 'swift' : 'objc');
Sentry.setTag('ui-engine', swiftUIRegex.test(fileContent) ? 'swiftui' : 'uikit');
if (fileContent.includes(checkForSentryInit)) {
//already initialized
prompts_1.default.log.info('Sentry is already initialized in your AppDelegate. Skipping adding the code snippet.');
return true;
}
var match = appLaunchRegex.exec(fileContent);
if (!match) {
var swiftUIMatch = swiftUIRegex.exec(fileContent);
if (!swiftUIMatch) {
return false;
}
//Is SwiftUI with no init
match = swiftUIMatch;
codeSnippet = " init() {\n".concat(codeSnippet, " }");
}
var insertIndex = match.index + match[0].length;
var newFileContent = fileContent.slice(0, insertIndex) +
'\n' +
codeSnippet +
fileContent.slice(insertIndex);
if (newFileContent.indexOf(importStatement) < 0) {
var firstImport = /^[ \t]*import +\w+.*$/m.exec(newFileContent);
if (firstImport) {
var importIndex = firstImport.index + firstImport[0].length;
newFileContent =
newFileContent.slice(0, importIndex) +
'\n' +
importStatement +
newFileContent.slice(importIndex);
}
else {
newFileContent = importStatement + newFileContent;
}
}
fs.writeFileSync(appDelegate, newFileContent, 'utf8');
prompts_1.default.log.step('Added Sentry initialization code snippet to ' + appDelegate);
return true;
}
exports.addCodeSnippetToProject = addCodeSnippetToProject;
//# sourceMappingURL=code-tools.js.map