"use strict";

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

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

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

exports.default = void 0;

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

var _filter = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/filter"));

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

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

var _fs = require("fs");

var _path = require("path");

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

const isDirectory = source => (0, _fs.lstatSync)(source).isDirectory();

const getDirectories = source => {
  var _context, _context2;

  return (0, _filter.default)(_context = (0, _map.default)(_context2 = (0, _fs.readdirSync)(source)).call(_context2, name => ({
    path: (0, _path.join)(source, name),
    name: name
  }))).call(_context, item => isDirectory(item.path));
};

var _default = doLinking => (0, _plugin.default)('LINK APPS', ({
  reporter
}) => /*#__PURE__*/(0, _asyncToGenerator2.default)(function* () {
  // if doLinking is falsy then just leave
  if (doLinking === false) {
    return _promise.default.resolve();
  }

  const appPath = './apps';

  if (!(0, _fs.existsSync)(appPath)) {
    reporter.emit('message', 'No apps directory found. Not attempting a link');
    return _promise.default.resolve();
  }

  return new _promise.default((resolve, reject) => {
    const {
      link
    } = require('linklocal');

    const apps = getDirectories(appPath);
    (0, _map.default)(apps).call(apps, app => {
      link.recursive(app.path, () => {});
    });
    resolve();
  });
}));

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