Skip to content

Commit c8cba5a

Browse files
committed
adjust codemod to refactoring
1 parent c9620a6 commit c8cba5a

File tree

2 files changed

+91
-57
lines changed

2 files changed

+91
-57
lines changed

packages/base/package.json

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,10 @@
4747
"types": "./dist/wrapper/withWebComponent.d.ts",
4848
"default": "./dist/wrapper/withWebComponent.js"
4949
},
50-
"./utils": {
51-
"types": "./dist/utils/index.d.ts",
52-
"default": "./dist/utils/index.js"
53-
},
5450
"./types": {
55-
"types": "./dist/types/index.d.ts",
56-
"default": "./dist/types/index.js"
51+
"types": "./dist/types/index.d.ts"
5752
},
58-
"./internal": "./dist/internal",
53+
"./internal/*": "./dist/internal/*",
5954
"./internal/addCustomCSSWithScoping.js": "./dist/utils/addCustomCSSWithScoping.js"
6055
},
6156
"homepage": "https://ui5.github.io/webcomponents-react",

packages/cli/src/scripts/codemod/transforms/export-maps/main.cts

Lines changed: 89 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,42 @@ const basePackageName = '@ui5/webcomponents-react-base';
77
const chartsPackageName = '@ui5/webcomponents-react-charts';
88
const aiPackageName = '@ui5/webcomponents-ai-react';
99
const compatPackageName = '@ui5/webcomponents-react-compat';
10-
1110
const packageNames = [mainPackageName, basePackageName, chartsPackageName, aiPackageName, compatPackageName];
1211

12+
function getFileNames(dir: string) {
13+
let fileNames: string[] = [];
14+
try {
15+
fileNames = fs
16+
.readdirSync(dir)
17+
.filter(
18+
(file) =>
19+
(file.endsWith('.js') || file.endsWith('.ts')) && !file.endsWith('.d.ts') && !file.startsWith('index'),
20+
)
21+
.map((file) => path.basename(file, path.extname(file)));
22+
} catch (e) {
23+
console.warn(`⚠️ Could not read directory at ${dir}.`, e);
24+
}
25+
26+
return fileNames;
27+
}
28+
29+
function getExportNames(indexPath: string) {
30+
let exportNames: string[] = [];
31+
try {
32+
const indexSource = fs.readFileSync(indexPath, 'utf-8');
33+
const exportRegex = /export\s+(?:const|function|class|type|interface|{[^}]+})\s+([a-zA-Z0-9_]+)/g;
34+
let match;
35+
while ((match = exportRegex.exec(indexSource)) !== null) {
36+
exportNames.push(match[1]);
37+
}
38+
exportNames = Array.from(new Set(exportNames)); // Remove duplicates
39+
} catch (e) {
40+
console.warn(`⚠️ Could not read index at ${indexPath}.`, e);
41+
}
42+
43+
return exportNames;
44+
}
45+
1346
// Enums for main package
1447
const libraryPath = require.resolve('@ui5/webcomponents-react/package.json');
1548
const enumsDir = path.join(path.dirname(libraryPath), 'dist', 'enums');
@@ -33,61 +66,46 @@ const hooksDir = path.join(
3366
'dist',
3467
'hooks',
3568
);
36-
let hookNames: string[] = [];
37-
try {
38-
hookNames = fs
39-
.readdirSync(hooksDir)
40-
.filter(
41-
(file) => (file.endsWith('.js') || file.endsWith('.ts')) && !file.endsWith('.d.ts') && !file.startsWith('index'),
42-
)
43-
.map((file) => path.basename(file, path.extname(file)));
44-
} catch (e) {
45-
console.warn(`⚠️ Could not read hooks directory at ${hooksDir}.`, e);
46-
}
69+
const hookNames = getFileNames(hooksDir);
4770

48-
const utilsDir = path.join(
71+
const internalHooksDir = path.join(
4972
path.dirname(require.resolve('@ui5/webcomponents-react-base/package.json')),
5073
'dist',
51-
'utils',
74+
'internal',
75+
'hooks',
5276
);
53-
let utilNames: string[] = [];
54-
try {
55-
utilNames = fs
56-
.readdirSync(utilsDir)
57-
.filter(
58-
(file) => (file.endsWith('.js') || file.endsWith('.ts')) && !file.endsWith('.d.ts') && !file.startsWith('index'),
59-
)
60-
.map((file) => path.basename(file, path.extname(file)));
61-
} catch (e) {
62-
console.warn(`⚠️ Could not read utils directory at ${utilsDir}.`, e);
63-
}
77+
const internalHookNames = getFileNames(internalHooksDir);
6478

65-
const utilsIndexPath = path.join(utilsDir, 'index.js');
79+
const internalUtilsDir = path.join(
80+
path.dirname(require.resolve('@ui5/webcomponents-react-base/package.json')),
81+
'dist',
82+
'internal',
83+
'utils',
84+
);
85+
const internalUtilNames: string[] = getFileNames(internalUtilsDir);
86+
const utilsIndexPath = path.join(internalUtilsDir, 'index.js');
87+
internalUtilNames.push(...getExportNames(utilsIndexPath));
6688

67-
try {
68-
const indexSource = fs.readFileSync(utilsIndexPath, 'utf-8');
69-
const exportRegex = /export\s+(?:const|function|class|type|interface|{[^}]+})\s+([a-zA-Z0-9_]+)/g;
70-
let match;
71-
while ((match = exportRegex.exec(indexSource)) !== null) {
72-
utilNames.push(match[1]);
73-
}
74-
utilNames = Array.from(new Set(utilNames)); // Remove duplicates
75-
} catch (e) {
76-
console.warn(`⚠️ Could not read utils index at ${utilsIndexPath}.`, e);
77-
}
89+
const internalTypeDir = path.join(
90+
path.dirname(require.resolve('@ui5/webcomponents-react-base/package.json')),
91+
'dist',
92+
'internal',
93+
'types',
94+
);
95+
const internalTypeNames: string[] = getFileNames(internalTypeDir);
96+
const internalTypesIndexPath = path.join(internalTypeDir, 'index.js');
97+
internalTypeNames.push(...getExportNames(internalTypesIndexPath));
7898

7999
// Mapping functions
80100
function resolveBaseExport(importedName: string): string | undefined {
81101
const directMap: Record<string, string> = {
82-
Device: `${basePackageName}/Device`,
83-
hooks: `${basePackageName}/hooks`,
84102
VersionInfo: `${basePackageName}/VersionInfo`,
85-
I18nStore: `${basePackageName}/I18nStore`,
86-
StyleStore: `${basePackageName}/StyleStore`,
87-
CssSizeVariables: `${basePackageName}/CssSizeVariables`,
103+
I18nStore: `${basePackageName}/internal/stores/I18nStore.js`,
104+
StyleStore: `${basePackageName}/internal/stores/StyleStore.js`,
105+
CssSizeVariables: `${basePackageName}/internal/styling/CssSizeVariables.js`,
88106
ThemingParameters: `${basePackageName}/ThemingParameters`,
89-
withWebComponent: `${basePackageName}/withWebComponent`,
90-
utils: `${basePackageName}/utils`,
107+
withWebComponent: `${basePackageName}/internal/wrapper/withWebComponent.js`,
108+
utils: `${basePackageName}/internal/utils/index.js`,
91109
addCustomCSSWithScoping: `${basePackageName}/internal/addCustomCSSWithScoping.js`,
92110
};
93111

@@ -97,12 +115,22 @@ function resolveBaseExport(importedName: string): string | undefined {
97115
if (hookNames.includes(importedName)) {
98116
return `${basePackageName}/hooks`;
99117
}
100-
if (utilNames.includes(importedName)) {
101-
return `${basePackageName}/utils`;
118+
if (internalHookNames.includes(importedName)) {
119+
return `${basePackageName}/internal/hooks/index.js`;
102120
}
103-
if (importedName === 'default' || importedName === 'index') {
104-
return basePackageName;
121+
if (internalUtilNames.includes(importedName)) {
122+
return `${basePackageName}/internal/utils/index.js`;
123+
}
124+
if (internalTypeNames.includes(importedName)) {
125+
return `${basePackageName}/internal/types/index.js`;
105126
}
127+
if (importedName === 'UI5WCSlotsNode') {
128+
return `${basePackageName}/types`;
129+
}
130+
//todo: why is this required?
131+
// if (importedName === 'default' || importedName === 'index') {
132+
// return basePackageName;
133+
// }
106134
return undefined;
107135
}
108136

@@ -161,7 +189,7 @@ export default function transform(file: FileInfo, api: API): string | undefined
161189
: enumNames.has(importedName)
162190
? `${mainPackageName}/enums/${importedName}`
163191
: `${mainPackageName}/${importedName}`;
164-
} else if (pkg === basePackageName) {
192+
} else if (pkg === basePackageName && importedName !== 'Device' && importedName !== 'hooks') {
165193
newSource = resolveBaseExport(importedName) || basePackageName;
166194
} else if (pkg === chartsPackageName) {
167195
newSource = resolveChartsExport(componentName) || `${chartsPackageName}/${componentName}`;
@@ -178,6 +206,18 @@ export default function transform(file: FileInfo, api: API): string | undefined
178206
],
179207
j.literal(newSource),
180208
);
209+
210+
// Namespace import deltas
211+
if (pkg === basePackageName && ['Device', 'hooks'].includes(importedName)) {
212+
const newImport = j.importDeclaration(
213+
[j.importNamespaceSpecifier(j.identifier(spec.local?.name || importedName))],
214+
j.literal(`${basePackageName}/${importedName}`),
215+
);
216+
j(importPath).insertBefore(newImport);
217+
isDirty = true;
218+
return;
219+
}
220+
181221
if ('importKind' in spec && spec.importKind === 'type') {
182222
newImport.importKind = 'type';
183223
}
@@ -188,6 +228,5 @@ export default function transform(file: FileInfo, api: API): string | undefined
188228
});
189229
});
190230

191-
//todo: 'use client' and other string expressions will receive two semicolons. This can be fixed by running prettier - is there a better option?
192231
return isDirty ? root.toSource() : undefined;
193232
}

0 commit comments

Comments
 (0)