@teambit/refactoring 0.0.1

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.
@@ -0,0 +1,4 @@
1
+ import { RefactoringAspect } from './refactoring.aspect';
2
+ export type { RefactoringMain } from './refactoring.main.runtime';
3
+ export default RefactoringAspect;
4
+ export { RefactoringAspect };
package/dist/index.js ADDED
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ Object.defineProperty(exports, "RefactoringAspect", {
7
+ enumerable: true,
8
+ get: function () {
9
+ return _refactoring().RefactoringAspect;
10
+ }
11
+ });
12
+ exports.default = void 0;
13
+
14
+ function _refactoring() {
15
+ const data = require("./refactoring.aspect");
16
+
17
+ _refactoring = function () {
18
+ return data;
19
+ };
20
+
21
+ return data;
22
+ }
23
+
24
+ var _default = _refactoring().RefactoringAspect;
25
+
26
+ exports.default = _default;
27
+
28
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["index.ts"],"names":["RefactoringAspect"],"mappings":";;;;;;;;;;;;;AAAA;AAAA;;AAAA;AAAA;AAAA;;AAAA;AAAA;;eAGeA,gC","sourcesContent":["import { RefactoringAspect } from './refactoring.aspect';\n\nexport type { RefactoringMain } from './refactoring.main.runtime';\nexport default RefactoringAspect;\nexport { RefactoringAspect };\n"]}
@@ -0,0 +1,23 @@
1
+ import { Command, CommandOptions } from '@teambit/cli';
2
+ import { ComponentMain } from '@teambit/component';
3
+ import { RefactoringMain } from './refactoring.main.runtime';
4
+ export declare class DependencyNameRefactorCmd implements Command {
5
+ private refactoringMain;
6
+ private componentMain;
7
+ name: string;
8
+ description: string;
9
+ options: CommandOptions;
10
+ group: string;
11
+ extendedDescription: string;
12
+ constructor(refactoringMain: RefactoringMain, componentMain: ComponentMain);
13
+ report([oldId, newId]: [string, string]): Promise<string>;
14
+ }
15
+ export declare class RefactorCmd implements Command {
16
+ name: string;
17
+ alias: string;
18
+ description: string;
19
+ options: never[];
20
+ group: string;
21
+ commands: Command[];
22
+ report([unrecognizedSubcommand]: [string]): Promise<string>;
23
+ }
@@ -0,0 +1,82 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ require("core-js/modules/es.array.iterator.js");
6
+
7
+ require("core-js/modules/es.promise.js");
8
+
9
+ Object.defineProperty(exports, "__esModule", {
10
+ value: true
11
+ });
12
+ exports.RefactorCmd = exports.DependencyNameRefactorCmd = void 0;
13
+
14
+ function _defineProperty2() {
15
+ const data = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
16
+
17
+ _defineProperty2 = function () {
18
+ return data;
19
+ };
20
+
21
+ return data;
22
+ }
23
+
24
+ function _chalk() {
25
+ const data = _interopRequireDefault(require("chalk"));
26
+
27
+ _chalk = function () {
28
+ return data;
29
+ };
30
+
31
+ return data;
32
+ }
33
+
34
+ // eslint-disable-next-line max-classes-per-file
35
+ // import { PATTERN_HELP } from '@teambit/legacy/dist/constants';
36
+ class DependencyNameRefactorCmd {
37
+ // extendedDescription = `${PATTERN_HELP('refactor dependency-name')}`;
38
+ constructor(refactoringMain, componentMain) {
39
+ this.refactoringMain = refactoringMain;
40
+ this.componentMain = componentMain;
41
+ (0, _defineProperty2().default)(this, "name", 'dependency-name <old-id> <new-id>');
42
+ (0, _defineProperty2().default)(this, "description", "replace the dependency's old package-name with a new one in the code");
43
+ (0, _defineProperty2().default)(this, "options", []);
44
+ (0, _defineProperty2().default)(this, "group", 'development');
45
+ (0, _defineProperty2().default)(this, "extendedDescription", `the "<old-id>" and "<new-id>" arguments can be either a component-id or a package-name.`);
46
+ }
47
+
48
+ async report([oldId, newId]) {
49
+ const host = this.componentMain.getHost();
50
+ const allComps = await host.list();
51
+ const {
52
+ changedComponents,
53
+ oldPackageName,
54
+ newPackageName
55
+ } = await this.refactoringMain.refactorDependencyName(allComps, oldId, newId);
56
+ await Promise.all(changedComponents.map(comp => host.write(comp)));
57
+ return `the following components have been changed (${oldPackageName} => ${newPackageName}):\n${changedComponents.map(c => c.id.toString()).join('\n')}`;
58
+ }
59
+
60
+ }
61
+
62
+ exports.DependencyNameRefactorCmd = DependencyNameRefactorCmd;
63
+
64
+ class RefactorCmd {
65
+ constructor() {
66
+ (0, _defineProperty2().default)(this, "name", 'refactor <sub-command>');
67
+ (0, _defineProperty2().default)(this, "alias", '');
68
+ (0, _defineProperty2().default)(this, "description", 'EXPERIMENTAL. source code refactoring / codemod');
69
+ (0, _defineProperty2().default)(this, "options", []);
70
+ (0, _defineProperty2().default)(this, "group", 'development');
71
+ (0, _defineProperty2().default)(this, "commands", []);
72
+ }
73
+
74
+ async report([unrecognizedSubcommand]) {
75
+ return _chalk().default.red(`"${unrecognizedSubcommand}" is not a subcommand of "refactor", please run "bit refactor --help" to list the subcommands`);
76
+ }
77
+
78
+ }
79
+
80
+ exports.RefactorCmd = RefactorCmd;
81
+
82
+ //# sourceMappingURL=refactor.cmd.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["refactor.cmd.ts"],"names":["DependencyNameRefactorCmd","constructor","refactoringMain","componentMain","report","oldId","newId","host","getHost","allComps","list","changedComponents","oldPackageName","newPackageName","refactorDependencyName","Promise","all","map","comp","write","c","id","toString","join","RefactorCmd","unrecognizedSubcommand","chalk","red"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAIA;AAAA;;AAAA;AAAA;AAAA;;AAAA;AAAA;;AAJA;AAGA;AAIO,MAAMA,yBAAN,CAAmD;AAKxD;AAGAC,EAAAA,WAAW,CAASC,eAAT,EAAmDC,aAAnD,EAAiF;AAAA,SAAxED,eAAwE,GAAxEA,eAAwE;AAAA,SAA9BC,aAA8B,GAA9BA,aAA8B;AAAA,kDAPrF,mCAOqF;AAAA,yDAN9E,sEAM8E;AAAA,qDALlF,EAKkF;AAAA,mDAJpF,aAIoF;AAAA,iEAFrE,yFAEqE;AAAE;;AAElF,QAANC,MAAM,CAAC,CAACC,KAAD,EAAQC,KAAR,CAAD,EAAmC;AAC7C,UAAMC,IAAI,GAAG,KAAKJ,aAAL,CAAmBK,OAAnB,EAAb;AACA,UAAMC,QAAQ,GAAG,MAAMF,IAAI,CAACG,IAAL,EAAvB;AACA,UAAM;AAAEC,MAAAA,iBAAF;AAAqBC,MAAAA,cAArB;AAAqCC,MAAAA;AAArC,QAAwD,MAAM,KAAKX,eAAL,CAAqBY,sBAArB,CAClEL,QADkE,EAElEJ,KAFkE,EAGlEC,KAHkE,CAApE;AAKA,UAAMS,OAAO,CAACC,GAAR,CAAYL,iBAAiB,CAACM,GAAlB,CAAuBC,IAAD,IAAUX,IAAI,CAACY,KAAL,CAAWD,IAAX,CAAhC,CAAZ,CAAN;AACA,WAAQ,+CAA8CN,cAAe,OAAMC,cAAe,OAAMF,iBAAiB,CAC9GM,GAD6F,CACxFG,CAAD,IAAOA,CAAC,CAACC,EAAF,CAAKC,QAAL,EADkF,EAE7FC,IAF6F,CAExF,IAFwF,CAElF,EAFd;AAGD;;AAtBuD;;;;AAyBnD,MAAMC,WAAN,CAAqC;AAAA;AAAA,kDACnC,wBADmC;AAAA,mDAElC,EAFkC;AAAA,yDAG5B,iDAH4B;AAAA,qDAIhC,EAJgC;AAAA,mDAKlC,aALkC;AAAA,sDAMpB,EANoB;AAAA;;AAQ9B,QAANpB,MAAM,CAAC,CAACqB,sBAAD,CAAD,EAAqC;AAC/C,WAAOC,iBAAMC,GAAN,CACJ,IAAGF,sBAAuB,+FADtB,CAAP;AAGD;;AAZyC","sourcesContent":["// eslint-disable-next-line max-classes-per-file\nimport { Command, CommandOptions } from '@teambit/cli';\nimport { ComponentMain } from '@teambit/component';\n// import { PATTERN_HELP } from '@teambit/legacy/dist/constants';\nimport chalk from 'chalk';\nimport { RefactoringMain } from './refactoring.main.runtime';\n\nexport class DependencyNameRefactorCmd implements Command {\n name = 'dependency-name <old-id> <new-id>';\n description = \"replace the dependency's old package-name with a new one in the code\";\n options = [] as CommandOptions;\n group = 'development';\n // extendedDescription = `${PATTERN_HELP('refactor dependency-name')}`;\n extendedDescription = `the \"<old-id>\" and \"<new-id>\" arguments can be either a component-id or a package-name.`;\n\n constructor(private refactoringMain: RefactoringMain, private componentMain: ComponentMain) {}\n\n async report([oldId, newId]: [string, string]) {\n const host = this.componentMain.getHost();\n const allComps = await host.list();\n const { changedComponents, oldPackageName, newPackageName } = await this.refactoringMain.refactorDependencyName(\n allComps,\n oldId,\n newId\n );\n await Promise.all(changedComponents.map((comp) => host.write(comp)));\n return `the following components have been changed (${oldPackageName} => ${newPackageName}):\\n${changedComponents\n .map((c) => c.id.toString())\n .join('\\n')}`;\n }\n}\n\nexport class RefactorCmd implements Command {\n name = 'refactor <sub-command>';\n alias = '';\n description = 'EXPERIMENTAL. source code refactoring / codemod';\n options = [];\n group = 'development';\n commands: Command[] = [];\n\n async report([unrecognizedSubcommand]: [string]) {\n return chalk.red(\n `\"${unrecognizedSubcommand}\" is not a subcommand of \"refactor\", please run \"bit refactor --help\" to list the subcommands`\n );\n }\n}\n"]}
@@ -0,0 +1,2 @@
1
+ import { Aspect } from '@teambit/harmony';
2
+ export declare const RefactoringAspect: Aspect;
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.RefactoringAspect = void 0;
7
+
8
+ function _harmony() {
9
+ const data = require("@teambit/harmony");
10
+
11
+ _harmony = function () {
12
+ return data;
13
+ };
14
+
15
+ return data;
16
+ }
17
+
18
+ const RefactoringAspect = _harmony().Aspect.create({
19
+ id: 'teambit.component/refactoring'
20
+ });
21
+
22
+ exports.RefactoringAspect = RefactoringAspect;
23
+
24
+ //# sourceMappingURL=refactoring.aspect.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["refactoring.aspect.ts"],"names":["RefactoringAspect","Aspect","create","id"],"mappings":";;;;;;;AAAA;AAAA;;AAAA;AAAA;AAAA;;AAAA;AAAA;;AAEO,MAAMA,iBAAiB,GAAGC,kBAAOC,MAAP,CAAc;AAC7CC,EAAAA,EAAE,EAAE;AADyC,CAAd,CAA1B","sourcesContent":["import { Aspect } from '@teambit/harmony';\n\nexport const RefactoringAspect = Aspect.create({\n id: 'teambit.component/refactoring',\n});\n"]}
@@ -0,0 +1,26 @@
1
+ import { CLIMain } from '@teambit/cli';
2
+ import { Component, ComponentID, ComponentMain } from '@teambit/component';
3
+ import { PkgMain } from '@teambit/pkg';
4
+ export declare class RefactoringMain {
5
+ private componentMain;
6
+ private pkg;
7
+ constructor(componentMain: ComponentMain, pkg: PkgMain);
8
+ /**
9
+ * refactor the dependency name of a component.
10
+ * oldId and newId can be either a component-id or a package-name.
11
+ * this method changes the source code of the component, but doesn't write to the filesystem.
12
+ */
13
+ refactorDependencyName(components: Component[], oldId: ComponentID | string, newId: ComponentID | string): Promise<{
14
+ oldPackageName: string;
15
+ newPackageName: string;
16
+ changedComponents: Component[];
17
+ }>;
18
+ private getPackageNameByUnknownId;
19
+ private getPackageNameByComponentID;
20
+ private isValidScopedPackageName;
21
+ private replaceString;
22
+ static slots: never[];
23
+ static dependencies: import("@teambit/harmony").Aspect[];
24
+ static runtime: import("@teambit/harmony").RuntimeDefinition;
25
+ static provider([componentMain, pkg, cli]: [ComponentMain, PkgMain, CLIMain]): Promise<RefactoringMain>;
26
+ }
@@ -0,0 +1,214 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ require("core-js/modules/es.array.iterator.js");
6
+
7
+ require("core-js/modules/es.promise.js");
8
+
9
+ require("core-js/modules/es.regexp.constructor.js");
10
+
11
+ require("core-js/modules/es.regexp.exec.js");
12
+
13
+ require("core-js/modules/es.string.replace.js");
14
+
15
+ Object.defineProperty(exports, "__esModule", {
16
+ value: true
17
+ });
18
+ exports.RefactoringMain = void 0;
19
+
20
+ function _defineProperty2() {
21
+ const data = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
22
+
23
+ _defineProperty2 = function () {
24
+ return data;
25
+ };
26
+
27
+ return data;
28
+ }
29
+
30
+ function _cli() {
31
+ const data = require("@teambit/cli");
32
+
33
+ _cli = function () {
34
+ return data;
35
+ };
36
+
37
+ return data;
38
+ }
39
+
40
+ function _isbinaryfile() {
41
+ const data = require("isbinaryfile");
42
+
43
+ _isbinaryfile = function () {
44
+ return data;
45
+ };
46
+
47
+ return data;
48
+ }
49
+
50
+ function _lodash() {
51
+ const data = require("lodash");
52
+
53
+ _lodash = function () {
54
+ return data;
55
+ };
56
+
57
+ return data;
58
+ }
59
+
60
+ function _component() {
61
+ const data = _interopRequireWildcard(require("@teambit/component"));
62
+
63
+ _component = function () {
64
+ return data;
65
+ };
66
+
67
+ return data;
68
+ }
69
+
70
+ function _bitError() {
71
+ const data = require("@teambit/bit-error");
72
+
73
+ _bitError = function () {
74
+ return data;
75
+ };
76
+
77
+ return data;
78
+ }
79
+
80
+ function _pkg() {
81
+ const data = _interopRequireDefault(require("@teambit/pkg"));
82
+
83
+ _pkg = function () {
84
+ return data;
85
+ };
86
+
87
+ return data;
88
+ }
89
+
90
+ function _refactoring() {
91
+ const data = require("./refactoring.aspect");
92
+
93
+ _refactoring = function () {
94
+ return data;
95
+ };
96
+
97
+ return data;
98
+ }
99
+
100
+ function _refactor() {
101
+ const data = require("./refactor.cmd");
102
+
103
+ _refactor = function () {
104
+ return data;
105
+ };
106
+
107
+ return data;
108
+ }
109
+
110
+ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
111
+
112
+ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
113
+
114
+ class RefactoringMain {
115
+ constructor(componentMain, pkg) {
116
+ this.componentMain = componentMain;
117
+ this.pkg = pkg;
118
+ }
119
+ /**
120
+ * refactor the dependency name of a component.
121
+ * oldId and newId can be either a component-id or a package-name.
122
+ * this method changes the source code of the component, but doesn't write to the filesystem.
123
+ */
124
+
125
+
126
+ async refactorDependencyName(components, oldId, newId) {
127
+ const oldPackageName = await this.getPackageNameByUnknownId(oldId);
128
+ const newPackageName = await this.getPackageNameByUnknownId(newId);
129
+
130
+ if (oldPackageName === newPackageName) {
131
+ throw new (_bitError().BitError)(`refactoring: the old package-name and the new package-name are the same: ${oldPackageName}`);
132
+ }
133
+
134
+ const changedComponents = await Promise.all(components.map(async comp => {
135
+ const hasChanged = await this.replaceString(comp, oldPackageName, newPackageName);
136
+ return hasChanged ? comp : null;
137
+ }));
138
+ return {
139
+ oldPackageName,
140
+ newPackageName,
141
+ changedComponents: (0, _lodash().compact)(changedComponents)
142
+ };
143
+ }
144
+
145
+ async getPackageNameByUnknownId(id) {
146
+ if (id instanceof _component().ComponentID) {
147
+ return this.getPackageNameByComponentID(id);
148
+ }
149
+
150
+ if (typeof id !== 'string') {
151
+ throw new Error(`getPackageNameByUnknownId expects id to be either string or ComponentID, got ${typeof id}`);
152
+ }
153
+
154
+ try {
155
+ const host = this.componentMain.getHost();
156
+ const componentID = await host.resolveComponentId(id);
157
+ return await this.getPackageNameByComponentID(componentID);
158
+ } catch (err) {
159
+ if (this.isValidScopedPackageName(id)) {
160
+ return id; // assume this is a package-name
161
+ }
162
+
163
+ throw new (_bitError().BitError)(`refactoring: the id "${id}" is neither a valid scoped-package-name nor an existing component-id`);
164
+ }
165
+ }
166
+
167
+ async getPackageNameByComponentID(id) {
168
+ const host = this.componentMain.getHost();
169
+ const comp = await host.get(id);
170
+ if (!comp) throw new Error(`unable to find a component: "${id.toString()}"`);
171
+ return this.pkg.getPackageName(comp);
172
+ }
173
+
174
+ isValidScopedPackageName(name) {
175
+ return name.startsWith('@') && name.includes('/') && name.length <= 214 && !name.includes('\\') && !name.includes('..');
176
+ }
177
+
178
+ async replaceString(comp, oldString, newString) {
179
+ const changed = await Promise.all(comp.filesystem.files.map(async file => {
180
+ const isBinary = await (0, _isbinaryfile().isBinaryFile)(file.contents);
181
+ if (isBinary) return false;
182
+ const strContent = file.contents.toString();
183
+
184
+ if (strContent.includes(oldString)) {
185
+ const oldStringRegex = new RegExp(oldString, 'g');
186
+ const newContent = strContent.replace(oldStringRegex, newString);
187
+ file.contents = Buffer.from(newContent);
188
+ return true;
189
+ }
190
+
191
+ return false;
192
+ }));
193
+ return changed.some(c => c);
194
+ }
195
+
196
+ static async provider([componentMain, pkg, cli]) {
197
+ const refactoringMain = new RefactoringMain(componentMain, pkg);
198
+ const subCommands = [new (_refactor().DependencyNameRefactorCmd)(refactoringMain, componentMain)];
199
+ const refactorCmd = new (_refactor().RefactorCmd)();
200
+ refactorCmd.commands = subCommands;
201
+ cli.register(refactorCmd);
202
+ return refactoringMain;
203
+ }
204
+
205
+ }
206
+
207
+ exports.RefactoringMain = RefactoringMain;
208
+ (0, _defineProperty2().default)(RefactoringMain, "slots", []);
209
+ (0, _defineProperty2().default)(RefactoringMain, "dependencies", [_component().default, _pkg().default, _cli().CLIAspect]);
210
+ (0, _defineProperty2().default)(RefactoringMain, "runtime", _cli().MainRuntime);
211
+
212
+ _refactoring().RefactoringAspect.addRuntime(RefactoringMain);
213
+
214
+ //# sourceMappingURL=refactoring.main.runtime.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["refactoring.main.runtime.ts"],"names":["RefactoringMain","constructor","componentMain","pkg","refactorDependencyName","components","oldId","newId","oldPackageName","getPackageNameByUnknownId","newPackageName","BitError","changedComponents","Promise","all","map","comp","hasChanged","replaceString","id","ComponentID","getPackageNameByComponentID","Error","host","getHost","componentID","resolveComponentId","err","isValidScopedPackageName","get","toString","getPackageName","name","startsWith","includes","length","oldString","newString","changed","filesystem","files","file","isBinary","contents","strContent","oldStringRegex","RegExp","newContent","replace","Buffer","from","some","c","provider","cli","refactoringMain","subCommands","DependencyNameRefactorCmd","refactorCmd","RefactorCmd","commands","register","ComponentAspect","PkgAspect","CLIAspect","MainRuntime","RefactoringAspect","addRuntime"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;;AAAA;AAAA;AAAA;;AAAA;AAAA;;AACA;AAAA;;AAAA;AAAA;AAAA;;AAAA;AAAA;;AACA;AAAA;;AAAA;AAAA;AAAA;;AAAA;AAAA;;AACA;AAAA;;AAAA;AAAA;AAAA;;AAAA;AAAA;;AACA;AAAA;;AAAA;AAAA;AAAA;;AAAA;AAAA;;AACA;AAAA;;AAAA;AAAA;AAAA;;AAAA;AAAA;;AACA;AAAA;;AAAA;AAAA;AAAA;;AAAA;AAAA;;AACA;AAAA;;AAAA;AAAA;AAAA;;AAAA;AAAA;;;;;;AAEO,MAAMA,eAAN,CAAsB;AAC3BC,EAAAA,WAAW,CAASC,aAAT,EAA+CC,GAA/C,EAA6D;AAAA,SAApDD,aAAoD,GAApDA,aAAoD;AAAA,SAAdC,GAAc,GAAdA,GAAc;AAAE;AAE1E;AACF;AACA;AACA;AACA;;;AAC8B,QAAtBC,sBAAsB,CAC1BC,UAD0B,EAE1BC,KAF0B,EAG1BC,KAH0B,EAImE;AAC7F,UAAMC,cAAc,GAAG,MAAM,KAAKC,yBAAL,CAA+BH,KAA/B,CAA7B;AACA,UAAMI,cAAc,GAAG,MAAM,KAAKD,yBAAL,CAA+BF,KAA/B,CA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B;AACA,eAAO,IAAP;AACD;;AACD,aAAO,KAAP;AACD,KAXD,CADoB,CAAtB;AAcA,WAAOT,OAAO,CAACa,IAAR,CAAcC,CAAD,IAAOA,CAApB,CAAP;AACD;;AAKoB,eAARC,QAAQ,CAAC,CAACnD,aAAD,EAAgBC,GAAhB,EAAqBmD,GAArB,CAAD,EAA+D;AAClF,UAAMC,eAAe,GAAG,IAAIvD,eAAJ,CAAoBE,aAApB,EAAmCC,GAAnC,CAAxB;AACA,UAAMqD,WAAW,GAAG,CAAC,KAAIC,qCAAJ,EAA8BF,eAA9B,EAA+CrD,aAA/C,CAAD,CAApB;AACA,UAAMwD,WAAW,GAAG,KAAIC,uBAAJ,GAApB;AACAD,IAAAA,WAAW,CAACE,QAAZ,GAAuBJ,WAAvB;AACAF,IAAAA,GAAG,CAACO,QAAJ,CAAaH,WAAb;AACA,WAAOH,eAAP;AACD;;AAzF0B;;;gCAAhBvD,e,WA+EI,E;gCA/EJA,e,kBAgFW,CAAC8D,oBAAD,EAAkBC,cAAlB,EAA6BC,gBAA7B,C;gCAhFXhE,e,aAiFMiE,kB;;AAWnBC,iCAAkBC,UAAlB,CAA6BnE,eAA7B","sourcesContent":["import { CLIAspect, CLIMain, MainRuntime } from '@teambit/cli';\nimport { isBinaryFile } from 'isbinaryfile';\nimport { compact } from 'lodash';\nimport ComponentAspect, { Component, ComponentID, ComponentMain } from '@teambit/component';\nimport { BitError } from '@teambit/bit-error';\nimport PkgAspect, { PkgMain } from '@teambit/pkg';\nimport { RefactoringAspect } from './refactoring.aspect';\nimport { DependencyNameRefactorCmd, RefactorCmd } from './refactor.cmd';\n\nexport class RefactoringMain {\n constructor(private componentMain: ComponentMain, private pkg: PkgMain) {}\n\n /**\n * refactor the dependency name of a component.\n * oldId and newId can be either a component-id or a package-name.\n * this method changes the source code of the component, but doesn't write to the filesystem.\n */\n async refactorDependencyName(\n components: Component[],\n oldId: ComponentID | string,\n newId: ComponentID | string\n ): Promise<{ oldPackageName: string; newPackageName: string; changedComponents: Component[] }> {\n const oldPackageName = await this.getPackageNameByUnknownId(oldId);\n const newPackageName = await this.getPackageNameByUnknownId(newId);\n if (oldPackageName === newPackageName) {\n throw new BitError(`refactoring: the old package-name and the new package-name are the same: ${oldPackageName}`);\n }\n const changedComponents = await Promise.all(\n components.map(async (comp) => {\n const hasChanged = await this.replaceString(comp, oldPackageName, newPackageName);\n return hasChanged ? comp : null;\n })\n );\n return { oldPackageName, newPackageName, changedComponents: compact(changedComponents) };\n }\n\n private async getPackageNameByUnknownId(id: ComponentID | string): Promise<string> {\n if (id instanceof ComponentID) {\n return this.getPackageNameByComponentID(id);\n }\n if (typeof id !== 'string') {\n throw new Error(`getPackageNameByUnknownId expects id to be either string or ComponentID, got ${typeof id}`);\n }\n try {\n const host = this.componentMain.getHost();\n const componentID = await host.resolveComponentId(id);\n return await this.getPackageNameByComponentID(componentID);\n } catch (err) {\n if (this.isValidScopedPackageName(id)) {\n return id; // assume this is a package-name\n }\n throw new BitError(\n `refactoring: the id \"${id}\" is neither a valid scoped-package-name nor an existing component-id`\n );\n }\n }\n\n private async getPackageNameByComponentID(id: ComponentID) {\n const host = this.componentMain.getHost();\n const comp = await host.get(id);\n if (!comp) throw new Error(`unable to find a component: \"${id.toString()}\"`);\n return this.pkg.getPackageName(comp);\n }\n\n private isValidScopedPackageName(name: string) {\n return (\n name.startsWith('@') && name.includes('/') && name.length <= 214 && !name.includes('\\\\') && !name.includes('..')\n );\n }\n\n private async replaceString(comp: Component, oldString: string, newString: string): Promise<boolean> {\n const changed = await Promise.all(\n comp.filesystem.files.map(async (file) => {\n const isBinary = await isBinaryFile(file.contents);\n if (isBinary) return false;\n const strContent = file.contents.toString();\n if (strContent.includes(oldString)) {\n const oldStringRegex = new RegExp(oldString, 'g');\n const newContent = strContent.replace(oldStringRegex, newString);\n file.contents = Buffer.from(newContent);\n return true;\n }\n return false;\n })\n );\n return changed.some((c) => c);\n }\n\n static slots = [];\n static dependencies = [ComponentAspect, PkgAspect, CLIAspect];\n static runtime = MainRuntime;\n static async provider([componentMain, pkg, cli]: [ComponentMain, PkgMain, CLIMain]) {\n const refactoringMain = new RefactoringMain(componentMain, pkg);\n const subCommands = [new DependencyNameRefactorCmd(refactoringMain, componentMain)];\n const refactorCmd = new RefactorCmd();\n refactorCmd.commands = subCommands;\n cli.register(refactorCmd);\n return refactoringMain;\n }\n}\n\nRefactoringAspect.addRuntime(RefactoringMain);\n"]}
package/package.json ADDED
@@ -0,0 +1,87 @@
1
+ {
2
+ "name": "@teambit/refactoring",
3
+ "version": "0.0.1",
4
+ "main": "dist/index.js",
5
+ "componentId": {
6
+ "scope": "teambit.component",
7
+ "name": "refactoring",
8
+ "version": "0.0.1"
9
+ },
10
+ "dependencies": {
11
+ "chalk": "2.4.2",
12
+ "@teambit/harmony": "0.2.11",
13
+ "isbinaryfile": "4.0.6",
14
+ "lodash": "4.17.21",
15
+ "@babel/runtime": "7.12.18",
16
+ "core-js": "^3.0.0",
17
+ "@teambit/cli": "0.0.468",
18
+ "@teambit/component": "0.0.704",
19
+ "@teambit/bit-error": "0.0.394",
20
+ "@teambit/pkg": "0.0.704"
21
+ },
22
+ "devDependencies": {
23
+ "@types/lodash": "4.14.165",
24
+ "@types/mocha": "9.1.0",
25
+ "@types/testing-library__jest-dom": "5.9.5",
26
+ "@types/jest": "^26.0.0",
27
+ "@types/react-dom": "^17.0.5",
28
+ "@types/react": "^17.0.8",
29
+ "@types/node": "12.20.4"
30
+ },
31
+ "peerDependencies": {
32
+ "@teambit/legacy": "1.0.246",
33
+ "react-dom": "^16.8.0 || ^17.0.0",
34
+ "react": "^16.8.0 || ^17.0.0"
35
+ },
36
+ "license": "Apache-2.0",
37
+ "bit": {
38
+ "bindingPrefix": "@teambit",
39
+ "env": {},
40
+ "overrides": {
41
+ "dependencies": {
42
+ "@teambit/legacy": "-",
43
+ "@babel/runtime": "7.12.18",
44
+ "core-js": "^3.0.0",
45
+ "react-dom": "-",
46
+ "react": "-"
47
+ },
48
+ "devDependencies": {
49
+ "@teambit/legacy": "-",
50
+ "@types/mocha": "9.1.0",
51
+ "@types/testing-library__jest-dom": "5.9.5",
52
+ "@types/jest": "^26.0.0",
53
+ "@types/react-dom": "^17.0.5",
54
+ "@types/react": "^17.0.8",
55
+ "@types/node": "12.20.4",
56
+ "react-dom": "-",
57
+ "react": "-"
58
+ },
59
+ "peerDependencies": {
60
+ "@teambit/legacy": "1.0.246",
61
+ "react-dom": "^16.8.0 || ^17.0.0",
62
+ "react": "^16.8.0 || ^17.0.0"
63
+ }
64
+ }
65
+ },
66
+ "private": false,
67
+ "engines": {
68
+ "node": ">=12.22.0"
69
+ },
70
+ "repository": {
71
+ "type": "git",
72
+ "url": "https://github.com/teambit/bit"
73
+ },
74
+ "keywords": [
75
+ "bit",
76
+ "bit-aspect",
77
+ "components",
78
+ "collaboration",
79
+ "web",
80
+ "react",
81
+ "react-components",
82
+ "angular",
83
+ "angular-components",
84
+ "vue",
85
+ "vue-components"
86
+ ]
87
+ }
@@ -0,0 +1 @@
1
+
package/tsconfig.json ADDED
@@ -0,0 +1,35 @@
1
+ {
2
+ "compilerOptions": {
3
+ "lib": [
4
+ "es2019",
5
+ "DOM",
6
+ "ES6",
7
+ "DOM.Iterable",
8
+ "ScriptHost"
9
+ ],
10
+ "target": "es2015",
11
+ "module": "commonjs",
12
+ "jsx": "react",
13
+ "allowJs": true,
14
+ "composite": true,
15
+ "declaration": true,
16
+ "sourceMap": true,
17
+ "skipLibCheck": true,
18
+ "outDir": "dist",
19
+ "moduleResolution": "node",
20
+ "esModuleInterop": true,
21
+ "rootDir": ".",
22
+ "resolveJsonModule": true,
23
+ "emitDeclarationOnly": true,
24
+ "experimentalDecorators": true,
25
+ "emitDecoratorMetadata": true,
26
+ "allowSyntheticDefaultImports": true,
27
+ "strictPropertyInitialization": false,
28
+ "strict": true,
29
+ "noImplicitAny": false,
30
+ "preserveConstEnums": true
31
+ },
32
+ "exclude": [
33
+ "dist"
34
+ ]
35
+ }
@@ -0,0 +1,29 @@
1
+ declare module '*.png' {
2
+ const value: any;
3
+ export = value;
4
+ }
5
+ declare module '*.svg' {
6
+ import type { FunctionComponent, SVGProps } from 'react';
7
+
8
+ export const ReactComponent: FunctionComponent<SVGProps<SVGSVGElement> & { title?: string }>;
9
+ const src: string;
10
+ export default src;
11
+ }
12
+
13
+ // @TODO Gilad
14
+ declare module '*.jpg' {
15
+ const value: any;
16
+ export = value;
17
+ }
18
+ declare module '*.jpeg' {
19
+ const value: any;
20
+ export = value;
21
+ }
22
+ declare module '*.gif' {
23
+ const value: any;
24
+ export = value;
25
+ }
26
+ declare module '*.bmp' {
27
+ const value: any;
28
+ export = value;
29
+ }
@@ -0,0 +1,42 @@
1
+ declare module '*.module.css' {
2
+ const classes: { readonly [key: string]: string };
3
+ export default classes;
4
+ }
5
+ declare module '*.module.scss' {
6
+ const classes: { readonly [key: string]: string };
7
+ export default classes;
8
+ }
9
+ declare module '*.module.sass' {
10
+ const classes: { readonly [key: string]: string };
11
+ export default classes;
12
+ }
13
+
14
+ declare module '*.module.less' {
15
+ const classes: { readonly [key: string]: string };
16
+ export default classes;
17
+ }
18
+
19
+ declare module '*.less' {
20
+ const classes: { readonly [key: string]: string };
21
+ export default classes;
22
+ }
23
+
24
+ declare module '*.css' {
25
+ const classes: { readonly [key: string]: string };
26
+ export default classes;
27
+ }
28
+
29
+ declare module '*.sass' {
30
+ const classes: { readonly [key: string]: string };
31
+ export default classes;
32
+ }
33
+
34
+ declare module '*.scss' {
35
+ const classes: { readonly [key: string]: string };
36
+ export default classes;
37
+ }
38
+
39
+ declare module '*.mdx' {
40
+ const component: any;
41
+ export default component;
42
+ }