@sentry/wizard
Version:
Sentry wizard helping you to configure your project
292 lines • 16.7 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 __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.runAppleWizard = void 0;
/* eslint-disable max-lines */
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
/* eslint-disable @typescript-eslint/no-unsafe-call */
/* eslint-disable @typescript-eslint/no-unused-vars */
// @ts-ignore - clack is ESM and TS complains about that. It works though
var prompts_1 = __importDefault(require("@clack/prompts"));
var fs = __importStar(require("fs"));
var path = __importStar(require("path"));
var xcode_manager_1 = require("./xcode-manager");
var codeTools = __importStar(require("./code-tools"));
var bash = __importStar(require("../utils/bash"));
var SentryUtils = __importStar(require("../utils/sentrycli-utils"));
var Sentry = __importStar(require("@sentry/node"));
var telemetry_1 = require("../telemetry");
var cocoapod = __importStar(require("./cocoapod"));
var fastlane = __importStar(require("./fastlane"));
var xcode = require('xcode');
/* eslint-enable @typescript-eslint/no-unused-vars */
var clack_utils_1 = require("../utils/clack-utils");
function runAppleWizard(options) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, (0, telemetry_1.withTelemetry)({
enabled: options.telemetryEnabled,
integration: 'ios',
}, function () { return runAppleWizardWithTelementry(options); })];
});
});
}
exports.runAppleWizard = runAppleWizard;
function runAppleWizardWithTelementry(options) {
return __awaiter(this, void 0, void 0, function () {
var hasCli, projectDir, xcodeProjFiles, xcodeProjFile, pbxproj, _a, project, apiKey, xcProject, availableTargets, target, _b, hasCocoa, pm, podAdded, codeAdded, hasFastlane, addLane, added;
return __generator(this, function (_c) {
switch (_c.label) {
case 0:
(0, clack_utils_1.printWelcome)({
wizardName: 'Sentry Apple Wizard',
promoCode: options.promoCode,
});
return [4 /*yield*/, (0, clack_utils_1.confirmContinueIfNoOrDirtyGitRepo)()];
case 1:
_c.sent();
hasCli = bash.hasSentryCLI();
Sentry.setTag('has-cli', hasCli);
if (!!hasCli) return [3 /*break*/, 5];
return [4 /*yield*/, (0, telemetry_1.traceStep)('Ask for SentryCLI', function () { return (0, clack_utils_1.askToInstallSentryCLI)(); })];
case 2:
if (!!(_c.sent())) return [3 /*break*/, 3];
prompts_1.default.log.warn("Without sentry-cli, you won't be able to upload debug symbols to Sentry. You can install it later by following the instructions at https://docs.sentry.io/cli/");
Sentry.setTag('CLI-Installed', false);
return [3 /*break*/, 5];
case 3: return [4 /*yield*/, bash.installSentryCLI()];
case 4:
_c.sent();
Sentry.setTag('CLI-Installed', true);
_c.label = 5;
case 5:
projectDir = process.cwd();
xcodeProjFiles = searchXcodeProject(projectDir);
if (!(!xcodeProjFiles || xcodeProjFiles.length === 0)) return [3 /*break*/, 7];
prompts_1.default.log.error('No Xcode project found. Please run this command from the root of your project.');
return [4 /*yield*/, (0, clack_utils_1.abort)()];
case 6:
_c.sent();
return [2 /*return*/];
case 7:
if (!(xcodeProjFiles.length === 1)) return [3 /*break*/, 8];
xcodeProjFile = xcodeProjFiles[0];
Sentry.setTag('multiple-projects', false);
return [3 /*break*/, 10];
case 8:
Sentry.setTag('multiple-projects', true);
return [4 /*yield*/, (0, telemetry_1.traceStep)('Choose Xcode project', function () {
return (0, clack_utils_1.askForItemSelection)(xcodeProjFiles, 'Which project do you want to add Sentry to?');
})];
case 9:
xcodeProjFile = (_c.sent()).value;
_c.label = 10;
case 10:
pbxproj = path.join(projectDir, xcodeProjFile, 'project.pbxproj');
if (!!fs.existsSync(pbxproj)) return [3 /*break*/, 12];
prompts_1.default.log.error("No pbxproj found at ".concat(xcodeProjFile));
return [4 /*yield*/, (0, clack_utils_1.abort)()];
case 11:
_c.sent();
return [2 /*return*/];
case 12: return [4 /*yield*/, getSentryProjectAndApiKey(options)];
case 13:
_a = _c.sent(), project = _a.project, apiKey = _a.apiKey;
xcProject = new xcode_manager_1.XcodeProject(pbxproj);
availableTargets = xcProject.getAllTargets();
if (!(availableTargets.length == 0)) return [3 /*break*/, 15];
prompts_1.default.log.error("No suttable target found in ".concat(xcodeProjFile));
Sentry.setTag('No-Target', true);
return [4 /*yield*/, (0, clack_utils_1.abort)()];
case 14:
_c.sent();
return [2 /*return*/];
case 15:
if (!(availableTargets.length == 1)) return [3 /*break*/, 16];
_b = availableTargets[0];
return [3 /*break*/, 18];
case 16: return [4 /*yield*/, (0, telemetry_1.traceStep)('Choose target', function () {
return (0, clack_utils_1.askForItemSelection)(availableTargets, 'Which target do you want to add Sentry to?');
})];
case 17:
_b = (_c.sent()).value;
_c.label = 18;
case 18:
target = _b;
SentryUtils.createSentryCLIRC(projectDir, { auth_token: apiKey.token });
prompts_1.default.log.info('We created a ".sentryclirc" file in your project directory in order to provide an auth token for Sentry CLI.\nIt was also added to your ".gitignore" file.\nAt your CI enviroment, you can set the SENTRY_AUTH_TOKEN environment variable instead. See https://docs.sentry.io/cli/configuration/#auth-token for more information.');
hasCocoa = cocoapod.usesCocoaPod(projectDir);
Sentry.setTag('cocoapod-exists', hasCocoa);
if (!hasCocoa) return [3 /*break*/, 21];
return [4 /*yield*/, (0, telemetry_1.traceStep)('Choose a package manager', function () {
return (0, clack_utils_1.askForItemSelection)(['Swift Package Manager', 'CocoaPods'], 'Which package manager would you like to use to add Sentry?');
})];
case 19:
pm = (_c.sent()).value;
hasCocoa = pm === 'CocoaPods';
if (!hasCocoa) return [3 /*break*/, 21];
return [4 /*yield*/, (0, telemetry_1.traceStep)('Add CocoaPods reference', function () {
return cocoapod.addCocoaPods(projectDir);
})];
case 20:
podAdded = _c.sent();
Sentry.setTag('cocoapod-added', podAdded);
if (!podAdded) {
prompts_1.default.log.warn("Could not add Sentry pod to your Podfile. You'll have to add it manually.\nPlease follow the instructions at https://docs.sentry.io/platforms/apple/guides/ios/#install");
}
_c.label = 21;
case 21:
Sentry.setTag('package-manager', hasCocoa ? 'cocoapods' : 'SPM');
(0, telemetry_1.traceStep)('Update Xcode project', function () {
xcProject.updateXcodeProject(project, target, apiKey, !hasCocoa, true);
});
codeAdded = (0, telemetry_1.traceStep)('Add code snippet', function () {
var files = xcProject.filesForTarget(target);
if (files === undefined || files.length == 0)
return false;
return codeTools.addCodeSnippetToProject(projectDir, files, project.keys[0].dsn.public);
});
Sentry.setTag('Snippet-Added', codeAdded);
if (!codeAdded) {
prompts_1.default.log.warn('Added the Sentry dependency to your project but could not add the Sentry code snippet. Please add the code snipped manually by following the docs: https://docs.sentry.io/platforms/apple/guides/ios/#configure');
}
hasFastlane = fastlane.fastFile(projectDir);
Sentry.setTag('fastlane-exists', hasFastlane);
if (!hasFastlane) return [3 /*break*/, 24];
return [4 /*yield*/, prompts_1.default.confirm({
message: 'Found a Fastfile in your project. Do you want to configure a lane to upload debug symbols to Sentry?',
})];
case 22:
addLane = _c.sent();
Sentry.setTag('fastlane-desired', addLane);
if (!addLane) return [3 /*break*/, 24];
return [4 /*yield*/, (0, telemetry_1.traceStep)('Configure fastlane', function () {
return fastlane.addSentryToFastlane(projectDir, project.organization.slug, project.slug);
})];
case 23:
added = _c.sent();
Sentry.setTag('fastlane-added', added);
if (added) {
prompts_1.default.log.step('A new step was added to your fastlane file. Now and you build your project with fastlane, debug symbols and source context will be uploaded to Sentry.');
}
else {
prompts_1.default.log.warn('Could not edit your fastlane file to upload debug symbols to Sentry. Please follow the instructions at https://docs.sentry.io/platforms/apple/guides/ios/dsym/#fastlane');
}
_c.label = 24;
case 24:
prompts_1.default.log.success('Sentry was successfully added to your project! Run your project to send your first event to Sentry. Go to Sentry.io to see whether everything is working fine.');
return [2 /*return*/];
}
});
});
}
//Prompt for Sentry project and API key
function getSentryProjectAndApiKey(options) {
return __awaiter(this, void 0, void 0, function () {
var _a, selectedProject, authToken;
return __generator(this, function (_b) {
switch (_b.label) {
case 0: return [4 /*yield*/, (0, clack_utils_1.getOrAskForProjectData)(options)];
case 1:
_a = _b.sent(), selectedProject = _a.selectedProject, authToken = _a.authToken;
return [2 /*return*/, { project: selectedProject, apiKey: { token: authToken } }];
}
});
});
}
function searchXcodeProject(at) {
var projs = findFilesWithExtension(at, '.xcodeproj');
if (projs.length > 0) {
return projs;
}
var workspace = findFilesWithExtension(at, '.xcworkspace');
if (workspace.length == 0) {
return [];
}
var xsworkspacedata = path.join(at, workspace[0], 'contents.xcworkspacedata');
if (!fs.existsSync(xsworkspacedata)) {
return [];
}
var groupRegex = /location *= *"group:([^"]+)"/gim;
var content = fs.readFileSync(xsworkspacedata, 'utf8');
var matches = groupRegex.exec(content);
while (matches) {
var group = matches[1];
var groupPath = path.join(at, group);
if (!group.endsWith('Pods.xcodeproj') &&
group.endsWith('.xcodeproj') &&
fs.existsSync(groupPath)) {
projs.push(group);
}
matches = groupRegex.exec(content);
}
return projs;
}
//find files with the given extension
function findFilesWithExtension(dir, extension) {
var files = fs.readdirSync(dir);
return files.filter(function (file) { return file.endsWith(extension); });
}
//# sourceMappingURL=apple-wizard.js.map