"use strict";

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

var _path = _interopRequireWildcard(require("path"));

var _fs = require("fs");

var _child_process = require("child_process");

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function runPreProcess({
  options,
  option,
  nodemon,
  spawnSync
}) {
  const {
    preprocess
  } = options;
  const preprocessorPath = preprocess.runner ? _path.default.join(process.cwd(), preprocess.runner) : '';
  const preprocessCli = preprocess.stopNodemon ? 'node' : nodemon;

  if (preprocessorPath && (0, _fs.existsSync)(preprocessorPath)) {
    const preprocessorDirPath = (0, _path.dirname)(preprocessorPath);
    const watchOptions = preprocessCli === nodemon ? ['--watch', preprocessorDirPath] : []; // eslint-disable-next-line default-case

    switch (option) {
      case 'start':
      case 'docs':
        (0, _child_process.spawn)(preprocessCli, [preprocessorPath, ...watchOptions], {
          stdio: 'inherit',
          cwd: preprocessorDirPath
        }); // NOTE:  it's ok if we not close this here
        // Because when node server stops this program will be closed So this  nodemon  will be killed  as well

        break;

      case 'nowatchstart':
      case 'devbuild':
      case 'build:library:es':
      case 'build:component:es':
      case 'build:library:cmjs':
      case 'build:component:cmjs':
        spawnSync('node', [preprocessorPath], {
          stdio: 'inherit',
          cwd: preprocessorPath.slice(0, preprocessorPath.lastIndexOf('/') + 1)
        });
        break;

      case 'preprocessor':
        {
          const result = spawnSync(preprocessCli, [preprocessorPath, ...watchOptions], {
            stdio: 'inherit',
            cwd: preprocessorDirPath
          });
          process.exit(result.status);
          break;
        }
    }
  } else if (option === 'preprocess') {
    console.error(`preProcessor not exists ${preprocessorPath}`);
    process.exit(0);
  }
}