"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.runPreProcess = runPreProcess;

var _path = require("path");

var _fs = require("fs");

var _spawnSyncIO = require("../commands-utils/spawnSyncIO");

var _logger = require("../../logger");

var _constants = require("../constants");

function runPreProcess(options, runnerCli = 'node') {
  const {
    preProcess
  } = options;
  const {
    enable,
    runner,
    stopNodemon
  } = preProcess;

  if (!enable || !runner) {
    return null;
  }

  const preprocessorPath = (0, _constants.joinWithAppPath)(runner);
  let result = null;

  if ((0, _fs.existsSync)(preprocessorPath)) {
    const preprocessorDirPath = (0, _path.dirname)(preprocessorPath);
    const watchOptions = ['--watch', preprocessorDirPath];
    const preprocessCli = stopNodemon ? 'node' : runnerCli;

    if (preprocessCli === 'node') {
      result = (0, _spawnSyncIO.spawnSyncIO)(preprocessCli, [preprocessorPath], {
        stdio: 'inherit',
        cwd: preprocessorDirPath
      });
      (0, _logger.messageLogger)('pre_processor ran successfully');
    } else {
      result = (0, _spawnSyncIO.spawnIO)(preprocessCli, [preprocessorPath, ...watchOptions], {
        stdio: 'inherit',
        cwd: preprocessorDirPath
      });
    }
  } else {
    (0, _logger.errorLogger)(`preProcessor not exists ${preprocessorPath}`);
    process.exit(0);
  }

  return result;
}