Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve rename lane command #7853

Merged
merged 5 commits into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions e2e/harmony/lanes/lanes.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ describe('bit lane command', function () {
const { scopePath, scopeName } = helper.scopeHelper.getNewBareScope();
helper.scopeHelper.addRemoteScope(scopePath);
helper.command.createLane('dev');
helper.command.changeLaneScope('dev', scopeName);
helper.command.changeLaneScope(scopeName);
helper.fs.outputFile('comp1/comp1.spec.js');
helper.command.snapAllComponentsWithoutBuild();
helper.command.export();
Expand Down Expand Up @@ -632,7 +632,7 @@ describe('bit lane command', function () {
before(() => {
helper.scopeHelper.reInitLocalScope();
helper.command.createLane();
output = helper.command.changeLaneScope('dev', 'my-remote');
output = helper.command.changeLaneScope('my-remote');
});
it('should output the changes', () => {
expect(removeChalkCharacters(output)).to.have.string(
Expand Down Expand Up @@ -1232,7 +1232,7 @@ describe('bit lane command', function () {
helper.fixtures.populateComponents(1);
helper.command.snapAllComponentsWithoutBuild();
helper.command.export();
helper.command.renameLane('dev', 'new-lane');
helper.command.renameLane('new-lane');
});
it('should rename the lane locally', () => {
const lanes = helper.command.listLanes();
Expand Down Expand Up @@ -1517,7 +1517,7 @@ describe('bit lane command', function () {
helper.command.export();
});
it('should block the rename', () => {
expect(() => helper.command.changeLaneScope('dev', 'new-scope')).to.throw(
expect(() => helper.command.changeLaneScope('new-scope')).to.throw(
'changing lane scope-name is allowed for new lanes only'
);
});
Expand All @@ -1530,7 +1530,7 @@ describe('bit lane command', function () {
});
it('should throw InvalidScopeName error', () => {
const err = new InvalidScopeName('invalid.scope.name');
const cmd = () => helper.command.changeLaneScope('dev', 'invalid.scope.name');
const cmd = () => helper.command.changeLaneScope('invalid.scope.name');
helper.general.expectToThrow(cmd, err);
});
});
Expand Down
2 changes: 1 addition & 1 deletion e2e/harmony/lanes/rename-lane.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe('rename lanes', function () {
helper.command.snapAllComponentsWithoutBuild();
helper.command.export();

helper.command.renameLane('d', 'new-dev');
helper.command.renameLane('new-dev');
});
it('should not throw on any command', () => {
expect(() => helper.command.status()).to.not.throw();
Expand Down
24 changes: 16 additions & 8 deletions scopes/lanes/lanes/lane.cmd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,35 +254,43 @@ it is useful e.g. when having multiple lanes with the same name, but with differ
}

export class LaneChangeScopeCmd implements Command {
name = 'change-scope <lane-name> <remote-scope-name>';
name = 'change-scope <remote-scope-name>';
description = `changes the remote scope of a lane`;
extendedDescription = 'NOTE: available only before the lane is exported to the remote';
alias = '';
options = [] as CommandOptions;
options = [
[
'l',
'lane-name <lane-name>',
'the name of the lane to change its remote scope. if not specified, the current lane is used',
],
] as CommandOptions;
loader = true;
migration = true;

constructor(private lanes: LanesMain) {}

async report([localName, remoteScope]: [string, string]): Promise<string> {
const { remoteScopeBefore } = await this.lanes.changeScope(localName, remoteScope);
async report([remoteScope]: [string], { laneName }: { laneName?: string }): Promise<string> {
const { remoteScopeBefore, localName } = await this.lanes.changeScope(remoteScope, laneName);
return `the remote-scope of ${chalk.bold(localName)} has been changed from ${chalk.bold(
remoteScopeBefore
)} to ${chalk.bold(remoteScope)}`;
}
}

export class LaneRenameCmd implements Command {
name = 'rename <current-name> <new-name>';
name = 'rename <new-name>';
description = `EXPERIMENTAL. change the lane-name locally and on the remote (if exported)`;
alias = '';
options = [] as CommandOptions;
options = [
['l', 'lane-name <lane-name>', 'the name of the lane to rename. if not specified, the current lane is used'],
] as CommandOptions;
loader = true;
migration = true;
constructor(private lanes: LanesMain) {}

async report([currentName, newName]: [string, string]): Promise<string> {
const { exported, exportErr } = await this.lanes.rename(currentName, newName);
async report([newName]: [string], { laneName }: { laneName?: string }): Promise<string> {
const { exported, exportErr, currentName } = await this.lanes.rename(newName, laneName);
const exportedStr = exported
? `and have been exported successfully to the remote`
: `however failed exporting the renamed lane to the remote, due to an error: ${exportErr?.message || 'unknown'}`;
Expand Down
27 changes: 19 additions & 8 deletions scopes/lanes/lanes/lanes.main.runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,14 +356,21 @@ if you wish to keep ${scope} scope, please re-run the command with "--fork-lane-
return { laneId };
}

async changeScope(laneName: string, remoteScope: string): Promise<{ remoteScopeBefore: string }> {
async changeScope(remoteScope: string, laneName?: string): Promise<{ remoteScopeBefore: string; localName: string }> {
if (!this.workspace) {
throw new BitError(`unable to change-scope of a lane outside of Bit workspace`);
}
const laneNameWithoutScope = laneName.includes(LANE_REMOTE_DELIMITER)
? laneName.split(LANE_REMOTE_DELIMITER)[1]
: laneName;
const laneId = await this.scope.legacyScope.lanes.parseLaneIdFromString(laneName);
let laneId: LaneId;
let laneNameWithoutScope: string;
if (laneName) {
laneNameWithoutScope = laneName.includes(LANE_REMOTE_DELIMITER)
? laneName.split(LANE_REMOTE_DELIMITER)[1]
: laneName;
laneId = await this.scope.legacyScope.lanes.parseLaneIdFromString(laneName);
} else {
laneId = this.workspace.getCurrentLaneId();
laneNameWithoutScope = laneId.name;
}
const lane = await this.loadLane(laneId);
if (!lane) {
throw new BitError(`unable to find a local lane "${laneName}"`);
Expand All @@ -388,17 +395,21 @@ please create a new lane instead, which will include all components of this lane
this.workspace.consumer.bitMap.setCurrentLane(newLaneId, false);
await this.workspace.consumer.onDestroy();

return { remoteScopeBefore };
return { remoteScopeBefore, localName: laneNameWithoutScope };
}

/**
* change a lane-name and if possible, export the lane to the remote
*/
async rename(currentName: string, newName: string): Promise<{ exported: boolean; exportErr?: Error }> {
async rename(
newName: string,
laneName?: string
): Promise<{ exported: boolean; exportErr?: Error; currentName: string }> {
if (!this.workspace) {
throw new BitError(`unable to rename a lane outside of Bit workspace`);
}
throwForInvalidLaneName(newName);
const currentName = laneName || this.workspace.getCurrentLaneId().name;
const existingAliasWithNewName = this.scope.legacyScope.lanes.getRemoteTrackedDataByLocalLane(newName);
if (existingAliasWithNewName) {
const remoteIdStr = `${existingAliasWithNewName.remoteLane}/${existingAliasWithNewName.remoteScope}`;
Expand Down Expand Up @@ -452,7 +463,7 @@ please create a new lane instead, which will include all components of this lane

await this.workspace.consumer.onDestroy();

return { exported, exportErr };
return { exported, exportErr, currentName };
}

async exportLane(lane: Lane) {
Expand Down
8 changes: 4 additions & 4 deletions src/e2e-helper/e2e-command-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,8 +312,8 @@ export default class CommandHelper {
createLane(laneName = 'dev', options = '') {
return this.runCmd(`bit lane create ${laneName} ${options}`);
}
changeLaneScope(laneName: string, newScope: string) {
return this.runCmd(`bit lane change-scope ${laneName} ${newScope}`);
changeLaneScope(newScope: string) {
return this.runCmd(`bit lane change-scope ${newScope}`);
}
clearCache() {
return this.runCmd('bit clear-cache');
Expand Down Expand Up @@ -443,8 +443,8 @@ export default class CommandHelper {
fetchAllComponents() {
return this.runCmd(`bit fetch --components`);
}
renameLane(oldName: string, newName: string) {
return this.runCmd(`bit lane rename ${oldName} ${newName}`);
renameLane(newName: string) {
return this.runCmd(`bit lane rename ${newName}`);
}
importManyComponents(ids: string[], flag = '') {
const idsWithRemote = ids.map((id) => `${this.scopes.remote}/${id}`);
Expand Down