"use strict";

var _getOptions = _interopRequireDefault(require("./getOptions"));

var _folderIterator = _interopRequireDefault(require("./folderIterator"));

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

const postcss = require('postcss');

const path = require('path');

const fs = require('fs');

const SelectorReplacePlugin = require('../postcss-plugins/SelectorReplace');

const cwd = process.cwd();
const src = path.join(cwd, process.argv[2]);
const dist = path.join(cwd, process.argv[3]);
const options = (0, _getOptions.default)();

function watchHandler(fromPath, toPath) {
  const css = fs.readFileSync(fromPath, 'utf-8'); // console.log(css);

  const {
    css: cssOptions
  } = options; // console.log(cssOptions, appOptions);

  const {
    selectorReplace
  } = cssOptions;
  postcss([SelectorReplacePlugin(selectorReplace)]).process(css, {
    from: fromPath,
    to: toPath
  }).then(result => {
    fs.writeFileSync(toPath, result.css);
  }); // console.log(variableOptions);
}

(0, _folderIterator.default)(src, dist, ['.css'], false, (fromPath, toPath) => {
  // console.log(fromPath, toPath);
  watchHandler(fromPath, toPath);
}); // if (canWacth) {
//   useExitCleanup(() => {
//     fs.unwatchFile(src, watchHandler);
//   });
// }