"use strict";

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

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

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = 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 = _interopRequireWildcard(require("path"));

  path = function () {
    return data;
  };

  return data;
}

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

  _fsExtra = function () {
    return data;
  };

  return data;
}

function _e2eHelper() {
  const data = require("./e2e-helper");

  _e2eHelper = function () {
    return data;
  };

  return data;
}

/* eslint no-console: 0 */
class ScopeHelper {
  // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
  // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
  // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
  constructor(debugMode, scopes, commandHelper, fsHelper) {
    (0, _defineProperty2().default)(this, "debugMode", void 0);
    (0, _defineProperty2().default)(this, "scopes", void 0);
    (0, _defineProperty2().default)(this, "e2eDir", void 0);
    (0, _defineProperty2().default)(this, "command", void 0);
    (0, _defineProperty2().default)(this, "fs", void 0);
    (0, _defineProperty2().default)(this, "cache", void 0);
    (0, _defineProperty2().default)(this, "keepEnvs", void 0);
    (0, _defineProperty2().default)(this, "clonedScopes", []);
    this.keepEnvs = !!process.env.npm_config_keep_envs; // default = false

    this.scopes = scopes;
    this.command = commandHelper;
    this.fs = fsHelper;
  }

  clean() {
    _fsExtra().default.emptyDirSync(this.scopes.localPath);

    _fsExtra().default.emptyDirSync(this.scopes.remotePath);
  }

  destroy() {
    if (this.keepEnvs) return;

    _fsExtra().default.removeSync(this.scopes.localPath);

    _fsExtra().default.removeSync(this.scopes.remotePath);

    if (this.cache) {
      // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
      _fsExtra().default.removeSync(this.cache.localScopePath); // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!


      _fsExtra().default.removeSync(this.cache.remoteScopePath);

      delete this.cache;
    }

    if (this.clonedScopes && this.clonedScopes.length) {
      this.clonedScopes.forEach(scopePath => _fsExtra().default.removeSync(scopePath));
    }

    this.fs.cleanExternalDirs();
  }

  cleanLocalScope() {
    _fsExtra().default.emptyDirSync(this.scopes.localPath);
  }

  reInitLocalScope() {
    this.cleanLocalScope();
    this.initLocalScope();
  }

  initLocalScope() {
    return this.initWorkspace();
  }

  initWorkspace(workspacePath) {
    return this.command.runCmd('bit init -N', workspacePath); // return this.command.runCmd('bit init', workspacePath);
  }

  initInteractive(inputs) {
    var _this = this;

    return (0, _bluebird().coroutine)(function* () {
      // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
      // return this.command.runInteractiveCmd({ args: ['init', '--interactive'], inputs });
      return _this.command.runInteractiveCmd({
        args: ['init'],
        inputs
      });
    })();
  }

  initLocalScopeWithOptions(options) {
    const value = Object.keys(options).map(key => `-${key} ${options[key]}`).join(' ');
    return this.command.runCmd(`bit init ${value}`);
  }

  setNewLocalAndRemoteScopes() {
    this.reInitLocalScope();
    this.reInitRemoteScope();
    this.addRemoteScope();
  }

  initNewLocalScope(deleteCurrentScope = true) {
    if (deleteCurrentScope) {
      _fsExtra().default.removeSync(this.scopes.localPath);
    }

    this.scopes.setLocalScope();

    _fsExtra().default.ensureDirSync(this.scopes.localPath);

    return this.initLocalScope();
  }

  addRemoteScope(remoteScopePath = this.scopes.remotePath, localScopePath = this.scopes.localPath, isGlobal = false) {
    const globalArg = isGlobal ? '-g' : '';

    if (process.env.npm_config_with_ssh) {
      return this.command.runCmd(`bit remote add ssh://\`whoami\`@127.0.0.1:/${remoteScopePath} ${globalArg}`, localScopePath);
    }

    return this.command.runCmd(`bit remote add file://${remoteScopePath} ${globalArg}`, localScopePath);
  }

  removeRemoteScope(remoteScope = this.scopes.remote, isGlobal = false) {
    const globalArg = isGlobal ? '-g' : '';
    return this.command.runCmd(`bit remote del ${remoteScope} ${globalArg}`);
  }

  addRemoteEnvironment(isGlobal = false) {
    return this.addRemoteScope(this.scopes.envPath, this.scopes.localPath, isGlobal);
  }

  addGlobalRemoteScope() {
    return this.addRemoteScope(this.scopes.globalRemotePath, this.scopes.localPath);
  }

  removeRemoteEnvironment(isGlobal = false) {
    return this.removeRemoteScope(this.scopes.env, isGlobal);
  } // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!


  reInitRemoteScope(scopePath = this.scopes.remotePath) {
    _fsExtra().default.emptyDirSync(scopePath);

    return this.command.runCmd('bit init --bare', scopePath);
  }
  /**
   * useful when publishing to a local npm registry so then multiple tests in the same file
   * won't collide in the @ci registry
   */


  setRemoteScopeAsDifferentDir() {
    _fsExtra().default.removeSync(this.scopes.remotePath);

    this.scopes.setRemoteScope();
    this.reInitRemoteScope();
    this.addRemoteScope();
  }

  reInitEnvsScope() {
    _fsExtra().default.emptyDirSync(this.scopes.envPath);

    return this.command.runCmd('bit init --bare', this.scopes.envPath);
  } // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!


  getNewBareScope(scopeNameSuffix = '-remote2') {
    const scopeName = (0, _e2eHelper().generateRandomStr)() + scopeNameSuffix;
    const scopePath = path().join(this.scopes.e2eDir, scopeName);

    _fsExtra().default.emptyDirSync(scopePath);

    this.command.runCmd('bit init --bare', scopePath);
    this.addRemoteScope(this.scopes.remotePath, scopePath);
    return {
      scopeName,
      scopePath
    };
  }
  /**
   * Sometimes many tests need to do the exact same steps to init the local-scope, such as importing compiler/tester.
   * To make it faster, use this method before all tests, and then use getClonedLocalScope method to restore from the
   * cloned scope.
   */


  cloneLocalScope() {
    const clonedScope = `${(0, _e2eHelper().generateRandomStr)()}-clone`;
    const clonedScopePath = path().join(this.scopes.e2eDir, clonedScope);
    if (this.debugMode) console.log(`cloning a scope from ${this.scopes.localPath} to ${clonedScopePath}`);

    _fsExtra().default.copySync(this.scopes.localPath, clonedScopePath);

    this.clonedScopes.push(clonedScopePath);
    return clonedScopePath;
  }

  getClonedLocalScope(clonedScopePath, deleteCurrentScope = true) {
    if (!_fsExtra().default.existsSync(clonedScopePath)) {
      throw new Error(`getClonedLocalScope was unable to find the clonedScopePath at ${clonedScopePath}`);
    }

    if (deleteCurrentScope) {
      _fsExtra().default.removeSync(this.scopes.localPath);
    } else {
      this.scopes.setLocalScope();
    }

    if (this.debugMode) console.log(`cloning a scope from ${clonedScopePath} to ${this.scopes.localPath}`);

    _fsExtra().default.copySync(clonedScopePath, this.scopes.localPath);
  }

  cloneRemoteScope() {
    const clonedScope = (0, _e2eHelper().generateRandomStr)();
    const clonedScopePath = path().join(this.scopes.e2eDir, clonedScope);
    if (this.debugMode) console.log(`cloning a scope from ${this.scopes.remotePath} to ${clonedScopePath}`);

    _fsExtra().default.copySync(this.scopes.remotePath, clonedScopePath);

    this.clonedScopes.push(clonedScopePath);
    return clonedScopePath;
  }

  getClonedRemoteScope(clonedScopePath, deleteCurrentScope = true) {
    if (deleteCurrentScope) {
      _fsExtra().default.removeSync(this.scopes.remotePath);
    } else {
      this.getNewBareScope();
    }

    if (this.debugMode) console.log(`cloning a scope from ${clonedScopePath} to ${this.scopes.remotePath}`);

    _fsExtra().default.copySync(clonedScopePath, this.scopes.remotePath);
  }

}

exports.default = ScopeHelper;