UNPKG

@sentry/wizard

Version:

Sentry wizard helping you to configure your project

208 lines 10.8 kB
"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 __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.SentryCli = void 0; var fs = __importStar(require("fs")); var _ = __importStar(require("lodash")); var path = __importStar(require("path")); var Git_1 = require("./Git"); var Logging_1 = require("./Logging"); var SENTRYCLIRC_FILENAME = '.sentryclirc'; var GITIGNORE_FILENAME = '.gitignore'; var PROPERTIES_FILENAME = 'sentry.properties'; var SentryCli = /** @class */ (function () { function SentryCli(_argv) { this._argv = _argv; // eslint-disable-next-line @typescript-eslint/typedef this._resolve = require.resolve; } SentryCli.prototype.setResolveFunction = function (resolve) { this._resolve = resolve; }; SentryCli.prototype.convertAnswersToProperties = function (answers) { var props = {}; props['defaults/url'] = this._argv.url; props['defaults/org'] = _.get(answers, 'config.organization.slug', null); props['defaults/project'] = _.get(answers, 'config.project.slug', null); props['auth/token'] = _.get(answers, 'config.auth.token', null); try { var cliPath = this._resolve('@sentry/cli/bin/sentry-cli', { paths: [process.cwd()], }); props['cli/executable'] = path .relative(process.cwd(), cliPath) .replace(/\\/g, '\\\\'); } catch (e) { // we do nothing and leave everyting as it is } return props; }; /** Create the contents of a `sentry.properties` file */ SentryCli.prototype.dumpProperties = function (props) { var rv = []; for (var key in props) { // eslint-disable-next-line no-prototype-builtins if (props.hasOwnProperty(key)) { var value = props[key]; key = key.replace(/\//g, '.'); if (value === undefined || value === null) { // comment that property out since it has no value rv.push("#".concat(key, "=")); } else { rv.push("".concat(key, "=").concat(value)); } } } // eslint-disable-next-line prefer-template return rv.join('\n') + '\n'; }; SentryCli.prototype.dumpConfig = function (config) { var dumpedSections = []; for (var sectionName in config) { // eslint-disable-next-line no-prototype-builtins if (config.hasOwnProperty(sectionName)) { var props = this.dumpProperties(config[sectionName]); var section = "[".concat(sectionName, "]\n").concat(props); dumpedSections.push(section); } } return dumpedSections.join('\n'); }; /** * Creates `.sentryclirc` and `sentry.properties` files with the CLI properties * obtained from the user answers (or from logging into Sentry). * The `.sentryclirc` only contains the auth token and will be added to the * user's `.gitignore` file. The properties file contains the rest of the * properties (org, project, etc.). * * @param sentryCli instance of the Sentry CLI * @param cliProps the properties to write to the files */ SentryCli.prototype.createSentryCliConfig = function (cliProps) { return __awaiter(this, void 0, void 0, function () { var authToken, cliPropsToWrite, _a, _b; return __generator(this, function (_c) { switch (_c.label) { case 0: authToken = cliProps["auth/token"], cliPropsToWrite = __rest(cliProps, ['auth/token']); if (!authToken) return [3 /*break*/, 5]; _c.label = 1; case 1: _c.trys.push([1, 3, , 4]); return [4 /*yield*/, fs.promises.appendFile(SENTRYCLIRC_FILENAME, this.dumpConfig({ auth: { token: authToken } }))]; case 2: _c.sent(); (0, Logging_1.green)("\u2713 Successfully added the auth token to ".concat(SENTRYCLIRC_FILENAME)); return [3 /*break*/, 4]; case 3: _a = _c.sent(); (0, Logging_1.red)("\u26A0 Could not add the auth token to ".concat(SENTRYCLIRC_FILENAME, ", ") + "please add it to identify your user account:\n".concat(authToken)); (0, Logging_1.nl)(); return [3 /*break*/, 4]; case 4: return [3 /*break*/, 6]; case 5: (0, Logging_1.red)("\u26A0 Did not find an auth token, please add your token to ".concat(SENTRYCLIRC_FILENAME)); (0, Logging_1.l)('To generate an auth token, visit https://sentry.io/settings/account/api/auth-tokens/'); (0, Logging_1.l)('To learn how to configure Sentry CLI, visit ' + 'https://docs.sentry.io/platforms/javascript/guides/nextjs/manual-setup/#configure-sentry-cli'); _c.label = 6; case 6: return [4 /*yield*/, (0, Git_1.addToGitignore)(SENTRYCLIRC_FILENAME, "\u26A0 Could not add ".concat(SENTRYCLIRC_FILENAME, " to ").concat(GITIGNORE_FILENAME, ", ") + 'please add it to not commit your auth key.')]; case 7: _c.sent(); _c.label = 8; case 8: _c.trys.push([8, 10, , 11]); return [4 /*yield*/, fs.promises.writeFile("./".concat(PROPERTIES_FILENAME), this.dumpProperties(cliPropsToWrite))]; case 9: _c.sent(); (0, Logging_1.green)('✓ Successfully created sentry.properties'); return [3 /*break*/, 11]; case 10: _b = _c.sent(); (0, Logging_1.red)("\u26A0 Could not add org and project data to ".concat(PROPERTIES_FILENAME)); (0, Logging_1.l)('See docs for a manual setup: https://docs.sentry.io/platforms/javascript/guides/nextjs/manual-setup/#configure-sentry-cli'); return [3 /*break*/, 11]; case 11: (0, Logging_1.nl)(); return [2 /*return*/]; } }); }); }; return SentryCli; }()); exports.SentryCli = SentryCli; //# sourceMappingURL=SentryCli.js.map