@teambit/renaming 1.0.108 → 1.0.110
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/artifacts/preview/teambit_component_renaming-preview.js +1 -0
- package/dist/{preview-1703647408454.js → preview-1703733956734.js} +2 -2
- package/package.json +26 -22
- package/index.ts +0 -5
- package/rename.cmd.ts +0 -54
- package/renaming.aspect.ts +0 -5
- package/renaming.fragment.ts +0 -26
- package/renaming.graphql.ts +0 -28
- package/renaming.main.runtime.ts +0 -439
- package/scope-rename-owner.cmd.ts +0 -37
- package/scope-rename.cmd.ts +0 -40
|
@@ -0,0 +1 @@
|
|
|
1
|
+
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports["teambit.component/renaming-preview"]=t():e["teambit.component/renaming-preview"]=t()}(self,(()=>(()=>{"use strict";var e={7626:(e,t,o)=>{var n={id:"teambit.component/renaming@1.0.110",homepage:"https://bit.cloud/teambit/component/renaming",exported:!0};function r(){const e=i(o(87363));return r=function(){return e},e}function i(e){return e&&e.__esModule?e:{default:e}}Object.defineProperty(t,"__esModule",{value:!0}),t.Logo=void 0,r.__bit_component=n,i.__bit_component=n;const p=()=>r().default.createElement("div",{style:{height:"100%",display:"flex",justifyContent:"center"}},r().default.createElement("img",{style:{width:70},src:"https://static.bit.dev/extensions-icons/rename.svg"}));p.__bit_component=n,t.Logo=p},87363:e=>{e.exports=React}},t={};function o(n){var r=t[n];if(void 0!==r)return r.exports;var i=t[n]={exports:{}};return e[n](i,i.exports,o),i.exports}o.d=(e,t)=>{for(var n in t)o.o(t,n)&&!o.o(e,n)&&Object.defineProperty(e,n,{enumerable:!0,get:t[n]})},o.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),o.r=e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})};var n={};return(()=>{o.r(n),o.d(n,{compositions:()=>u,compositions_metadata:()=>f,overview:()=>m});var e={};o.r(e),o.d(e,{default:()=>d});var t=o(7626);o(87363);const r=MdxJsReact,i=TeambitMdxUiMdxScopeContext;var p=["components"];function a(){return a=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var o=arguments[t];for(var n in o)Object.prototype.hasOwnProperty.call(o,n)&&(e[n]=o[n])}return e},a.apply(this,arguments)}var c={},s="wrapper";function d(e){var t=e.components,o=function(e,t){if(null==e)return{};var o,n,r=function(e,t){if(null==e)return{};var o,n,r={},i=Object.keys(e);for(n=0;n<i.length;n++)o=i[n],t.indexOf(o)>=0||(r[o]=e[o]);return r}(e,t);if(Object.getOwnPropertySymbols){var i=Object.getOwnPropertySymbols(e);for(n=0;n<i.length;n++)o=i[n],t.indexOf(o)>=0||Object.prototype.propertyIsEnumerable.call(e,o)&&(r[o]=e[o])}return r}(e,p);return(0,r.mdx)(s,a({},c,o,{components:t,mdxType:"MDXLayout"}),(0,r.mdx)(i.MDXScopeProvider,{components:{},mdxType:"MDXScopeProvider"}))}d.isMDXComponent=!0;const u=[t],m=[e],f={compositions:[{displayName:"Logo",identifier:"Logo"}]}})(),n})()));
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import * as compositions_0 from '/home/circleci/Library/Caches/Bit/capsules/8891be5ad3d35bfc38b9cd90c0e05b598a5a55af/teambit.component_renaming@1.0.
|
|
2
|
-
import * as overview_0 from '/home/circleci/Library/Caches/Bit/capsules/8891be5ad3d35bfc38b9cd90c0e05b598a5a55af/teambit.component_renaming@1.0.
|
|
1
|
+
import * as compositions_0 from '/home/circleci/Library/Caches/Bit/capsules/8891be5ad3d35bfc38b9cd90c0e05b598a5a55af/teambit.component_renaming@1.0.110/dist/renaming.composition.js';
|
|
2
|
+
import * as overview_0 from '/home/circleci/Library/Caches/Bit/capsules/8891be5ad3d35bfc38b9cd90c0e05b598a5a55af/teambit.component_renaming@1.0.110/dist/renaming.docs.mdx';
|
|
3
3
|
|
|
4
4
|
export const compositions = [compositions_0];
|
|
5
5
|
export const overview = [overview_0];
|
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@teambit/renaming",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.110",
|
|
4
4
|
"homepage": "https://bit.cloud/teambit/component/renaming",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"componentId": {
|
|
7
7
|
"scope": "teambit.component",
|
|
8
8
|
"name": "renaming",
|
|
9
|
-
"version": "1.0.
|
|
9
|
+
"version": "1.0.110"
|
|
10
10
|
},
|
|
11
11
|
"dependencies": {
|
|
12
12
|
"chalk": "2.4.2",
|
|
@@ -15,28 +15,28 @@
|
|
|
15
15
|
"@teambit/harmony": "0.4.6",
|
|
16
16
|
"@teambit/legacy-bit-id": "1.1.0",
|
|
17
17
|
"@teambit/bit-error": "0.0.404",
|
|
18
|
-
"@teambit/cli": "0.0.
|
|
19
|
-
"@teambit/component": "1.0.
|
|
20
|
-
"@teambit/graphql": "1.0.
|
|
21
|
-
"@teambit/compiler": "1.0.
|
|
22
|
-
"@teambit/component-writer": "1.0.
|
|
23
|
-
"@teambit/config": "0.0.
|
|
24
|
-
"@teambit/deprecation": "1.0.
|
|
25
|
-
"@teambit/envs": "1.0.
|
|
26
|
-
"@teambit/install": "1.0.
|
|
27
|
-
"@teambit/logger": "0.0.
|
|
28
|
-
"@teambit/new-component-helper": "1.0.
|
|
29
|
-
"@teambit/refactoring": "1.0.
|
|
30
|
-
"@teambit/typescript": "1.0.
|
|
31
|
-
"@teambit/workspace.modules.node-modules-linker": "0.0.
|
|
32
|
-
"@teambit/workspace": "1.0.
|
|
18
|
+
"@teambit/cli": "0.0.842",
|
|
19
|
+
"@teambit/component": "1.0.110",
|
|
20
|
+
"@teambit/graphql": "1.0.110",
|
|
21
|
+
"@teambit/compiler": "1.0.110",
|
|
22
|
+
"@teambit/component-writer": "1.0.110",
|
|
23
|
+
"@teambit/config": "0.0.861",
|
|
24
|
+
"@teambit/deprecation": "1.0.110",
|
|
25
|
+
"@teambit/envs": "1.0.110",
|
|
26
|
+
"@teambit/install": "1.0.110",
|
|
27
|
+
"@teambit/logger": "0.0.935",
|
|
28
|
+
"@teambit/new-component-helper": "1.0.110",
|
|
29
|
+
"@teambit/refactoring": "1.0.110",
|
|
30
|
+
"@teambit/typescript": "1.0.110",
|
|
31
|
+
"@teambit/workspace.modules.node-modules-linker": "0.0.159",
|
|
32
|
+
"@teambit/workspace": "1.0.110"
|
|
33
33
|
},
|
|
34
34
|
"devDependencies": {
|
|
35
35
|
"@types/fs-extra": "9.0.7",
|
|
36
36
|
"@types/mocha": "9.1.0",
|
|
37
37
|
"@types/jest": "^29.2.2",
|
|
38
38
|
"@types/testing-library__jest-dom": "^5.9.5",
|
|
39
|
-
"@teambit/harmony.envs.core-aspect-env": "0.0.
|
|
39
|
+
"@teambit/harmony.envs.core-aspect-env": "0.0.15"
|
|
40
40
|
},
|
|
41
41
|
"peerDependencies": {
|
|
42
42
|
"react": "^17.0.0 || ^18.0.0",
|
|
@@ -47,11 +47,15 @@
|
|
|
47
47
|
"optionalDependencies": {},
|
|
48
48
|
"peerDependenciesMeta": {},
|
|
49
49
|
"exports": {
|
|
50
|
-
"
|
|
51
|
-
"
|
|
52
|
-
|
|
50
|
+
".": {
|
|
51
|
+
"node": {
|
|
52
|
+
"require": "./dist/index.js",
|
|
53
|
+
"import": "./dist/esm.mjs"
|
|
54
|
+
},
|
|
55
|
+
"default": "./dist/index.js"
|
|
53
56
|
},
|
|
54
|
-
"
|
|
57
|
+
"./dist/*": "./dist/*",
|
|
58
|
+
"./artifacts/*": "./artifacts/*"
|
|
55
59
|
},
|
|
56
60
|
"private": false,
|
|
57
61
|
"engines": {
|
package/index.ts
DELETED
package/rename.cmd.ts
DELETED
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
import chalk from 'chalk';
|
|
2
|
-
import { Command, CommandOptions } from '@teambit/cli';
|
|
3
|
-
import { RenamingMain } from '.';
|
|
4
|
-
|
|
5
|
-
export type RenameOptions = {
|
|
6
|
-
scope?: string;
|
|
7
|
-
path?: string;
|
|
8
|
-
refactor?: boolean;
|
|
9
|
-
preserve?: boolean;
|
|
10
|
-
ast?: boolean;
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
export class RenameCmd implements Command {
|
|
14
|
-
name = 'rename <current-name> <new-name>';
|
|
15
|
-
description =
|
|
16
|
-
'rename component. if tagged/exported, create a new component and deprecate the original component. otherwise just renames current component';
|
|
17
|
-
helpUrl = 'reference/components/renaming-components';
|
|
18
|
-
arguments = [
|
|
19
|
-
{
|
|
20
|
-
name: 'current-name',
|
|
21
|
-
description: 'the current component name (without its scope name)',
|
|
22
|
-
},
|
|
23
|
-
{
|
|
24
|
-
name: 'new-name',
|
|
25
|
-
description: "the new component name (without its scope name. use --scope to define the new component's scope)",
|
|
26
|
-
},
|
|
27
|
-
];
|
|
28
|
-
group = 'collaborate';
|
|
29
|
-
skipWorkspace = true;
|
|
30
|
-
alias = '';
|
|
31
|
-
options = [
|
|
32
|
-
['s', 'scope <scope-name>', 'define the scope for the newly created component'],
|
|
33
|
-
[
|
|
34
|
-
'p',
|
|
35
|
-
'path <relative-path>',
|
|
36
|
-
'relative path in the workspace to place new component in. by default, the directory of the new component is from your workspace\'s "defaultScope" value',
|
|
37
|
-
],
|
|
38
|
-
['r', 'refactor', 'update the import/require statements in all dependent components (in the same workspace)'],
|
|
39
|
-
['', 'preserve', 'avoid renaming files and variables/classes according to the new component name'],
|
|
40
|
-
['', 'ast', 'EXPERIMENTAL. use ast to transform files instead of regex'],
|
|
41
|
-
] as CommandOptions;
|
|
42
|
-
loader = true;
|
|
43
|
-
migration = true;
|
|
44
|
-
remoteOp = true;
|
|
45
|
-
|
|
46
|
-
constructor(private renaming: RenamingMain) {}
|
|
47
|
-
|
|
48
|
-
async report([sourceId, targetId]: [string, string], options: RenameOptions): Promise<string> {
|
|
49
|
-
const results = await this.renaming.rename(sourceId, targetId, options);
|
|
50
|
-
return chalk.green(
|
|
51
|
-
`successfully renamed ${chalk.bold(results.sourceId.toString())} to ${chalk.bold(results.targetId.toString())}`
|
|
52
|
-
);
|
|
53
|
-
}
|
|
54
|
-
}
|
package/renaming.aspect.ts
DELETED
package/renaming.fragment.ts
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import { Component, ShowFragment } from '@teambit/component';
|
|
2
|
-
import { RenamingMain } from './renaming.main.runtime';
|
|
3
|
-
|
|
4
|
-
export class RenamingFragment implements ShowFragment {
|
|
5
|
-
constructor(private renaming: RenamingMain) {}
|
|
6
|
-
|
|
7
|
-
title = 'renaming';
|
|
8
|
-
|
|
9
|
-
async renderRow(component: Component) {
|
|
10
|
-
const renamingInfo = this.renaming.getRenamingInfo(component);
|
|
11
|
-
const content = renamingInfo ? `origin: ${renamingInfo.renamedFrom.toString()}` : '';
|
|
12
|
-
return {
|
|
13
|
-
title: this.title,
|
|
14
|
-
content,
|
|
15
|
-
};
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
async json(component: Component) {
|
|
19
|
-
return {
|
|
20
|
-
title: this.title,
|
|
21
|
-
json: this.renaming.getRenamingInfo(component),
|
|
22
|
-
};
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
weight = 3;
|
|
26
|
-
}
|
package/renaming.graphql.ts
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import { Component } from '@teambit/component';
|
|
2
|
-
import { Schema } from '@teambit/graphql';
|
|
3
|
-
import gql from 'graphql-tag';
|
|
4
|
-
import { RenamingMain } from './renaming.main.runtime';
|
|
5
|
-
|
|
6
|
-
export function renamingSchema(renaming: RenamingMain): Schema {
|
|
7
|
-
return {
|
|
8
|
-
typeDefs: gql`
|
|
9
|
-
extend type Component {
|
|
10
|
-
renaming: RenamingInfo
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
type RenamingInfo {
|
|
14
|
-
renamedFrom: String
|
|
15
|
-
}
|
|
16
|
-
`,
|
|
17
|
-
resolvers: {
|
|
18
|
-
Component: {
|
|
19
|
-
renaming: (component: Component) => {
|
|
20
|
-
const renamingInfo = renaming.getRenamingInfo(component);
|
|
21
|
-
return {
|
|
22
|
-
renamedFrom: renamingInfo?.renamedFrom.toString(),
|
|
23
|
-
};
|
|
24
|
-
},
|
|
25
|
-
},
|
|
26
|
-
},
|
|
27
|
-
};
|
|
28
|
-
}
|
package/renaming.main.runtime.ts
DELETED
|
@@ -1,439 +0,0 @@
|
|
|
1
|
-
import componentIdToPackageName from '@teambit/legacy/dist/utils/bit/component-id-to-package-name';
|
|
2
|
-
import fs from 'fs-extra';
|
|
3
|
-
import path from 'path';
|
|
4
|
-
import { ConfigAspect, ConfigMain } from '@teambit/config';
|
|
5
|
-
import { Logger, LoggerAspect, LoggerMain } from '@teambit/logger';
|
|
6
|
-
import { linkToNodeModulesByComponents } from '@teambit/workspace.modules.node-modules-linker';
|
|
7
|
-
import { CLIAspect, CLIMain, MainRuntime } from '@teambit/cli';
|
|
8
|
-
import ComponentAspect, { Component, ComponentID, ComponentMain } from '@teambit/component';
|
|
9
|
-
import { DeprecationAspect, DeprecationMain } from '@teambit/deprecation';
|
|
10
|
-
import GraphqlAspect, { GraphqlMain } from '@teambit/graphql';
|
|
11
|
-
import { CompilerAspect, CompilerMain } from '@teambit/compiler';
|
|
12
|
-
import EnvsAspect, { EnvsMain } from '@teambit/envs';
|
|
13
|
-
import NewComponentHelperAspect, { NewComponentHelperMain } from '@teambit/new-component-helper';
|
|
14
|
-
import RefactoringAspect, { MultipleStringsReplacement, RefactoringMain } from '@teambit/refactoring';
|
|
15
|
-
import ComponentWriterAspect, { ComponentWriterMain } from '@teambit/component-writer';
|
|
16
|
-
import { getBindingPrefixByDefaultScope } from '@teambit/legacy/dist/consumer/config/component-config';
|
|
17
|
-
import WorkspaceAspect, { Workspace } from '@teambit/workspace';
|
|
18
|
-
import { importTransformer, exportTransformer } from '@teambit/typescript';
|
|
19
|
-
import { InstallMain, InstallAspect } from '@teambit/install';
|
|
20
|
-
import { isValidIdChunk, InvalidName } from '@teambit/legacy-bit-id';
|
|
21
|
-
import { RenameCmd, RenameOptions } from './rename.cmd';
|
|
22
|
-
import { RenamingAspect } from './renaming.aspect';
|
|
23
|
-
import { RenamingFragment } from './renaming.fragment';
|
|
24
|
-
import { renamingSchema } from './renaming.graphql';
|
|
25
|
-
import { ScopeRenameCmd } from './scope-rename.cmd';
|
|
26
|
-
import { OldScopeNotFound } from './exceptions/old-scope-not-found';
|
|
27
|
-
import { OldScopeExported } from './exceptions/old-scope-exported';
|
|
28
|
-
import { OldScopeTagged } from './exceptions/old-scope-tagged';
|
|
29
|
-
import { ScopeRenameOwnerCmd } from './scope-rename-owner.cmd';
|
|
30
|
-
|
|
31
|
-
export class RenamingMain {
|
|
32
|
-
constructor(
|
|
33
|
-
private workspace: Workspace,
|
|
34
|
-
private install: InstallMain,
|
|
35
|
-
private newComponentHelper: NewComponentHelperMain,
|
|
36
|
-
private deprecation: DeprecationMain,
|
|
37
|
-
private refactoring: RefactoringMain,
|
|
38
|
-
private config: ConfigMain,
|
|
39
|
-
private componentWriter: ComponentWriterMain,
|
|
40
|
-
private compiler: CompilerMain,
|
|
41
|
-
private logger: Logger,
|
|
42
|
-
private envs: EnvsMain
|
|
43
|
-
) {}
|
|
44
|
-
|
|
45
|
-
async rename(sourceIdStr: string, targetName: string, options: RenameOptions): Promise<RenameDependencyNameResult> {
|
|
46
|
-
if (!isValidIdChunk(targetName)) {
|
|
47
|
-
if (targetName.includes('.'))
|
|
48
|
-
throw new Error(`error: new-name argument "${targetName}" includes a dot.
|
|
49
|
-
make sure this argument is the name only, without the scope-name. to change the scope-name, use --scope flag`);
|
|
50
|
-
throw new InvalidName(targetName);
|
|
51
|
-
}
|
|
52
|
-
const sourceId = await this.workspace.resolveComponentId(sourceIdStr);
|
|
53
|
-
const isTagged = sourceId.hasVersion();
|
|
54
|
-
const sourceComp = await this.workspace.get(sourceId);
|
|
55
|
-
const sourcePackageName = this.workspace.componentPackageName(sourceComp);
|
|
56
|
-
const targetId = this.newComponentHelper.getNewComponentId(targetName, undefined, options?.scope || sourceId.scope);
|
|
57
|
-
if (!options.preserve) {
|
|
58
|
-
await this.refactoring.refactorVariableAndClasses(sourceComp, sourceId, targetId);
|
|
59
|
-
this.refactoring.refactorFilenames(sourceComp, sourceId, targetId);
|
|
60
|
-
}
|
|
61
|
-
if (isTagged) {
|
|
62
|
-
const config = await this.getConfig(sourceComp);
|
|
63
|
-
await this.newComponentHelper.writeAndAddNewComp(sourceComp, targetId, options, config);
|
|
64
|
-
await this.deprecation.deprecate(sourceId, targetId);
|
|
65
|
-
} else {
|
|
66
|
-
this.workspace.bitMap.renameNewComponent(sourceId, targetId);
|
|
67
|
-
await this.workspace.bitMap.write(`rename (${sourceIdStr} to ${targetName})`);
|
|
68
|
-
await this.deleteLinkFromNodeModules(sourcePackageName);
|
|
69
|
-
}
|
|
70
|
-
await this.renameAspectIdInWorkspaceConfig(sourceId, targetId);
|
|
71
|
-
await this.workspace._reloadConsumer(); // in order to reload .bitmap file and clear all caches.
|
|
72
|
-
const targetComp = await this.workspace.get(targetId);
|
|
73
|
-
if (options.refactor) {
|
|
74
|
-
const allComponents = await this.workspace.list();
|
|
75
|
-
const targetPackageName = this.workspace.componentPackageName(targetComp);
|
|
76
|
-
const { changedComponents } = await this.refactoring.refactorDependencyName(
|
|
77
|
-
allComponents,
|
|
78
|
-
sourcePackageName,
|
|
79
|
-
targetPackageName
|
|
80
|
-
);
|
|
81
|
-
await Promise.all(changedComponents.map((comp) => this.workspace.write(comp)));
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
// only write if it is not tagged, since we writeAndAddNewComp for tagged components
|
|
85
|
-
if (!isTagged && !options.preserve) {
|
|
86
|
-
await this.refactoring.refactorVariableAndClasses(targetComp, sourceId, targetId, options);
|
|
87
|
-
this.refactoring.refactorFilenames(targetComp, sourceId, targetId);
|
|
88
|
-
await this.componentWriter.writeMany({
|
|
89
|
-
components: [targetComp.state._consumer],
|
|
90
|
-
skipDependencyInstallation: true,
|
|
91
|
-
writeToPath: this.newComponentHelper.getNewComponentPath(targetId),
|
|
92
|
-
reasonForBitmapChange: 'rename',
|
|
93
|
-
});
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
this.workspace.bitMap.renameAspectInConfig(sourceId, targetId);
|
|
97
|
-
await this.workspace.bitMap.write(`rename (${sourceIdStr} to ${targetName})`);
|
|
98
|
-
|
|
99
|
-
await linkToNodeModulesByComponents([targetComp], this.workspace); // link the new-name to node-modules
|
|
100
|
-
await this.compileGracefully([targetComp.id]);
|
|
101
|
-
|
|
102
|
-
return {
|
|
103
|
-
sourceId,
|
|
104
|
-
targetId,
|
|
105
|
-
};
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
getRenamingInfo(component: Component): RenamingInfo | null {
|
|
109
|
-
const renameConfig = component.state.aspects.get(RenamingAspect.id)?.config as RenamingInfo | undefined;
|
|
110
|
-
if (!renameConfig) return null;
|
|
111
|
-
return {
|
|
112
|
-
renamedFrom: ComponentID.fromObject(renameConfig.renamedFrom),
|
|
113
|
-
};
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
/**
|
|
117
|
-
* change the default-scope for new components. optionally (if refactor is true), change the source code to match the
|
|
118
|
-
* new scope-name.
|
|
119
|
-
* keep in mind that this is working for new components only, for tagged/exported it's impossible. See the errors
|
|
120
|
-
* thrown in such cases in this method.
|
|
121
|
-
*/
|
|
122
|
-
async renameScope(oldScope: string, newScope: string, options: { refactor?: boolean }): Promise<RenameScopeResult> {
|
|
123
|
-
const allComponents = await this.workspace.list();
|
|
124
|
-
const componentsUsingOldScope = allComponents.filter((comp) => comp.id.scope === oldScope);
|
|
125
|
-
if (!componentsUsingOldScope.length && this.workspace.defaultScope !== oldScope) {
|
|
126
|
-
throw new OldScopeNotFound(oldScope);
|
|
127
|
-
}
|
|
128
|
-
const envs = componentsUsingOldScope.filter((c) => this.envs.isEnv(c));
|
|
129
|
-
const compsUsingEnv: { [envIdStr: string]: ComponentID[] } = {};
|
|
130
|
-
await Promise.all(
|
|
131
|
-
envs.map(async (env) => {
|
|
132
|
-
const components = await this.workspace.getComponentsUsingEnv(env.id.toString(), true);
|
|
133
|
-
if (!components.length) return;
|
|
134
|
-
const componentIds = components.map((comp) => comp.id);
|
|
135
|
-
compsUsingEnv[env.id.toString()] = componentIds;
|
|
136
|
-
})
|
|
137
|
-
);
|
|
138
|
-
|
|
139
|
-
// verify they're all new.
|
|
140
|
-
const exported = componentsUsingOldScope.filter((comp) => this.workspace.isExported(comp.id));
|
|
141
|
-
if (exported.length) {
|
|
142
|
-
const idsStr = exported.map((comp) => comp.id.toString());
|
|
143
|
-
throw new OldScopeExported(idsStr);
|
|
144
|
-
}
|
|
145
|
-
const tagged = componentsUsingOldScope.filter((comp) => comp.id.hasVersion());
|
|
146
|
-
if (tagged.length) {
|
|
147
|
-
const idsStr = tagged.map((comp) => comp.id.toString());
|
|
148
|
-
throw new OldScopeTagged(idsStr);
|
|
149
|
-
}
|
|
150
|
-
if (this.workspace.defaultScope === oldScope) {
|
|
151
|
-
await this.workspace.setDefaultScope(newScope);
|
|
152
|
-
}
|
|
153
|
-
componentsUsingOldScope.forEach((comp) => this.workspace.bitMap.setDefaultScope(comp.id, newScope));
|
|
154
|
-
await this.workspace.bitMap.write(`rename-scope (${oldScope} to ${newScope})`);
|
|
155
|
-
await this.workspace.clearCache();
|
|
156
|
-
|
|
157
|
-
await Promise.all(
|
|
158
|
-
envs.map(async (env) => {
|
|
159
|
-
const componentIds = compsUsingEnv[env.id.toString()];
|
|
160
|
-
if (!componentIds?.length) return;
|
|
161
|
-
const newEnvId = env.id.changeDefaultScope(newScope);
|
|
162
|
-
const newComponentIds = componentIds.map((id) => id.changeDefaultScope(newScope));
|
|
163
|
-
await this.workspace.setEnvToComponents(newEnvId, newComponentIds);
|
|
164
|
-
})
|
|
165
|
-
);
|
|
166
|
-
|
|
167
|
-
const refactoredIds: ComponentID[] = [];
|
|
168
|
-
if (options.refactor) {
|
|
169
|
-
const legacyComps = componentsUsingOldScope.map((c) => c.state._consumer);
|
|
170
|
-
const packagesToReplace: MultipleStringsReplacement = legacyComps.map((comp) => {
|
|
171
|
-
return {
|
|
172
|
-
oldStr: componentIdToPackageName(comp),
|
|
173
|
-
newStr: componentIdToPackageName({
|
|
174
|
-
...comp,
|
|
175
|
-
bindingPrefix: getBindingPrefixByDefaultScope(newScope),
|
|
176
|
-
id: comp.id.changeScope(newScope),
|
|
177
|
-
}),
|
|
178
|
-
};
|
|
179
|
-
});
|
|
180
|
-
|
|
181
|
-
const { changedComponents } = await this.refactoring.replaceMultipleStrings(allComponents, packagesToReplace, [
|
|
182
|
-
importTransformer,
|
|
183
|
-
exportTransformer,
|
|
184
|
-
]);
|
|
185
|
-
await this.renameScopeOfAspectIdsInWorkspaceConfig(
|
|
186
|
-
componentsUsingOldScope.map((c) => c.id),
|
|
187
|
-
newScope
|
|
188
|
-
);
|
|
189
|
-
await Promise.all(changedComponents.map((comp) => this.workspace.write(comp)));
|
|
190
|
-
refactoredIds.push(...changedComponents.map((c) => c.id));
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
const newIds = componentsUsingOldScope.map((comp) => new ComponentID(comp.id._legacy, newScope));
|
|
194
|
-
await this.relinkAndCompile(componentsUsingOldScope, newIds);
|
|
195
|
-
|
|
196
|
-
return { scopeRenamedComponentIds: componentsUsingOldScope.map((comp) => comp.id), refactoredIds };
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
private async relinkAndCompile(componentsUsingOldScope: Component[], newIds: ComponentID[]) {
|
|
200
|
-
this.logger.debug(`the scope has been renamed, re-linking to node-modules`);
|
|
201
|
-
await Promise.all(
|
|
202
|
-
componentsUsingOldScope.map(async (comp) => {
|
|
203
|
-
const pkgName = this.workspace.componentPackageName(comp);
|
|
204
|
-
await this.deleteLinkFromNodeModules(pkgName);
|
|
205
|
-
})
|
|
206
|
-
);
|
|
207
|
-
|
|
208
|
-
await this.workspace.clearCache();
|
|
209
|
-
await this.workspace._reloadConsumer();
|
|
210
|
-
|
|
211
|
-
const newComps = await this.workspace.getMany(newIds);
|
|
212
|
-
await linkToNodeModulesByComponents(newComps, this.workspace); // link the new-name to node-modules
|
|
213
|
-
await this.compileGracefully(newIds);
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
/**
|
|
217
|
-
* change the default-scope for new components. optionally (if refactor is true), change the source code to match the
|
|
218
|
-
* new scope-name.
|
|
219
|
-
* keep in mind that this is working for new components only, for tagged/exported it's impossible. See the errors
|
|
220
|
-
* thrown in such cases in this method.
|
|
221
|
-
*/
|
|
222
|
-
async renameOwner(
|
|
223
|
-
oldOwner: string,
|
|
224
|
-
newOwner: string,
|
|
225
|
-
options: { refactor?: boolean; ast?: boolean }
|
|
226
|
-
): Promise<RenameScopeResult> {
|
|
227
|
-
const allComponents = await this.workspace.list();
|
|
228
|
-
const isScopeUsesOldOwner = (scope: string) => scope.startsWith(`${oldOwner}.`);
|
|
229
|
-
const componentsUsingOldScope = allComponents.filter((comp) => isScopeUsesOldOwner(comp.id.scope));
|
|
230
|
-
if (!componentsUsingOldScope.length && !isScopeUsesOldOwner(this.workspace.defaultScope)) {
|
|
231
|
-
throw new OldScopeNotFound(oldOwner);
|
|
232
|
-
}
|
|
233
|
-
// verify they're all new.
|
|
234
|
-
const exported = componentsUsingOldScope.filter((comp) => this.workspace.isExported(comp.id));
|
|
235
|
-
if (exported.length) {
|
|
236
|
-
const idsStr = exported.map((comp) => comp.id.toString());
|
|
237
|
-
throw new OldScopeExported(idsStr);
|
|
238
|
-
}
|
|
239
|
-
const tagged = componentsUsingOldScope.filter((comp) => comp.id.hasVersion());
|
|
240
|
-
if (tagged.length) {
|
|
241
|
-
const idsStr = tagged.map((comp) => comp.id.toString());
|
|
242
|
-
throw new OldScopeTagged(idsStr);
|
|
243
|
-
}
|
|
244
|
-
const oldWorkspaceDefaultScope = this.workspace.defaultScope;
|
|
245
|
-
const newWorkspaceDefaultScope = isScopeUsesOldOwner(oldWorkspaceDefaultScope)
|
|
246
|
-
? this.renameOwnerInScopeName(oldWorkspaceDefaultScope, oldOwner, newOwner)
|
|
247
|
-
: undefined;
|
|
248
|
-
if (newWorkspaceDefaultScope) {
|
|
249
|
-
await this.workspace.setDefaultScope(newWorkspaceDefaultScope);
|
|
250
|
-
}
|
|
251
|
-
const newIds = componentsUsingOldScope.map((comp) => {
|
|
252
|
-
if (newWorkspaceDefaultScope && comp.id.scope === oldWorkspaceDefaultScope) {
|
|
253
|
-
this.workspace.bitMap.removeDefaultScope(comp.id);
|
|
254
|
-
return new ComponentID(comp.id._legacy, newWorkspaceDefaultScope);
|
|
255
|
-
}
|
|
256
|
-
const newCompScope = this.renameOwnerInScopeName(comp.id.scope, oldOwner, newOwner);
|
|
257
|
-
this.workspace.bitMap.setDefaultScope(comp.id, newCompScope);
|
|
258
|
-
return new ComponentID(comp.id._legacy, newCompScope);
|
|
259
|
-
});
|
|
260
|
-
await this.workspace.bitMap.write(`rename-owner (${oldOwner} to ${newOwner})`);
|
|
261
|
-
const refactoredIds: ComponentID[] = [];
|
|
262
|
-
if (options.refactor) {
|
|
263
|
-
const legacyComps = componentsUsingOldScope.map((c) => c.state._consumer);
|
|
264
|
-
const packagesToReplace: MultipleStringsReplacement = legacyComps.map((comp) => {
|
|
265
|
-
const newScope = this.renameOwnerInScopeName(comp.id.scope, oldOwner, newOwner);
|
|
266
|
-
return {
|
|
267
|
-
oldStr: componentIdToPackageName(comp),
|
|
268
|
-
newStr: componentIdToPackageName({
|
|
269
|
-
...comp,
|
|
270
|
-
bindingPrefix: getBindingPrefixByDefaultScope(newScope),
|
|
271
|
-
id: comp.id,
|
|
272
|
-
defaultScope: newScope,
|
|
273
|
-
}),
|
|
274
|
-
};
|
|
275
|
-
});
|
|
276
|
-
const { changedComponents } = await this.refactoring.replaceMultipleStrings(
|
|
277
|
-
allComponents,
|
|
278
|
-
packagesToReplace,
|
|
279
|
-
options.ast ? [importTransformer, exportTransformer] : undefined
|
|
280
|
-
);
|
|
281
|
-
await this.renameOwnerOfAspectIdsInWorkspaceConfig(
|
|
282
|
-
componentsUsingOldScope.map((c) => c.id),
|
|
283
|
-
oldOwner,
|
|
284
|
-
newOwner
|
|
285
|
-
);
|
|
286
|
-
await Promise.all(changedComponents.map((comp) => this.workspace.write(comp)));
|
|
287
|
-
refactoredIds.push(...changedComponents.map((c) => c.id));
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
await this.relinkAndCompile(componentsUsingOldScope, newIds);
|
|
291
|
-
|
|
292
|
-
return { scopeRenamedComponentIds: componentsUsingOldScope.map((comp) => comp.id), refactoredIds };
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
private async renameAspectIdInWorkspaceConfig(sourceId: ComponentID, targetId: ComponentID) {
|
|
296
|
-
const config = this.config.workspaceConfig;
|
|
297
|
-
if (!config) throw new Error('unable to get workspace config');
|
|
298
|
-
const hasChanged = config.renameExtensionInRaw(
|
|
299
|
-
sourceId.toStringWithoutVersion(),
|
|
300
|
-
targetId.toStringWithoutVersion()
|
|
301
|
-
);
|
|
302
|
-
if (hasChanged) await config.write();
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
private async renameScopeOfAspectIdsInWorkspaceConfig(ids: ComponentID[], newScope: string) {
|
|
306
|
-
const config = this.config.workspaceConfig;
|
|
307
|
-
if (!config) throw new Error('unable to get workspace config');
|
|
308
|
-
let hasChanged = false;
|
|
309
|
-
ids.forEach((id) => {
|
|
310
|
-
const changed = config.renameExtensionInRaw(
|
|
311
|
-
id.toStringWithoutVersion(),
|
|
312
|
-
id.changeScope(newScope).toStringWithoutVersion()
|
|
313
|
-
);
|
|
314
|
-
if (changed) hasChanged = true;
|
|
315
|
-
});
|
|
316
|
-
if (hasChanged) await config.write();
|
|
317
|
-
}
|
|
318
|
-
|
|
319
|
-
private async renameOwnerOfAspectIdsInWorkspaceConfig(ids: ComponentID[], oldOwner: string, newOwner: string) {
|
|
320
|
-
const config = this.config.workspaceConfig;
|
|
321
|
-
if (!config) throw new Error('unable to get workspace config');
|
|
322
|
-
let hasChanged = false;
|
|
323
|
-
ids.forEach((id) => {
|
|
324
|
-
const newScope = this.renameOwnerInScopeName(id.scope, oldOwner, newOwner);
|
|
325
|
-
const changed = config.renameExtensionInRaw(
|
|
326
|
-
id.toStringWithoutVersion(),
|
|
327
|
-
id.changeScope(newScope).toStringWithoutVersion()
|
|
328
|
-
);
|
|
329
|
-
if (changed) hasChanged = true;
|
|
330
|
-
});
|
|
331
|
-
if (hasChanged) await config.write();
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
private async deleteLinkFromNodeModules(packageName: string) {
|
|
335
|
-
await fs.remove(path.join(this.workspace.path, 'node_modules', packageName));
|
|
336
|
-
}
|
|
337
|
-
private async compileGracefully(ids: ComponentID[]) {
|
|
338
|
-
try {
|
|
339
|
-
await this.compiler.compileOnWorkspace(ids);
|
|
340
|
-
} catch (err: any) {
|
|
341
|
-
const idsStr = ids.map((id) => id.toString()).join(', ');
|
|
342
|
-
this.logger.consoleFailure(`failed compiling the component(s) ${idsStr}. error: ${err.message}`);
|
|
343
|
-
}
|
|
344
|
-
}
|
|
345
|
-
|
|
346
|
-
private renameOwnerInScopeName(scopeName: string, oldOwner: string, newOwner: string): string {
|
|
347
|
-
return scopeName.replace(`${oldOwner}.`, `${newOwner}.`);
|
|
348
|
-
}
|
|
349
|
-
|
|
350
|
-
private async getConfig(comp: Component) {
|
|
351
|
-
const fromExisting = await this.newComponentHelper.getConfigFromExistingToNewComponent(comp);
|
|
352
|
-
return {
|
|
353
|
-
...fromExisting,
|
|
354
|
-
[RenamingAspect.id]: {
|
|
355
|
-
renamedFrom: comp.id.toObject(),
|
|
356
|
-
},
|
|
357
|
-
};
|
|
358
|
-
}
|
|
359
|
-
|
|
360
|
-
static slots = [];
|
|
361
|
-
static dependencies = [
|
|
362
|
-
CLIAspect,
|
|
363
|
-
WorkspaceAspect,
|
|
364
|
-
DeprecationAspect,
|
|
365
|
-
NewComponentHelperAspect,
|
|
366
|
-
ComponentAspect,
|
|
367
|
-
GraphqlAspect,
|
|
368
|
-
RefactoringAspect,
|
|
369
|
-
InstallAspect,
|
|
370
|
-
ConfigAspect,
|
|
371
|
-
ComponentWriterAspect,
|
|
372
|
-
CompilerAspect,
|
|
373
|
-
LoggerAspect,
|
|
374
|
-
EnvsAspect,
|
|
375
|
-
];
|
|
376
|
-
static runtime = MainRuntime;
|
|
377
|
-
static async provider([
|
|
378
|
-
cli,
|
|
379
|
-
workspace,
|
|
380
|
-
deprecation,
|
|
381
|
-
newComponentHelper,
|
|
382
|
-
componentMain,
|
|
383
|
-
graphql,
|
|
384
|
-
refactoring,
|
|
385
|
-
install,
|
|
386
|
-
config,
|
|
387
|
-
componentWriter,
|
|
388
|
-
compiler,
|
|
389
|
-
loggerMain,
|
|
390
|
-
envs,
|
|
391
|
-
]: [
|
|
392
|
-
CLIMain,
|
|
393
|
-
Workspace,
|
|
394
|
-
DeprecationMain,
|
|
395
|
-
NewComponentHelperMain,
|
|
396
|
-
ComponentMain,
|
|
397
|
-
GraphqlMain,
|
|
398
|
-
RefactoringMain,
|
|
399
|
-
InstallMain,
|
|
400
|
-
ConfigMain,
|
|
401
|
-
ComponentWriterMain,
|
|
402
|
-
CompilerMain,
|
|
403
|
-
LoggerMain,
|
|
404
|
-
EnvsMain
|
|
405
|
-
]) {
|
|
406
|
-
const logger = loggerMain.createLogger(RenamingAspect.id);
|
|
407
|
-
const renaming = new RenamingMain(
|
|
408
|
-
workspace,
|
|
409
|
-
install,
|
|
410
|
-
newComponentHelper,
|
|
411
|
-
deprecation,
|
|
412
|
-
refactoring,
|
|
413
|
-
config,
|
|
414
|
-
componentWriter,
|
|
415
|
-
compiler,
|
|
416
|
-
logger,
|
|
417
|
-
envs
|
|
418
|
-
);
|
|
419
|
-
cli.register(new RenameCmd(renaming));
|
|
420
|
-
|
|
421
|
-
const scopeCommand = cli.getCommand('scope');
|
|
422
|
-
scopeCommand?.commands?.push(new ScopeRenameCmd(renaming));
|
|
423
|
-
scopeCommand?.commands?.push(new ScopeRenameOwnerCmd(renaming));
|
|
424
|
-
|
|
425
|
-
graphql.register(renamingSchema(renaming));
|
|
426
|
-
componentMain.registerShowFragments([new RenamingFragment(renaming)]);
|
|
427
|
-
return renaming;
|
|
428
|
-
}
|
|
429
|
-
}
|
|
430
|
-
|
|
431
|
-
RenamingAspect.addRuntime(RenamingMain);
|
|
432
|
-
|
|
433
|
-
export type RenameDependencyNameResult = { sourceId: ComponentID; targetId: ComponentID };
|
|
434
|
-
|
|
435
|
-
export type RenamingInfo = {
|
|
436
|
-
renamedFrom: ComponentID;
|
|
437
|
-
};
|
|
438
|
-
|
|
439
|
-
export type RenameScopeResult = { scopeRenamedComponentIds: ComponentID[]; refactoredIds: ComponentID[] };
|
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
import { Command, CommandOptions } from '@teambit/cli';
|
|
2
|
-
import chalk from 'chalk';
|
|
3
|
-
import { RenamingMain } from './renaming.main.runtime';
|
|
4
|
-
|
|
5
|
-
export class ScopeRenameOwnerCmd implements Command {
|
|
6
|
-
name = 'rename-owner <current-owner-name> <new-owner-name>';
|
|
7
|
-
description = "Renames the owner part of the scope-name for all components with the specified 'current owner name'";
|
|
8
|
-
arguments = [
|
|
9
|
-
{ name: 'current-owner-name', description: 'the owner name to be replaced by another owner name' },
|
|
10
|
-
{ name: 'new-owner-name', description: 'a new owner name to replace the current owner name' },
|
|
11
|
-
];
|
|
12
|
-
options = [
|
|
13
|
-
[
|
|
14
|
-
'r',
|
|
15
|
-
'refactor',
|
|
16
|
-
'update the import statements in all dependent components to the new package name (that contains the new owner name)',
|
|
17
|
-
],
|
|
18
|
-
['', 'ast', 'EXPERIMENTAL. use ast to transform files instead of regex'],
|
|
19
|
-
] as CommandOptions;
|
|
20
|
-
group = 'development';
|
|
21
|
-
|
|
22
|
-
constructor(private renaming: RenamingMain) {}
|
|
23
|
-
|
|
24
|
-
async report([oldName, newName]: [string, string], { refactor }: { refactor?: boolean }) {
|
|
25
|
-
const { scopeRenamedComponentIds, refactoredIds } = await this.renaming.renameOwner(oldName, newName, { refactor });
|
|
26
|
-
const title = chalk.green(`successfully replaced "${oldName}" owner with "${newName}"`);
|
|
27
|
-
const renamedIdsStr = scopeRenamedComponentIds.length
|
|
28
|
-
? `\n${chalk.bold(
|
|
29
|
-
'the following components were affected by this scope-name change:'
|
|
30
|
-
)}\n${scopeRenamedComponentIds.map((c) => c.changeScope(newName)).join('\n')}`
|
|
31
|
-
: '';
|
|
32
|
-
const refactoredStr = refactoredIds.length
|
|
33
|
-
? `\n\n${chalk.bold('the following components have been refactored:')}\n${refactoredIds.join('\n')}`
|
|
34
|
-
: '';
|
|
35
|
-
return `${title}\n${renamedIdsStr}${refactoredStr}`;
|
|
36
|
-
}
|
|
37
|
-
}
|
package/scope-rename.cmd.ts
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import { Command, CommandOptions } from '@teambit/cli';
|
|
2
|
-
import chalk from 'chalk';
|
|
3
|
-
import { RenamingMain } from './renaming.main.runtime';
|
|
4
|
-
|
|
5
|
-
export class ScopeRenameCmd implements Command {
|
|
6
|
-
name = 'rename <current-scope-name> <new-scope-name>';
|
|
7
|
-
description =
|
|
8
|
-
"Renames the scope name for all components with the specified 'current scope name' - only available for new components that have not yet been snapped/tagged";
|
|
9
|
-
extendedDescription = `Note: if \`<current-scope-name>\` is also the defaultScope for the workspace, this command will set \`<new-scope-name>\`
|
|
10
|
-
as the defaultScope instead, and that will then be set for all components by default. You may see updates in your .bitmap file
|
|
11
|
-
as a result of this change`;
|
|
12
|
-
arguments = [
|
|
13
|
-
{ name: 'current-scope-name', description: 'the scope name to be replaced by another scope name' },
|
|
14
|
-
{ name: 'new-scope-name', description: 'a new scope name to replace the current scope name' },
|
|
15
|
-
];
|
|
16
|
-
options = [
|
|
17
|
-
[
|
|
18
|
-
'r',
|
|
19
|
-
'refactor',
|
|
20
|
-
'update the import statements in all dependent components to the new package name (i.e. with the new scope name)',
|
|
21
|
-
],
|
|
22
|
-
] as CommandOptions;
|
|
23
|
-
group = 'development';
|
|
24
|
-
|
|
25
|
-
constructor(private renaming: RenamingMain) {}
|
|
26
|
-
|
|
27
|
-
async report([oldName, newName]: [string, string], { refactor }: { refactor?: boolean }) {
|
|
28
|
-
const { scopeRenamedComponentIds, refactoredIds } = await this.renaming.renameScope(oldName, newName, { refactor });
|
|
29
|
-
const title = chalk.green(`successfully replaced "${oldName}" scope with "${newName}"`);
|
|
30
|
-
const renamedIdsStr = scopeRenamedComponentIds.length
|
|
31
|
-
? `\n${chalk.bold(
|
|
32
|
-
'the following components were affected by this scope-name change:'
|
|
33
|
-
)}\n${scopeRenamedComponentIds.map((c) => c.changeScope(newName)).join('\n')}`
|
|
34
|
-
: '';
|
|
35
|
-
const refactoredStr = refactoredIds.length
|
|
36
|
-
? `\n\n${chalk.bold('the following components have been refactored:')}\n${refactoredIds.join('\n')}`
|
|
37
|
-
: '';
|
|
38
|
-
return `${title}\n${renamedIdsStr}${refactoredStr}`;
|
|
39
|
-
}
|
|
40
|
-
}
|