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

improvement(bit-scope-rename): improve --refactor flag to also rename aspect-ids in workspace.jsonc #6564

Merged
merged 1 commit into from
Oct 19, 2022
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
18 changes: 18 additions & 0 deletions e2e/harmony/scope-cmd.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,22 @@ describe('bit scope command', function () {
expect(showFork.config.forkedFrom.name).to.equal('comp1');
});
});
describe('bit scope rename --refactor', () => {
before(() => {
helper.scopeHelper.setNewLocalAndRemoteScopes();
helper.fixtures.populateComponents(3);
helper.bitJsonc.addKeyVal('my-scope/comp2', {});
helper.command.renameScope('my-scope', helper.scopes.remote, '--refactor');
});
it('should change the package name in the component files', () => {
const comp2Content = helper.fs.readFile('comp2/index.js');
expect(comp2Content).to.not.include('my-scope');
expect(comp2Content).to.include(helper.scopes.remote);
});
it('should also change the aspect-id in the workspace.jsonc', () => {
const workspaceJsonc = helper.bitJsonc.read();
expect(workspaceJsonc).to.have.property(`${helper.scopes.remote}/comp2`);
expect(workspaceJsonc).not.to.have.property(`my-scope/comp2`);
});
});
});
29 changes: 26 additions & 3 deletions scopes/component/renaming/renaming.main.runtime.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { BitError } from '@teambit/bit-error';
import componentIdToPackageName from '@teambit/legacy/dist/utils/bit/component-id-to-package-name';
import { ConfigAspect, ConfigMain } from '@teambit/config';
import { CLIAspect, CLIMain, MainRuntime } from '@teambit/cli';
import ComponentAspect, { Component, ComponentID, ComponentMain } from '@teambit/component';
import { DeprecationAspect, DeprecationMain } from '@teambit/deprecation';
Expand All @@ -21,7 +22,8 @@ export class RenamingMain {
private install: InstallMain,
private newComponentHelper: NewComponentHelperMain,
private deprecation: DeprecationMain,
private refactoring: RefactoringMain
private refactoring: RefactoringMain,
private config: ConfigMain
) {}

async rename(sourceIdStr: string, targetIdStr: string, options: RenameOptions): Promise<RenameDependencyNameResult> {
Expand Down Expand Up @@ -111,13 +113,31 @@ to be able to rename the scope, please untag the components first (using "bit re
};
});
const { changedComponents } = await this.refactoring.replaceMultipleStrings(allComponents, packagesToReplace);
await this.renameScopeOfAspectIdsInWorkspaceConfig(
componentsUsingOldScope.map((c) => c.id),
newScope
);
await Promise.all(changedComponents.map((comp) => this.workspace.write(comp)));
refactoredIds.push(...changedComponents.map((c) => c.id));
}

return { scopeRenamedComponentIds: componentsUsingOldScope.map((comp) => comp.id), refactoredIds };
}

private async renameScopeOfAspectIdsInWorkspaceConfig(ids: ComponentID[], newScope: string) {
const config = this.config.workspaceConfig;
if (!config) throw new Error('unable to get workspace config');
let hasChanged = false;
ids.forEach((id) => {
const changed = config.renameExtensionInRaw(
id.toStringWithoutVersion(),
id._legacy.changeScope(newScope).toStringWithoutVersion()
);
if (changed) hasChanged = true;
});
if (hasChanged) await config.write();
}

private async getConfig(comp: Component) {
const fromExisting = await this.newComponentHelper.getConfigFromExistingToNewComponent(comp);
return {
Expand All @@ -138,6 +158,7 @@ to be able to rename the scope, please untag the components first (using "bit re
GraphqlAspect,
RefactoringAspect,
InstallAspect,
ConfigAspect,
];
static runtime = MainRuntime;
static async provider([
Expand All @@ -149,6 +170,7 @@ to be able to rename the scope, please untag the components first (using "bit re
graphql,
refactoring,
install,
config,
]: [
CLIMain,
Workspace,
Expand All @@ -157,9 +179,10 @@ to be able to rename the scope, please untag the components first (using "bit re
ComponentMain,
GraphqlMain,
RefactoringMain,
InstallMain
InstallMain,
ConfigMain
]) {
const renaming = new RenamingMain(workspace, install, newComponentHelper, deprecation, refactoring);
const renaming = new RenamingMain(workspace, install, newComponentHelper, deprecation, refactoring, config);
cli.register(new RenameCmd(renaming));

const scopeCommand = cli.getCommand('scope');
Expand Down
19 changes: 15 additions & 4 deletions scopes/harmony/config/workspace-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,15 @@ export class WorkspaceConfig implements HostConfig {
this.loadExtensions();
}

renameExtensionInRaw(oldExtId: string, newExtId: string): boolean {
if (this.raw[oldExtId]) {
this.raw[newExtId] = this.raw[oldExtId];
delete this.raw[oldExtId];
return true;
}
return false;
}

/**
* Create an instance of the WorkspaceConfig by an instance of the legacy config
*
Expand Down Expand Up @@ -287,11 +296,13 @@ export class WorkspaceConfig implements HostConfig {
return WorkspaceConfig.fromObject(parsed);
}

async write({ dir }: { dir?: PathOsBasedAbsolute }): Promise<void> {
const calculatedDir = dir || this._path;
if (!calculatedDir) {
async write({ dir }: { dir?: PathOsBasedAbsolute } = {}): Promise<void> {
const getCalculatedDir = () => {
if (dir) return dir;
if (this._path) return path.dirname(this._path);
throw new ConfigDirNotDefined();
}
};
const calculatedDir = getCalculatedDir();
if (this.data) {
const files = await this.toVinyl(calculatedDir);
const dataToPersist = new DataToPersist();
Expand Down
2 changes: 1 addition & 1 deletion scopes/workspace/workspace/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -859,7 +859,7 @@ the following envs are used in this workspace: ${availableEnvs.join(', ')}`);
ignoreVersion: false,
}
);
await config.write({ dir: path.dirname(config.path) });
await config.write();
return aspectIdToAdd;
}

Expand Down
3 changes: 3 additions & 0 deletions src/e2e-helper/e2e-command-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ export default class CommandHelper {
setScope(scopeName: string, component: string) {
return this.runCmd(`bit scope set ${scopeName} ${component}`);
}
renameScope(oldScope: string, newScope: string, flags = '') {
return this.runCmd(`bit scope rename ${oldScope} ${newScope} ${flags}`);
}
setEnv(compId: string, envId: string) {
return this.runCmd(`bit envs set ${compId} ${envId}`);
}
Expand Down