UNPKG

@sentry/wizard

Version:

Sentry wizard helping you to configure your project

136 lines 6.07 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; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.SentryCli = void 0; const fs = __importStar(require("node:fs")); const path = __importStar(require("node:path")); const Git_1 = require("./Git"); const Logging_1 = require("./Logging"); const SENTRYCLIRC_FILENAME = '.sentryclirc'; const GITIGNORE_FILENAME = '.gitignore'; const PROPERTIES_FILENAME = 'sentry.properties'; class SentryCli { _argv; // eslint-disable-next-line @typescript-eslint/typedef _resolve = require.resolve; constructor(_argv) { this._argv = _argv; } setResolveFunction(resolve) { this._resolve = resolve; } convertAnswersToProperties(answers) { const props = {}; props['defaults/url'] = this._argv.url; props['defaults/org'] = answers.config?.organization?.slug ?? null; props['defaults/project'] = answers.config?.project?.slug ?? null; props['auth/token'] = answers.config?.auth?.token ?? null; try { const 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 everything as it is } return props; } /** Create the contents of a `sentry.properties` file */ dumpProperties(props) { const rv = []; for (const [key, value] of Object.entries(props)) { const normalizedKey = key.replace(/\//g, '.'); if (value === undefined || value === null) { // comment that property out since it has no value rv.push(`#${normalizedKey}=`); } else { rv.push(`${normalizedKey}=${value}`); } } // eslint-disable-next-line prefer-template return rv.join('\n') + '\n'; } dumpConfig(config) { const dumpedSections = []; for (const [sectionName, val] of Object.entries(config)) { const props = this.dumpProperties(val); const section = `[${sectionName}]\n${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 */ async createSentryCliConfig(cliProps) { const { 'auth/token': authToken, ...cliPropsToWrite } = cliProps; /** * To not commit the auth token to the VCS, instead of adding it to the * properties file (like the rest of props), it's added to the Sentry CLI * config, which is added to the gitignore. This way makes the properties * file safe to commit without exposing any auth tokens. */ if (authToken) { try { await fs.promises.appendFile(SENTRYCLIRC_FILENAME, this.dumpConfig({ auth: { token: authToken } })); (0, Logging_1.green)(`✓ Successfully added the auth token to ${SENTRYCLIRC_FILENAME}`); } catch { (0, Logging_1.red)(`⚠ Could not add the auth token to ${SENTRYCLIRC_FILENAME}, ` + `please add it to identify your user account:\n${authToken}`); (0, Logging_1.nl)(); } } else { (0, Logging_1.red)(`⚠ Did not find an auth token, please add your token to ${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'); } await (0, Git_1.addToGitignore)(SENTRYCLIRC_FILENAME, `⚠ Could not add ${SENTRYCLIRC_FILENAME} to ${GITIGNORE_FILENAME}, please add it to not commit your auth key.`); try { await fs.promises.writeFile(`./${PROPERTIES_FILENAME}`, this.dumpProperties(cliPropsToWrite)); (0, Logging_1.green)('✓ Successfully created sentry.properties'); } catch { (0, Logging_1.red)(`⚠ Could not add org and project data to ${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'); } (0, Logging_1.nl)(); } } exports.SentryCli = SentryCli; //# sourceMappingURL=SentryCli.js.map