@teambit/workspace
Version:
224 lines (220 loc) • 10.9 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.CapsuleListCmd = exports.CapsuleDeleteCmd = exports.CapsuleCreateCmd = exports.CapsuleCmd = void 0;
function _chalk() {
const data = _interopRequireDefault(require("chalk"));
_chalk = function () {
return data;
};
return data;
}
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } // eslint-disable-next-line max-classes-per-file
class CapsuleCreateCmd {
constructor(workspace, scope, isolator) {
this.workspace = workspace;
this.scope = scope;
this.isolator = isolator;
_defineProperty(this, "name", 'create [component-id...]');
_defineProperty(this, "description", `create capsules for components`);
_defineProperty(this, "helpUrl", 'reference/build-pipeline/capsule');
_defineProperty(this, "group", 'advanced');
_defineProperty(this, "alias", '');
_defineProperty(this, "options", [['b', 'base-dir <name>', 'set base dir of all capsules (hashed to create the base dir inside the root dir - host path by default)'], ['r', 'root-base-dir <name>', 'set root base dir of all capsules (absolute path to use as root dir)'], ['a', 'always-new', 'create new environment for capsule'], ['s', 'seeders-only', 'create capsules for the seeders only (not for the entire graph)'], ['i', 'id <name>', 'reuse capsule of certain name'], ['', 'use-hash', 'whether to use hash function (of base dir) as capsules root dir name'], ['j', 'json', 'json format'], ['d', 'install-packages', 'install packages by the package-manager'], ['p', 'package-manager <name>', 'npm, yarn or pnpm, default to npm']]);
}
async create([componentIds = []], {
baseDir,
rootBaseDir,
alwaysNew = false,
id,
installPackages = false,
seedersOnly = false,
useHash
}) {
// @todo: why it is not an array?
if (componentIds && !Array.isArray(componentIds)) componentIds = [componentIds];
let finalUseHash = useHash;
if (useHash === undefined) {
if (baseDir) {
finalUseHash = false;
} else {
finalUseHash = this.workspace ? this.workspace?.shouldUseHashForCapsules() : this.scope.shouldUseHashForCapsules();
}
}
const baseInstallOptions = {
installPackages
};
const additionalInstallOptions = this.workspace ? {} : {
copyPeerToRuntimeOnRoot: true,
useNesting: true,
copyPeerToRuntimeOnComponents: true,
installPeersFromEnvs: true
};
const installOptions = _objectSpread(_objectSpread({}, baseInstallOptions), additionalInstallOptions);
const capsuleOptions = {
baseDir,
rootBaseDir,
installOptions,
alwaysNew,
seedersOnly,
includeFromNestedHosts: true,
name: id,
useHash: finalUseHash
};
const host = this.workspace || this.scope;
const ids = await host.resolveMultipleComponentIds(componentIds);
const network = await this.isolator.isolateComponents(ids, capsuleOptions);
const capsules = network.graphCapsules;
return capsules;
}
async report([componentIds], opts) {
// @ts-ignore
const capsules = await this.create(componentIds, opts);
const capsuleOutput = capsules.map(capsule => `${_chalk().default.bold(capsule.component.id.toString())} - ${capsule.path}`).join('\n');
const title = `${capsules.length} capsule(s) were created successfully`;
return `${_chalk().default.green(title)}\n${capsuleOutput}`;
}
async json([componentIds], opts) {
// @ts-ignore
const capsules = await this.create(componentIds, opts);
return capsules.map(c => ({
id: c.component.id.toString(),
path: c.path
}));
}
}
exports.CapsuleCreateCmd = CapsuleCreateCmd;
class CapsuleListCmd {
constructor(isolator, workspace, scope) {
this.isolator = isolator;
this.workspace = workspace;
this.scope = scope;
_defineProperty(this, "name", 'list');
_defineProperty(this, "description", `list the capsules generated for this workspace`);
_defineProperty(this, "group", 'advanced');
_defineProperty(this, "alias", '');
_defineProperty(this, "options", [['j', 'json', 'json format']]);
}
async report() {
if (!this.workspace && !this.scope) {
throw new Error(`This command requires a Bit workspace or scope.
To initialize a workspace: bit init`);
}
const {
workspaceCapsulesRootDir,
scopeAspectsCapsulesRootDir,
scopeCapsulesRootDir
} = this.getCapsulesRootDirs();
const listWs = workspaceCapsulesRootDir ? await this.isolator.list(workspaceCapsulesRootDir) : undefined;
const listScope = await this.isolator.list(scopeAspectsCapsulesRootDir);
const hostPath = this.workspace ? this.workspace.path : this.scope.path;
const numOfWsCapsules = listWs ? listWs.capsules.length : listScope.capsules.length;
const hostType = this.workspace ? 'workspace' : 'scope';
const title = _chalk().default.green(`found ${_chalk().default.cyan(numOfWsCapsules.toString())} capsule(s) for ${hostType}: ${_chalk().default.cyan(hostPath)}`);
const wsLine = listWs ? _chalk().default.green(`workspace capsules root-dir: ${_chalk().default.cyan(workspaceCapsulesRootDir)}`) : undefined;
const scopeAspectLine = _chalk().default.green(`scope's aspects capsules root-dir: ${_chalk().default.cyan(scopeAspectsCapsulesRootDir)}`);
const scopeLine = _chalk().default.green(`scope's capsules root-dir: ${_chalk().default.cyan(scopeCapsulesRootDir)}`);
const suggestLine = _chalk().default.green(`use --json to get the list of all capsules`);
const lines = [title, wsLine, scopeAspectLine, scopeLine, suggestLine].filter(x => x).join('\n');
// TODO: improve output
return lines;
}
async json() {
if (!this.workspace && !this.scope) {
throw new Error(`This command requires a Bit workspace or scope.
To initialize a workspace: bit init`);
}
const rootDirs = this.getCapsulesRootDirs();
const listWs = rootDirs.workspaceCapsulesRootDir ? await this.isolator.list(rootDirs.workspaceCapsulesRootDir) : undefined;
const listScope = await this.isolator.list(rootDirs.scopeAspectsCapsulesRootDir);
const capsules = listWs ? listWs.capsules : [];
const scopeCapsules = listScope ? listScope.capsules : [];
return _objectSpread(_objectSpread({}, rootDirs), {}, {
capsules,
scopeCapsules
});
}
getCapsulesRootDirs() {
return getCapsulesRootDirs(this.isolator, this.scope, this.workspace);
}
}
exports.CapsuleListCmd = CapsuleListCmd;
class CapsuleDeleteCmd {
constructor(isolator, scope, workspace) {
this.isolator = isolator;
this.scope = scope;
this.workspace = workspace;
_defineProperty(this, "name", 'delete');
_defineProperty(this, "description", `delete capsules`);
_defineProperty(this, "extendedDescription", `with no args, only workspace's capsules are deleted`);
_defineProperty(this, "group", 'advanced');
_defineProperty(this, "alias", '');
_defineProperty(this, "options", [['', 'scope-aspects', 'delete scope-aspects capsules'], ['a', 'all', 'delete all capsules for all workspaces and scopes']]);
}
async report(args, {
all,
scopeAspects
}) {
const capsuleBaseDirToDelete = () => {
if (all) return undefined;
if (scopeAspects) {
const {
scopeAspectsCapsulesRootDir
} = getCapsulesRootDirs(this.isolator, this.scope, this.workspace);
return scopeAspectsCapsulesRootDir;
}
return undefined;
};
const capsuleBaseDir = capsuleBaseDirToDelete();
const deletedDir = await this.isolator.deleteCapsules(capsuleBaseDir);
return _chalk().default.green(`the following capsules dir has been deleted ${_chalk().default.bold(deletedDir)}`);
}
}
exports.CapsuleDeleteCmd = CapsuleDeleteCmd;
class CapsuleCmd {
constructor(isolator, workspace, scope) {
this.isolator = isolator;
this.workspace = workspace;
this.scope = scope;
_defineProperty(this, "name", 'capsule');
_defineProperty(this, "description", 'manage isolated component environments');
_defineProperty(this, "extendedDescription", `capsules are temporary isolated directories containing component code and dependencies.
automatically created during build processes to compile and test components in isolation.
ensures components work independently before publishing, similar to how they'll be consumed.`);
_defineProperty(this, "alias", '');
_defineProperty(this, "group", 'advanced');
_defineProperty(this, "commands", []);
_defineProperty(this, "options", [['j', 'json', 'json format']]);
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
async report(args) {
return new CapsuleListCmd(this.isolator, this.workspace, this.scope).report();
}
}
exports.CapsuleCmd = CapsuleCmd;
function getCapsulesRootDirs(isolator, scope, workspace) {
const workspaceCapsulesRootDir = workspace ? isolator.getCapsulesRootDir({
baseDir: workspace.getCapsulePath(),
useHash: workspace.shouldUseHashForCapsules()
}) : undefined;
const scopeAspectsCapsulesRootDir = isolator.getCapsulesRootDir({
baseDir: scope.getAspectCapsulePath(),
useHash: scope.shouldUseHashForCapsules()
});
const scopeCapsulesRootDir = workspace ? undefined : isolator.getCapsulesRootDir({
baseDir: process.cwd(),
useHash: true
});
return {
workspaceCapsulesRootDir,
scopeAspectsCapsulesRootDir,
scopeCapsulesRootDir
};
}
//# sourceMappingURL=capsule.cmd.js.map