bit-bin
Version:
<a href="https://opensource.org/licenses/Apache-2.0"><img alt="apache" src="https://img.shields.io/badge/License-Apache%202.0-blue.svg"></a> <a href="https://github.com/teambit/bit/blob/master/CONTRIBUTING.md"><img alt="prs" src="https://img.shields.io/b
330 lines (243 loc) • 9.23 kB
JavaScript
"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 _utils() {
const data = require("../utils");
_utils = function () {
return data;
};
return data;
}
function _featureToggle() {
const data = require("../api/consumer/lib/feature-toggle");
_featureToggle = 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", []);
(0, _defineProperty2().default)(this, "packageManager", 'npm');
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);
}
usePackageManager(packageManager) {
this.packageManager = packageManager;
}
reInitLocalScope() {
this.cleanLocalScope();
this.initLocalScope();
}
reInitLocalScopeHarmony() {
this.cleanLocalScope();
this.command.runCmd('bit init', undefined, undefined, _featureToggle().HARMONY_FEATURE);
}
initLocalScope() {
return this.initWorkspace();
}
initWorkspace(workspacePath) {
// return this.command.runCmd('bit init -N', workspacePath);
return this.command.runCmd(`bit init -p ${this.packageManager}`, workspacePath);
}
initWorkspaceAndRemoteScope(workspacePath) {
this.initWorkspace(workspacePath);
this.reInitRemoteScope();
this.addRemoteScope();
}
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
});
})();
}
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();
}
setNewLocalAndRemoteScopesHarmony() {
this.reInitLocalScopeHarmony();
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, _utils().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(dereferenceSymlinks = true) {
const clonedScope = `${(0, _utils().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, {
dereference: dereferenceSymlinks
});
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() {
return this.cloneScope(this.scopes.remotePath);
}
cloneScope(scopePath) {
const clonedScope = (0, _utils().generateRandomStr)();
const clonedScopePath = path().join(this.scopes.e2eDir, clonedScope);
if (this.debugMode) console.log(`cloning a scope from ${scopePath} to ${clonedScopePath}`);
_fsExtra().default.copySync(scopePath, clonedScopePath);
this.clonedScopes.push(clonedScopePath);
return clonedScopePath;
}
getClonedScope(clonedScopePath, scopePath) {
_fsExtra().default.removeSync(scopePath);
if (this.debugMode) console.log(`cloning a scope from ${clonedScopePath} to ${scopePath}`);
_fsExtra().default.copySync(clonedScopePath, scopePath);
}
getClonedRemoteScope(clonedScopePath) {
return this.getClonedScope(clonedScopePath, this.scopes.remotePath);
}
}
exports.default = ScopeHelper;