'use strict';Object.defineProperty(exports, "__esModule", { value: true });exports.runCLI = exports.run = undefined;let run = exports.run = (() => {var _ref = _asyncToGenerator(


































  function* (maybeArgv, project) {
    const argv = _buildArgv(maybeArgv, project);
    const projects = _getProjectListFromCLIArgs(argv, project);
    // If we're running a single Jest project, we might want to use another
    // version of Jest (the one that is specified in this project's package.json)
    const runCLIFn = _getRunCLIFn(projects);var _ref2 =

    yield runCLIFn(argv, projects);const results = _ref2.results,globalConfig = _ref2.globalConfig;
    _readResultsAndExit(results, globalConfig);
  });return function run(_x, _x2) {return _ref.apply(this, arguments);};})();var _jestUtil;function _load_jestUtil() {return _jestUtil = require('jest-util');}var _jestConfig;function _load_jestConfig() {return _jestConfig = require('jest-config');}var _package;function _load_package() {return _package = require('../../package.json');}var _args;function _load_args() {return _args = _interopRequireWildcard(require('./args'));}var _chalk;function _load_chalk() {return _chalk = _interopRequireDefault(require('chalk'));}var _create_context;function _load_create_context() {return _create_context = _interopRequireDefault(require('../lib/create_context'));}var _get_changed_files_promise;function _load_get_changed_files_promise() {return _get_changed_files_promise = _interopRequireDefault(require('../get_changed_files_promise'));}var _get_jest;function _load_get_jest() {return _get_jest = _interopRequireDefault(require('./get_jest'));}var _handle_deprecation_warnings;function _load_handle_deprecation_warnings() {return _handle_deprecation_warnings = _interopRequireDefault(require('../lib/handle_deprecation_warnings'));}var _log_debug_messages;function _load_log_debug_messages() {return _log_debug_messages = _interopRequireDefault(require('../lib/log_debug_messages'));}var _pre_run_message;function _load_pre_run_message() {return _pre_run_message = require('../pre_run_message');}var _run_jest;function _load_run_jest() {return _run_jest = _interopRequireDefault(require('../run_jest'));}var _jestRuntime;function _load_jestRuntime() {return _jestRuntime = _interopRequireDefault(require('jest-runtime'));}var _test_watcher;function _load_test_watcher() {return _test_watcher = _interopRequireDefault(require('../test_watcher'));}var _watch;function _load_watch() {return _watch = _interopRequireDefault(require('../watch'));}var _yargs;function _load_yargs() {return _yargs = _interopRequireDefault(require('yargs'));}function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}function _interopRequireWildcard(obj) {if (obj && obj.__esModule) {return obj;} else {var newObj = {};if (obj != null) {for (var key in obj) {if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key];}}newObj.default = obj;return newObj;}}function _asyncToGenerator(fn) {return function () {var gen = fn.apply(this, arguments);return new Promise(function (resolve, reject) {function step(key, arg) {try {var info = gen[key](arg);var value = info.value;} catch (error) {reject(error);return;}if (info.done) {resolve(value);} else {return Promise.resolve(value).then(function (value) {step("next", value);}, function (err) {step("throw", err);});}}return step("next");});};} /**
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            * Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            *
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            * This source code is licensed under the MIT license found in the
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            * LICENSE file in the root directory of this source tree.
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            *
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            * 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            */const runCLI = exports.runCLI = (() => {var _ref3 = _asyncToGenerator(function* (argv, projects) {let results; // Optimize 'fs' module and make it more compatible with multiple platforms.
    _patchGlobalFSModule();

    // If we output a JSON object, we can't write anything to stdout, since
    // it'll break the JSON structure and it won't be valid.
    const outputStream =
    argv.json || argv.useStderr ? process.stderr : process.stdout;

    argv.version && _printVersionAndExit(outputStream);

    try {var _getConfigs2 =
      _getConfigs(
      projects,
      argv,
      outputStream);const globalConfig = _getConfigs2.globalConfig,configs = _getConfigs2.configs,hasDeprecationWarnings = _getConfigs2.hasDeprecationWarnings;


      yield _run(
      globalConfig,
      configs,
      hasDeprecationWarnings,
      outputStream,
      function (r) {return results = r;});


      if (argv.watch || argv.watchAll) {
        // If in watch mode, return the promise that will never resolve.
        // If the watch mode is interrupted, watch should handle the process
        // shutdown.
        return new Promise(function () {});
      }

      if (!results) {
        throw new Error(
        'AggregatedResult must be present after test run is complete');

      }

      return Promise.resolve({ globalConfig, results });
    } catch (error) {
      (0, (_jestUtil || _load_jestUtil()).clearLine)(process.stderr);
      (0, (_jestUtil || _load_jestUtil()).clearLine)(process.stdout);
      console.error((_chalk || _load_chalk()).default.red(error.stack));
      process.exit(1);
      throw error;
    }
  });return function runCLI(_x3, _x4) {return _ref3.apply(this, arguments);};})();

const _readResultsAndExit = (
result,
globalConfig) =>
{
  const code = !result || result.success ? 0 : globalConfig.testFailureExitCode;
  process.on('exit', () => process.exit(code));
  if (globalConfig.forceExit) {
    process.exit(code);
  }
};

const _buildArgv = (maybeArgv, project) => {
  const argv = (0, (_yargs || _load_yargs()).default)(maybeArgv || process.argv.slice(2)).
  usage((_args || _load_args()).usage).
  alias('help', 'h').
  options((_args || _load_args()).options).
  epilogue((_args || _load_args()).docs).
  check((_args || _load_args()).check).
  version(false).argv;

  (0, (_jestUtil || _load_jestUtil()).validateCLIOptions)(argv, (_args || _load_args()).options);

  return argv;
};

const _getProjectListFromCLIArgs = (argv, project) => {
  const projects = argv.projects ? argv.projects : [];

  if (project) {
    projects.push(project);
  }

  if (!projects.length) {
    projects.push(process.cwd());
  }

  return projects;
};

const _getRunCLIFn = projects =>
projects.length === 1 ? (0, (_get_jest || _load_get_jest()).default)(projects[0]).runCLI : runCLI;

const _printDebugInfoAndExitIfNeeded = (
argv,
globalConfig,
configs,
outputStream) =>
{
  if (argv.debug || argv.showConfig) {
    (0, (_log_debug_messages || _load_log_debug_messages()).default)(globalConfig, configs, outputStream);
  }
  if (argv.showConfig) {
    process.exit(0);
  }
};

const _printVersionAndExit = outputStream => {
  outputStream.write(`v${(_package || _load_package()).version}\n`);
  process.exit(0);
};

const _ensureNoDuplicateConfigs = (parsedConfigs, projects) => {
  const configPathSet = new Set();

  for (const _ref4 of parsedConfigs) {const configPath = _ref4.configPath;
    if (configPathSet.has(configPath)) {
      let message =
      'One or more specified projects share the same config file\n';

      parsedConfigs.forEach((_ref5, index) => {let configPath = _ref5.configPath;
        message =
        message +
        '\nProject: "' +
        projects[index] +
        '"\nConfig: "' +
        String(configPath) +
        '"';
      });
      throw new Error(message);
    }
    configPathSet.add(configPath);
  }
};

// Possible scenarios:
//  1. jest --config config.json
//  2. jest --projects p1 p2
//  3. jest --projects p1 p2 --config config.json
//  4. jest --projects p1
//  5. jest
//
// If no projects are specified, process.cwd() will be used as the default
// (and only) project.
const _getConfigs = (
projectsFromCLIArgs,
argv,
outputStream) =>




{
  let globalConfig;
  let hasDeprecationWarnings;
  let configs = [];
  let projects = projectsFromCLIArgs;

  if (projectsFromCLIArgs.length === 1) {
    const parsedConfig = (0, (_jestConfig || _load_jestConfig()).readConfig)(argv, projects[0]);

    if (parsedConfig.globalConfig.projects) {
      // If this was a single project, and its config has `projects`
      // settings, use that value instead.
      projects = parsedConfig.globalConfig.projects;
    }

    hasDeprecationWarnings = parsedConfig.hasDeprecationWarnings;
    globalConfig = parsedConfig.globalConfig;
    configs = [parsedConfig.projectConfig];
    if (globalConfig.projects && globalConfig.projects.length) {
      // Even though we had one project in CLI args, there might be more
      // projects defined in the config.
      projects = globalConfig.projects;
    }
  }

  if (projects.length > 1) {
    const parsedConfigs = projects.map(root => (0, (_jestConfig || _load_jestConfig()).readConfig)(argv, root, true));
    _ensureNoDuplicateConfigs(parsedConfigs, projects);
    configs = parsedConfigs.map((_ref6) => {let projectConfig = _ref6.projectConfig;return projectConfig;});
    if (!hasDeprecationWarnings) {
      hasDeprecationWarnings = parsedConfigs.some(
      (_ref7) => {let hasDeprecationWarnings = _ref7.hasDeprecationWarnings;return !!hasDeprecationWarnings;});

    }
    // If no config was passed initially, use the one from the first project
    if (!globalConfig) {
      globalConfig = parsedConfigs[0].globalConfig;
    }
  }

  if (!globalConfig || !configs.length) {
    throw new Error('jest: No configuration found for any project.');
  }

  _printDebugInfoAndExitIfNeeded(argv, globalConfig, configs, outputStream);

  return {
    configs,
    globalConfig,
    hasDeprecationWarnings: !!hasDeprecationWarnings };

};

const _patchGlobalFSModule = () => {
  const realFs = require('fs');
  const fs = require('graceful-fs');
  fs.gracefulify(realFs);
};

const _buildContextsAndHasteMaps = (() => {var _ref8 = _asyncToGenerator(function* (
  configs,
  globalConfig,
  outputStream)
  {
    const hasteMapInstances = Array(configs.length);
    const contexts = yield Promise.all(
    configs.map((() => {var _ref9 = _asyncToGenerator(function* (config, index) {
        (0, (_jestUtil || _load_jestUtil()).createDirectory)(config.cacheDirectory);
        const hasteMapInstance = (_jestRuntime || _load_jestRuntime()).default.createHasteMap(config, {
          console: new (_jestUtil || _load_jestUtil()).Console(outputStream, outputStream),
          maxWorkers: globalConfig.maxWorkers,
          resetCache: !config.cache,
          watch: globalConfig.watch || globalConfig.watchAll,
          watchman: globalConfig.watchman });

        hasteMapInstances[index] = hasteMapInstance;
        return (0, (_create_context || _load_create_context()).default)(config, (yield hasteMapInstance.build()));
      });return function (_x8, _x9) {return _ref9.apply(this, arguments);};})()));


    return { contexts, hasteMapInstances };
  });return function _buildContextsAndHasteMaps(_x5, _x6, _x7) {return _ref8.apply(this, arguments);};})();

const _run = (() => {var _ref10 = _asyncToGenerator(function* (
  globalConfig,
  configs,
  hasDeprecationWarnings,
  outputStream,
  onComplete)
  {
    // Queries to hg/git can take a while, so we need to start the process
    // as soon as possible, so by the time we need the result it's already there.
    const changedFilesPromise = (0, (_get_changed_files_promise || _load_get_changed_files_promise()).default)(globalConfig, configs);var _ref11 =
    yield _buildContextsAndHasteMaps(
    configs,
    globalConfig,
    outputStream);const contexts = _ref11.contexts,hasteMapInstances = _ref11.hasteMapInstances;


    globalConfig.watch || globalConfig.watchAll ?
    yield _runWatch(
    contexts,
    configs,
    hasDeprecationWarnings,
    globalConfig,
    outputStream,
    hasteMapInstances,
    changedFilesPromise) :

    yield _runWithoutWatch(
    globalConfig,
    contexts,
    outputStream,
    onComplete,
    changedFilesPromise);

  });return function _run(_x10, _x11, _x12, _x13, _x14) {return _ref10.apply(this, arguments);};})();

const _runWatch = (() => {var _ref12 = _asyncToGenerator(function* (
  contexts,
  configs,
  hasDeprecationWarnings,
  globalConfig,
  outputStream,
  hasteMapInstances,
  changedFilesPromise)
  {
    if (hasDeprecationWarnings) {
      try {
        yield (0, (_handle_deprecation_warnings || _load_handle_deprecation_warnings()).default)(outputStream, process.stdin);
        return (0, (_watch || _load_watch()).default)(globalConfig, contexts, outputStream, hasteMapInstances);
      } catch (e) {
        process.exit(0);
      }
    }

    return (0, (_watch || _load_watch()).default)(globalConfig, contexts, outputStream, hasteMapInstances);
  });return function _runWatch(_x15, _x16, _x17, _x18, _x19, _x20, _x21) {return _ref12.apply(this, arguments);};})();

const _runWithoutWatch = (() => {var _ref13 = _asyncToGenerator(function* (
  globalConfig,
  contexts,
  outputStream,
  onComplete,
  changedFilesPromise)
  {
    const startRun = (() => {var _ref14 = _asyncToGenerator(function* () {
        if (!globalConfig.listTests) {
          (0, (_pre_run_message || _load_pre_run_message()).print)(outputStream);
        }
        return yield (0, (_run_jest || _load_run_jest()).default)({
          changedFilesPromise,
          contexts,
          globalConfig,
          onComplete,
          outputStream,
          startRun,
          testWatcher: new (_test_watcher || _load_test_watcher()).default({ isWatchMode: false }) });

      });return function startRun() {return _ref14.apply(this, arguments);};})();
    return yield startRun();
  });return function _runWithoutWatch(_x22, _x23, _x24, _x25, _x26) {return _ref13.apply(this, arguments);};})();