@teambit/checkout
Version:
228 lines (225 loc) • 12.2 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.CheckoutCmd = void 0;
exports.checkoutOutput = checkoutOutput;
function _chalk() {
const data = _interopRequireDefault(require("chalk"));
_chalk = function () {
return data;
};
return data;
}
function _lodash() {
const data = require("lodash");
_lodash = function () {
return data;
};
return data;
}
function _componentModules() {
const data = require("@teambit/component.modules.merge-helper");
_componentModules = function () {
return data;
};
return data;
}
function _legacy() {
const data = require("@teambit/legacy.constants");
_legacy = function () {
return data;
};
return data;
}
function _componentId() {
const data = require("@teambit/component-id");
_componentId = function () {
return data;
};
return data;
}
function _bitError() {
const data = require("@teambit/bit-error");
_bitError = function () {
return data;
};
return data;
}
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: 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); }
class CheckoutCmd {
constructor(checkout) {
this.checkout = checkout;
_defineProperty(this, "name", 'checkout <to> [component-pattern]');
_defineProperty(this, "arguments", [{
name: 'to',
description: "permitted values: [head, latest, reset, {specific-version}, {head~x}]. 'head' - last snap/tag. 'latest' - semver latest tag. 'reset' - removes local changes"
}, {
name: 'component-pattern',
description: _legacy().COMPONENT_PATTERN_HELP
}]);
_defineProperty(this, "description", 'switch between component versions or remove local changes');
_defineProperty(this, "helpUrl", 'reference/components/merging-changes#checkout-snaps-to-the-working-directory');
_defineProperty(this, "group", 'version-control');
_defineProperty(this, "extendedDescription", `
\`bit checkout <version> [component-pattern]\` => checkout the specified ids (or all components when --all is used) to the specified version
\`bit checkout head [component-pattern]\` => checkout to the last snap/tag (use --latest if you only want semver tags), omit [component-pattern] to checkout head for all
\`bit checkout head~x [component-pattern]\` => go backward x generations from the head and checkout to that version
\`bit checkout latest [component-pattern]\` => checkout to the latest satisfying semver tag, omit [component-pattern] to checkout latest for all
\`bit checkout reset [component-pattern]\` => remove local modifications from the specified ids (or all components when --all is used). also, if a component dir is deleted from the filesystem, it'll be restored
when on a lane, "checkout head" only checks out components on this lane. to update main components, run "bit lane merge main"`);
_defineProperty(this, "alias", 'U');
_defineProperty(this, "options", [['i', 'interactive-merge', 'when a component is modified and the merge process found conflicts, display options to resolve them'], ['r', 'auto-merge-resolve <merge-strategy>', 'in case of merge conflict, resolve according to the provided strategy: [ours, theirs, manual]'], ['', 'manual', 'same as "--auto-merge-resolve manual". in case of merge conflict, write the files with the conflict markers'], ['a', 'all', 'all components'], ['e', 'workspace-only', "only relevant for 'bit checkout head' when on a lane. don't import components from the remote lane that are not already in the workspace"], ['v', 'verbose', 'showing verbose output for inspection'], ['x', 'skip-dependency-installation', 'do not auto-install dependencies of the imported components'], ['', 'force-ours', 'do not merge, preserve local files as is'], ['', 'force-theirs', 'do not merge, just overwrite with incoming files']]);
_defineProperty(this, "loader", true);
}
async report([to, componentPattern], {
interactiveMerge = false,
forceOurs,
forceTheirs,
autoMergeResolve,
manual,
all = false,
workspaceOnly = false,
verbose = false,
skipDependencyInstallation = false,
revert = false
}) {
if (forceOurs && forceTheirs) {
throw new (_bitError().BitError)('please use either --force-ours or --force-theirs, not both');
}
if (autoMergeResolve && autoMergeResolve !== 'ours' && autoMergeResolve !== 'theirs' && autoMergeResolve !== 'manual') {
throw new (_bitError().BitError)('--auto-merge-resolve must be one of the following: [ours, theirs, manual]');
}
if (manual) autoMergeResolve = 'manual';
if (workspaceOnly && to !== _legacy().HEAD) {
throw new (_bitError().BitError)('--workspace-only is only relevant when running "bit checkout head" on a lane');
}
const checkoutProps = {
promptMergeOptions: interactiveMerge,
mergeStrategy: autoMergeResolve,
all,
verbose,
isLane: false,
skipNpmInstall: skipDependencyInstallation,
workspaceOnly,
revert,
forceOurs,
forceTheirs
};
to = String(to); // it can be a number in case short-hash is used
if (to === _legacy().HEAD) checkoutProps.head = true;else if (to === _legacy().LATEST) checkoutProps.latest = true;else if (to === 'reset') checkoutProps.reset = true;else if (to === 'main') checkoutProps.main = true;else if (to.startsWith(`${_legacy().HEAD}~`)) {
const ancestor = parseInt(to.split('~')[1]);
if (Number.isNaN(ancestor)) throw new (_bitError().BitError)(`the character after "${_legacy().HEAD}~" must be a number, got ${ancestor}`);
checkoutProps.ancestor = ancestor;
} else {
if (!_componentId().ComponentID.isValidVersion(to)) throw new (_bitError().BitError)(`the specified version "${to}" is not a valid version`);
checkoutProps.version = to;
}
const checkoutResults = await this.checkout.checkoutByCLIValues(componentPattern || '', checkoutProps);
return checkoutOutput(checkoutResults, checkoutProps);
}
}
exports.CheckoutCmd = CheckoutCmd;
function checkoutOutput(checkoutResults, checkoutProps, alternativeTitle) {
const {
components,
version,
failedComponents,
removedComponents,
addedComponents,
leftUnresolvedConflicts,
workspaceConfigUpdateResult,
newFromLane,
newFromLaneAdded,
installationError,
compilationError
} = checkoutResults;
const {
head,
reset,
latest,
main,
revert,
verbose,
all
} = checkoutProps;
// components that failed for no legitimate reason. e.g. merge-conflict.
const realFailedComponents = (failedComponents || []).filter(f => !f.unchangedLegitimately);
if (realFailedComponents.length) {
throw new Error('checkout should throw in case of errors');
}
// components that weren't checked out for legitimate reasons, e.g. up-to-date.
const notCheckedOutComponents = failedComponents || [];
const getNotCheckedOutOutput = () => {
if (!notCheckedOutComponents.length) return '';
if (!verbose && all) {
return _chalk().default.green(`checkout was not needed for ${_chalk().default.bold(notCheckedOutComponents.length.toString())} components (use --verbose to get more details)\n`);
}
const title = 'checkout was not required for the following component(s)';
const body = notCheckedOutComponents.map(failedComponent => `${failedComponent.id.toString()} - ${failedComponent.unchangedMessage}`).join('\n');
return `${_chalk().default.underline(title)}\n${body}`;
};
const getWsConfigUpdateLogs = () => {
const logs = workspaceConfigUpdateResult?.logs;
if (!logs || !logs.length) return '';
const logsStr = logs.join('\n');
return `${_chalk().default.underline('verbose logs of workspace config update')}\n${logsStr}`;
};
const getConflictSummary = () => {
if (!components || !components.length || !leftUnresolvedConflicts) return '';
const title = `files with conflicts summary\n`;
const suggestion = `\n\nfix the conflicts above manually and then run "bit install".
once ready, snap/tag the components to persist the changes`;
const conflictSummary = (0, _componentModules().conflictSummaryReport)(components);
return _chalk().default.underline(title) + conflictSummary.conflictStr + _chalk().default.yellow(suggestion);
};
const getSuccessfulOutput = () => {
if (!components || !components.length) return '';
const newLine = '\n';
const switchedOrReverted = revert ? 'reverted' : 'switched';
if (components.length === 1) {
const component = components[0];
const componentName = reset ? component.id.toString() : component.id.toStringWithoutVersion();
if (reset) return `successfully reset ${_chalk().default.bold(componentName)}\n`;
const title = alternativeTitle || `successfully ${switchedOrReverted} ${_chalk().default.bold(componentName)} to version ${_chalk().default.bold(head || latest ? component.id.version : version)}`;
return _chalk().default.bold(title) + newLine + (0, _componentModules().applyVersionReport)(components, false);
}
if (reset) {
const title = 'successfully reset the following components\n\n';
const body = components.map(component => _chalk().default.bold(component.id.toString())).join('\n');
return _chalk().default.underline(title) + body;
}
const getVerOutput = () => {
if (head) return 'their head version';
if (latest) return 'their latest version';
if (main) return 'their main version';
return `version ${_chalk().default.bold(version)}`;
};
const versionOutput = getVerOutput();
const title = alternativeTitle || `successfully ${switchedOrReverted} ${components.length} components to ${versionOutput}`;
const showVersion = head || reset;
return _chalk().default.bold(title) + newLine + (0, _componentModules().applyVersionReport)(components, true, showVersion);
};
const getNewOnLaneOutput = () => {
if (!newFromLane?.length) return '';
const title = newFromLaneAdded ? `successfully added the following components from the lane` : `the following components exist on the lane but were not added to the workspace. omit --workspace-only flag to add them`;
const body = newFromLane.join('\n');
return `${_chalk().default.underline(title)}\n${body}`;
};
const getSummary = () => {
const checkedOut = components?.length || 0;
const notCheckedOutLegitimately = notCheckedOutComponents.length;
const title = _chalk().default.bold.underline('Summary');
const checkedOutStr = `\nTotal Changed: ${_chalk().default.bold(checkedOut.toString())}`;
const unchangedLegitimatelyStr = `\nTotal Unchanged: ${_chalk().default.bold(notCheckedOutLegitimately.toString())}`;
const newOnLaneNum = newFromLane?.length || 0;
const newOnLaneAddedStr = newFromLaneAdded ? ' (added)' : ' (not added)';
const newOnLaneStr = newOnLaneNum ? `\nNew on lane${newOnLaneAddedStr}: ${_chalk().default.bold(newOnLaneNum.toString())}` : '';
return title + checkedOutStr + unchangedLegitimatelyStr + newOnLaneStr;
};
return (0, _lodash().compact)([getWsConfigUpdateLogs(), getNotCheckedOutOutput(), getSuccessfulOutput(), (0, _componentModules().getRemovedOutput)(removedComponents), (0, _componentModules().getAddedOutput)(addedComponents), getNewOnLaneOutput(), (0, _componentModules().getWorkspaceConfigUpdateOutput)(workspaceConfigUpdateResult), getConflictSummary(), getSummary(), (0, _componentModules().installationErrorOutput)(installationError), (0, _componentModules().compilationErrorOutput)(compilationError)]).join('\n\n');
}
//# sourceMappingURL=checkout-cmd.js.map