"use strict";

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

function _react() {
  const data = _interopRequireDefault(require("react"));

  _react = function _react() {
    return data;
  };

  return data;
}

function _chokidar() {
  const data = _interopRequireDefault(require("chokidar"));

  _chokidar = function _chokidar() {
    return data;
  };

  return data;
}

function _path() {
  const data = require("path");

  _path = function _path() {
    return data;
  };

  return data;
}

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

// 按 key 存，值为数组
const watchers = {};

function toAbsolute(p) {
  if ((0, _path().isAbsolute)(p)) {
    return p;
  }

  return (0, _path().join)(process.cwd(), p);
}

function watch(key, files) {
  if (process.env.WATCH_FILES === 'none') return;

  if (!watchers[key]) {
    watchers[key] = [];
  }

  const APP_ROOT = process.env.APP_ROOT;

  const watcher = _chokidar().default.watch(files, {
    ignoreInitial: true,
    cwd: APP_ROOT ? toAbsolute(APP_ROOT) : process.cwd()
  });

  watchers[key].push(watcher);
  return watcher;
}

function unwatch(key) {
  if (!key) {
    return Object.keys(watchers).forEach(unwatch);
  }

  if (watchers[key]) {
    watchers[key].forEach(watcher => {
      watcher.close();
    });
  }
}