123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- 'use strict';
- const path = require('path');
- const childProcess = require('child_process');
- function getBinaryPath() {
- const parts = [];
- parts.push(__dirname);
- parts.push('..');
- parts.push(`sentry-cli${process.platform === 'win32' ? '.exe' : ''}`);
- return path.resolve(...parts);
- }
- let binaryPath = getBinaryPath();
- function mockBinaryPath(mockPath) {
- binaryPath = mockPath;
- }
- function serializeOptions(schema, options) {
- return Object.keys(schema).reduce((newOptions, option) => {
- const paramValue = options[option];
- if (paramValue === undefined) {
- return newOptions;
- }
- const paramType = schema[option].type;
- const paramName = schema[option].param;
- if (paramType === 'array') {
- if (!Array.isArray(paramValue)) {
- throw new Error(`${option} should be an array`);
- }
- return newOptions.concat(
- paramValue.reduce((acc, value) => acc.concat([paramName, String(value)]), [])
- );
- }
- if (paramType === 'boolean') {
- if (typeof paramValue !== 'boolean') {
- throw new Error(`${option} should be a bool`);
- }
- const invertedParamName = schema[option].invertedParam;
- if (paramValue && paramName !== undefined) {
- return newOptions.concat([paramName]);
- }
- if (!paramValue && invertedParamName !== undefined) {
- return newOptions.concat([invertedParamName]);
- }
- return newOptions;
- }
- return newOptions.concat(paramName, paramValue);
- }, []);
- }
- function prepareCommand(command, schema, options) {
- return command.concat(serializeOptions(schema || {}, options || {}));
- }
- function getPath() {
- return binaryPath;
- }
- function execute(args, live, silent, configFile, config = {}) {
- const env = { ...process.env };
- if (configFile) {
- env.SENTRY_PROPERTIES = configFile;
- }
- if (config.url) {
- env.SENTRY_URL = config.url;
- }
- if (config.authToken) {
- env.SENTRY_AUTH_TOKEN = config.authToken;
- }
- if (config.apiKey) {
- env.SENTRY_API_KEY = config.apiKey;
- }
- if (config.dsn) {
- env.SENTRY_DSN = config.dsn;
- }
- if (config.org) {
- env.SENTRY_ORG = config.org;
- }
- if (config.project) {
- env.SENTRY_PROJECT = config.project;
- }
- if (config.vcsRemote) {
- env.SENTRY_VCS_REMOTE = config.vcsRemote;
- }
- if (config.customHeader) {
- env.CUSTOM_HEADER = config.customHeader;
- }
- return new Promise((resolve, reject) => {
- if (live === true) {
- const output = silent ? 'ignore' : 'inherit';
- const pid = childProcess.spawn(getPath(), args, {
- env,
-
- stdio: ['ignore', output, output],
- });
- pid.on('exit', () => {
- resolve();
- });
- } else {
- childProcess.execFile(getPath(), args, { env }, (err, stdout) => {
- if (err) {
- reject(err);
- } else {
- resolve(stdout);
- }
- });
- }
- });
- }
- function getProjectFlagsFromOptions({ projects = [] } = {}) {
- return projects.reduce((flags, project) => flags.concat('-p', project), []);
- }
- module.exports = {
- execute,
- getPath,
- getProjectFlagsFromOptions,
- mockBinaryPath,
- prepareCommand,
- serializeOptions,
- };
|