{"size": 2217, "ext": "js", "max_stars_repo_path": "bin/helpers/util.js", "max_stars_repo_name": "pawanpanjwani/browserstack-cypress-cli", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "bin/helpers/util.js", "max_issues_repo_name": "pawanpanjwani/browserstack-cypress-cli", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "bin/helpers/util.js", "max_forks_repo_name": "pawanpanjwani/browserstack-cypress-cli", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 29.1710526316, "max_line_length": 108, "alphanum_fraction": 0.6968876861}
'use strict';
const os = require("os");

const usageReporting =  require('./usageReporting'),
  logger = require('./logger').winstonLogger,
  Constants = require('./constants');

exports.validateBstackJson = (bsConfigPath) => {
  return new Promise(function(resolve, reject){
    try {
      logger.info(`Reading config from ${bsConfigPath}`);
      let bsConfig = require(bsConfigPath);
      resolve(bsConfig);
    }
    catch (e) {
      reject(e);
    }
  });
}

exports.getErrorCodeFromMsg = (errMsg) => {
  let errorCode = null;
  switch (errMsg) {
    case Constants.validationMessages.EMPTY_BROWSERSTACK_JSON:
      errorCode = "bstack_json_invalid_empty";
      break;
    case Constants.validationMessages.INCORRECT_AUTH_PARAMS:
      errorCode = "bstack_json_invalid_missing_keys";
      break;
    case Constants.validationMessages.EMPTY_BROWSER_LIST:
      errorCode = "bstack_json_invalid_no_browsers";
      break;
    case Constants.validationMessages.EMPTY_RUN_SETTINGS:
      errorCode = "bstack_json_invalid_no_run_settings";
      break;
    case Constants.validationMessages.EMPTY_SPEC_FILES:
      errorCode = "bstack_json_invalid_values";
      break;
  }
  return errorCode;
}

exports.getErrorCodeFromErr = (err) => {
  let errorCode = null;
  if (err.code === 'SyntaxError') {
    errorCode = 'bstack_json_parse_error';
  } else if (err.code === 'EACCES') {
    errorCode = 'bstack_json_no_permission';
  } else {
    errorCode = 'bstack_json_invalid_unknown';
  }
  return errorCode
}

exports.sendUsageReport = (bsConfig, args, message, message_type, error_code) => {
  usageReporting.send({
    cli_args: args,
    message: message,
    message_type: message_type,
    error_code: error_code,
    bstack_config: bsConfig
  });
}

exports.setUsageReportingFlag = (bsConfig, disableUsageReporting) => {
  if (disableUsageReporting === undefined && bsConfig && bsConfig['disable_usage_reporting'] != undefined) {
    process.env.DISABLE_USAGE_REPORTING = bsConfig['disable_usage_reporting'];
  } else {
    process.env.DISABLE_USAGE_REPORTING = disableUsageReporting;
  }
}

exports.getUserAgent = () => {
  return `BStack-Cypress-CLI/1.x (${os.arch()}/${os.platform()}/${os.release()})`;
}
