"use strict";

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

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = exports.DIAGNOSIS_NAME = void 0;

function _bluebird() {
  const data = require("bluebird");

  _bluebird = function () {
    return data;
  };

  return data;
}

function _defineProperty2() {
  const data = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

  _defineProperty2 = function () {
    return data;
  };

  return data;
}

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

  _path = function () {
    return data;
  };

  return data;
}

function _fsExtra() {
  const data = _interopRequireDefault(require("fs-extra"));

  _fsExtra = function () {
    return data;
  };

  return data;
}

function _glob() {
  const data = _interopRequireDefault(require("glob"));

  _glob = function () {
    return data;
  };

  return data;
}

function _ramda() {
  const data = _interopRequireDefault(require("ramda"));

  _ramda = function () {
    return data;
  };

  return data;
}

function _diagnosis() {
  const data = _interopRequireDefault(require("../diagnosis"));

  _diagnosis = function () {
    return data;
  };

  return data;
}

function _consumer() {
  const data = require("../../consumer");

  _consumer = function () {
    return data;
  };

  return data;
}

function _scope() {
  const data = require("../../scope");

  _scope = function () {
    return data;
  };

  return data;
}

const DIAGNOSIS_NAME = 'check environment symlinks';
exports.DIAGNOSIS_NAME = DIAGNOSIS_NAME;

class BrokenSymlinkFiles extends _diagnosis().default {
  constructor(...args) {
    super(...args);
    (0, _defineProperty2().default)(this, "name", DIAGNOSIS_NAME);
    (0, _defineProperty2().default)(this, "description", 'validate generated symlinks for workspace environments');
    (0, _defineProperty2().default)(this, "category", 'local environments');
  }

  _formatSymptoms(bareResult) {
    if (!bareResult.data) throw new Error('BrokenSymlinkFiles, bareResult.data is missing');
    const toString = bareResult.data.brokenSymlinks.map(brokenSymlink => `symlink path: "${brokenSymlink.symlinkPath}", broken link: "${brokenSymlink.brokenPath}"`).join('\n');
    return `the following symlinks points to non-existing paths\n${toString}`;
  }

  _formatManualTreat(bareResult) {
    if (!bareResult.data) throw new Error('BrokenSymlinkFiles, bareResult.data is missing');

    const toString = _ramda().default.uniq(bareResult.data.brokenSymlinks.map(b => b.pathToDelete)).join('\n');

    return `please delete the following paths:\n${toString}`;
  }

  _runExamine() {
    var _this = this;

    return (0, _bluebird().coroutine)(function* () {
      const consumer = yield (0, _consumer().loadConsumer)();

      const envComponentsDir = _path().default.join(consumer.scope.getPath(), _scope().Scope.getComponentsRelativePath());

      const potentialSymlinks = _glob().default.sync('**/node_modules/@bit/**', {
        cwd: envComponentsDir
      });

      const potentialSymlinksAbs = potentialSymlinks.map(p => _path().default.join(envComponentsDir, p));
      const brokenSymlinks = [];
      const results = potentialSymlinksAbs.map(
      /*#__PURE__*/
      function () {
        var _ref = (0, _bluebird().coroutine)(function* (potentialSymlink) {
          const link = yield _this._getLinkIfExist(potentialSymlink);
          if (!link) return;
          const exists = yield _fsExtra().default.exists(link);
          if (exists) return;
          const brokenSymlink = {
            symlinkPath: potentialSymlink,
            brokenPath: link,
            pathToDelete: _this._getPathToDelete(potentialSymlink)
          };
          brokenSymlinks.push(brokenSymlink);
        });

        return function (_x) {
          return _ref.apply(this, arguments);
        };
      }());
      yield Promise.all(results);
      return {
        valid: brokenSymlinks.length === 0,
        data: {
          brokenSymlinks
        }
      };
    })();
  }

  _getLinkIfExist(symlinkPath) {
    return (0, _bluebird().coroutine)(function* () {
      try {
        const link = yield _fsExtra().default.readlink(symlinkPath);
        return link;
      } catch (err) {
        // probably not a symlink
        return null;
      }
    })();
  }
  /**
   * deleting the environment directory causes Bit to reinstall it next time
   */


  _getPathToDelete(symlinkPath) {
    const envDirectory = symlinkPath.split(_path().default.join('node_modules', '@bit'))[0];
    return envDirectory.slice(0, -1); // remove the last slash
  }

}

exports.default = BrokenSymlinkFiles;