"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getOptions = exports.args = void 0;

var _applyValuesToSchema = require("./applyValuesToSchema");

var _cliArgsToObject = require("./cliArgsToObject");

var _defaultConfigValues = _interopRequireDefault(require("./defaultConfigValues"));

var _deprecatedOptionsHandler = require("./deprecatedOptionsHandler");

var _getNpmVersion = require("./getNpmVersion");

var _npmConfigToObject = require("./npmConfigToObject");

var _readOptionsForConfigFile = require("./readOptionsForConfigFile");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

const args = process.argv.slice(2);
exports.args = args;
const processEnv = (0, _npmConfigToObject.npmConfigToObject)();
const cliArgsObject = (0, _cliArgsToObject.cliArgsToObject)(args);
Object.assign(processEnv, cliArgsObject);
let reactCLIOptions = null;

const getOptions = () => {
  if (reactCLIOptions) {
    return reactCLIOptions;
  }

  const appPath = process.cwd();
  const userSchemas = (0, _readOptionsForConfigFile.readOptionsForPackageJson)(appPath); // NOTE: Only config File option will with cli option  config_file

  const configFileSchemas = (0, _readOptionsForConfigFile.readOptionsForConfigFile)(appPath, cliArgsObject.config_file || 'build.config.js');
  const options = (0, _applyValuesToSchema.applyValuesToSchema)(_defaultConfigValues.default, configFileSchemas || userSchemas || {}, processEnv);
  (0, _deprecatedOptionsHandler.deprecatedOptionsHandler)(options); // for future may be for npm 8 edge cases

  options.npmVersion = (0, _getNpmVersion.getNpmVersion)();
  options.cwd = appPath;
  options.packageVersion = process.env.npm_package_version;
  options.extraArgs = process.argv.slice(3); // eslint-disable-next-line prefer-destructuring

  options.commandOption = process.argv[2];
  reactCLIOptions = options; // console.log({ options });

  return options;
};

exports.getOptions = getOptions;