"use strict";

var _postcss = _interopRequireDefault(require("postcss"));

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

var _fs = _interopRequireDefault(require("fs"));

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

var _utils = require("../utils");

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

const cwd = process.cwd();

const src = _path.default.join(cwd, process.argv[2]);

const dist = _path.default.join(cwd, process.argv[3]);

const docopy = process.argv[4] === 'true';
const {
  css: {
    valueReplacer
  }
} = (0, _utils.getOptions)();

if (!_fs.default.existsSync(dist)) {
  _fs.default.mkdirSync(dist, {
    recursive: true
  });
}

(0, _folderIterator.default)(src, dist, docopy ? false : ['.css'], false, (fromPath, toPath) => {
  const css = _fs.default.readFileSync(fromPath);

  if (docopy && !fromPath.endsWith('.css')) {
    _fs.default.writeFileSync(toPath, css);

    return;
  }

  (0, _postcss.default)([valueReplacer && require('../postcss-plugins/ValueReplacer')(valueReplacer)]).process(css, {
    from: fromPath,
    to: toPath
  }).then(result => {
    _fs.default.writeFile(toPath, result.css, () => true);

    if (result.map) {
      _fs.default.writeFile(`${toPath}.map`, result.map, () => true);
    }
  });
});