"use strict";

var _interopRequireWildcard = require("@babel/runtime-corejs3/helpers/interopRequireWildcard");

var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault");

require("core-js/modules/es.array.iterator");

require("core-js/modules/es.promise");

require("core-js/modules/es.string.replace");

var _Object$defineProperty = require("@babel/runtime-corejs3/core-js-stable/object/define-property");

var _mapInstanceProperty = require("@babel/runtime-corejs3/core-js-stable/instance/map");

_Object$defineProperty(exports, "__esModule", {
  value: true
});

exports.default = void 0;

var _promise = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/promise"));

var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/asyncToGenerator"));

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

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

var _util = require("util");

var _plugin = _interopRequireDefault(require("@start/plugin"));

var _lodash = require("lodash");

const readdir = (0, _util.promisify)(_fs.default.readdir);
const mkdir = (0, _util.promisify)(_fs.default.mkdir);
const exists = (0, _util.promisify)(_fs.default.exists);
const writeFile = (0, _util.promisify)(_fs.default.writeFile);
const readFile = (0, _util.promisify)(_fs.default.readFile);
const appendFile = (0, _util.promisify)(_fs.default.appendFile);
const lstat = (0, _util.promisify)(_fs.default.lstat);

var _default = outputDir => (0, _plugin.default)('COPY DOCS', ({
  reporter
}) =>
/*#__PURE__*/
(0, _asyncToGenerator2.default)(function* () {
  const repoPath = (0, _path.resolve)(process.cwd());
  const docsDir = (0, _path.resolve)(repoPath, outputDir);

  if (!(yield exists(docsDir))) {
    yield mkdir(docsDir);
  }

  const loadReadme =
  /*#__PURE__*/
  function () {
    var _ref2 = (0, _asyncToGenerator2.default)(function* (file, location) {
      const readme = _path.default.join(location, 'README.md');

      if (yield exists(readme)) {
        const packageJsonFile = _path.default.join(location, 'package.json');

        let version = '';

        if (exists(packageJsonFile)) {
          const packageJsonObj = JSON.parse(_fs.default.readFileSync(packageJsonFile, 'utf8'));
          version = packageJsonObj.version;
        }

        const content = yield readFile(readme, 'utf8');

        if (content) {
          return content.replace('# ' + file, '## ' + file + ' ' + version);
        }
      }
    });

    return function loadReadme(_x, _x2) {
      return _ref2.apply(this, arguments);
    };
  }();

  try {
    const packageDirectory = _path.default.join(repoPath, 'packages');

    const packageNames = yield readdir(packageDirectory);
    yield _promise.default.all((0, _mapInstanceProperty(_lodash))(packageNames,
    /*#__PURE__*/
    function () {
      var _ref3 = (0, _asyncToGenerator2.default)(function* (packageName) {
        const packageDirName = _path.default.join(packageDirectory, packageName);

        const isDirectory = (yield lstat(packageDirName)).isDirectory();

        if (isDirectory) {
          const readme = yield loadReadme(packageName, packageDirName);

          if (readme) {
            const fileName = packageName + '.md';

            const readmeFilePath = _path.default.join(docsDir, fileName);

            yield writeFile(readmeFilePath, readme, 'utf8');
          }
        }
      });

      return function (_x3) {
        return _ref3.apply(this, arguments);
      };
    }()));
  } catch (e) {
    reporter.emit('error', 'Copy docs', e);
  }
}));

exports.default = _default;
module.exports = exports.default;