|
| 1 | +import fs from 'fs'; |
| 2 | +import path from 'path'; |
| 3 | +import type { API, FileInfo, JSCodeshift, Collection } from 'jscodeshift'; |
| 4 | + |
| 5 | +const mainPackageName = '@ui5/webcomponents-react'; |
| 6 | +const basePackageName = '@ui5/webcomponents-react-base'; |
| 7 | +const chartsPackageName = '@ui5/webcomponents-react-charts'; |
| 8 | +const aiPackageName = '@ui5/webcomponents-ai-react'; |
| 9 | +const compatPackageName = '@ui5/webcomponents-react-compat'; |
| 10 | +const packageNames = [mainPackageName, basePackageName, chartsPackageName, aiPackageName, compatPackageName]; |
| 11 | + |
| 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 | + |
| 46 | +// Enums for main package |
| 47 | +const libraryPath = require.resolve('@ui5/webcomponents-react/package.json'); |
| 48 | +const enumsDir = path.join(path.dirname(libraryPath), 'dist', 'enums'); |
| 49 | +let enumNames: Set<string> = new Set(); |
| 50 | +try { |
| 51 | + enumNames = new Set( |
| 52 | + fs |
| 53 | + .readdirSync(enumsDir) |
| 54 | + .filter( |
| 55 | + (file) => |
| 56 | + (file.endsWith('.js') || file.endsWith('.ts')) && !file.endsWith('.d.ts') && !file.startsWith('index'), |
| 57 | + ) |
| 58 | + .map((file) => path.basename(file, path.extname(file))), |
| 59 | + ); |
| 60 | +} catch (e) { |
| 61 | + console.warn(`⚠️ Could not read enums directory at ${enumsDir}. Skipping enum detection.`, e); |
| 62 | +} |
| 63 | + |
| 64 | +const hooksDir = path.join( |
| 65 | + path.dirname(require.resolve('@ui5/webcomponents-react-base/package.json')), |
| 66 | + 'dist', |
| 67 | + 'hooks', |
| 68 | +); |
| 69 | +const hookNames = getFileNames(hooksDir); |
| 70 | + |
| 71 | +const internalHooksDir = path.join( |
| 72 | + path.dirname(require.resolve('@ui5/webcomponents-react-base/package.json')), |
| 73 | + 'dist', |
| 74 | + 'internal', |
| 75 | + 'hooks', |
| 76 | +); |
| 77 | +const internalHookNames = getFileNames(internalHooksDir); |
| 78 | + |
| 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)); |
| 88 | + |
| 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)); |
| 98 | + |
| 99 | +// Mapping functions |
| 100 | +function resolveBaseExport(importedName: string): string | undefined { |
| 101 | + const directMap: Record<string, string> = { |
| 102 | + VersionInfo: `${basePackageName}/VersionInfo`, |
| 103 | + I18nStore: `${basePackageName}/internal/stores/I18nStore`, |
| 104 | + StyleStore: `${basePackageName}/internal/stores/StyleStore`, |
| 105 | + CssSizeVariables: `${basePackageName}/internal/styling/CssSizeVariables`, |
| 106 | + ThemingParameters: `${basePackageName}/ThemingParameters`, |
| 107 | + withWebComponent: `${basePackageName}/internal/wrapper/withWebComponent`, |
| 108 | + utils: `${basePackageName}/internal/utils`, |
| 109 | + addCustomCSSWithScoping: `${basePackageName}/internal/utils`, |
| 110 | + UI5WCSlotsNode: `${basePackageName}/types`, |
| 111 | + }; |
| 112 | + |
| 113 | + if (directMap[importedName]) { |
| 114 | + return directMap[importedName]; |
| 115 | + } |
| 116 | + if (hookNames.includes(importedName)) { |
| 117 | + return `${basePackageName}/hooks`; |
| 118 | + } |
| 119 | + if (internalHookNames.includes(importedName)) { |
| 120 | + return `${basePackageName}/internal/hooks`; |
| 121 | + } |
| 122 | + if (internalUtilNames.includes(importedName)) { |
| 123 | + return `${basePackageName}/internal/utils`; |
| 124 | + } |
| 125 | + if (internalTypeNames.includes(importedName)) { |
| 126 | + return `${basePackageName}/internal/types`; |
| 127 | + } |
| 128 | + return undefined; |
| 129 | +} |
| 130 | + |
| 131 | +function resolveChartsExport(importedName: string): string | undefined { |
| 132 | + const directMap: Record<string, string> = { |
| 133 | + TimelineChartAnnotation: `${chartsPackageName}/TimelineChartAnnotation`, |
| 134 | + BarChartPlaceholder: `${chartsPackageName}/BarChartPlaceholder`, |
| 135 | + BulletChartPlaceholder: `${chartsPackageName}/BulletChartPlaceholder`, |
| 136 | + ColumnChartPlaceholder: `${chartsPackageName}/ColumnChartPlaceholder`, |
| 137 | + ColumnChartWithTrendPlaceholder: `${chartsPackageName}/ColumnChartWithTrendPlaceholder`, |
| 138 | + ComposedChartPlaceholder: `${chartsPackageName}/ComposedChartPlaceholder`, |
| 139 | + LineChartPlaceholder: `${chartsPackageName}/LineChartPlaceholder`, |
| 140 | + PieChartPlaceholder: `${chartsPackageName}/PieChartPlaceholder`, |
| 141 | + ScatterChartPlaceholder: `${chartsPackageName}/ScatterChartPlaceholder`, |
| 142 | + TimelineChartPlaceholder: `${chartsPackageName}/TimelineChartPlaceholder`, |
| 143 | + }; |
| 144 | + if (directMap[importedName]) { |
| 145 | + return directMap[importedName]; |
| 146 | + } |
| 147 | + return undefined; |
| 148 | +} |
| 149 | + |
| 150 | +export default function transform(file: FileInfo, api: API): string | undefined { |
| 151 | + const j: JSCodeshift = api.jscodeshift; |
| 152 | + const root: Collection = j(file.source); |
| 153 | + |
| 154 | + if (file.path.includes('node_modules')) { |
| 155 | + return undefined; |
| 156 | + } |
| 157 | + |
| 158 | + let isDirty = false; |
| 159 | + |
| 160 | + packageNames.forEach((pkg) => { |
| 161 | + root.find(j.ImportDeclaration, { source: { value: pkg } }).forEach((importPath) => { |
| 162 | + const specifiers = importPath.node.specifiers || []; |
| 163 | + specifiers.forEach((spec) => { |
| 164 | + if (spec.type !== 'ImportSpecifier') return; |
| 165 | + const importedName = spec.imported.name as string; |
| 166 | + let componentName = importedName; |
| 167 | + if (importedName.endsWith('PropTypes')) { |
| 168 | + componentName = importedName.replace(/PropTypes$/, ''); |
| 169 | + } else if (importedName.endsWith('Props')) { |
| 170 | + componentName = componentName.replace(/Props$/, ''); |
| 171 | + } else if (importedName.endsWith('DomRef')) { |
| 172 | + componentName = componentName.replace(/DomRef$/, ''); |
| 173 | + } |
| 174 | + |
| 175 | + let newSource: string; |
| 176 | + if (pkg === mainPackageName) { |
| 177 | + newSource = |
| 178 | + componentName !== importedName |
| 179 | + ? `${mainPackageName}/${componentName}` |
| 180 | + : enumNames.has(importedName) |
| 181 | + ? `${mainPackageName}/enums/${importedName}` |
| 182 | + : `${mainPackageName}/${importedName}`; |
| 183 | + } else if (pkg === basePackageName && importedName !== 'Device' && importedName !== 'hooks') { |
| 184 | + newSource = resolveBaseExport(importedName) || basePackageName; |
| 185 | + } else if (pkg === chartsPackageName) { |
| 186 | + newSource = resolveChartsExport(componentName) || `${chartsPackageName}/${componentName}`; |
| 187 | + } else { |
| 188 | + newSource = pkg; |
| 189 | + } |
| 190 | + |
| 191 | + const newImport = j.importDeclaration( |
| 192 | + [ |
| 193 | + j.importSpecifier( |
| 194 | + j.identifier(importedName), |
| 195 | + j.identifier(spec.local && typeof spec.local.name === 'string' ? spec.local.name : importedName), |
| 196 | + ), |
| 197 | + ], |
| 198 | + j.literal(newSource), |
| 199 | + ); |
| 200 | + |
| 201 | + // Delta: Namespace imports |
| 202 | + if (pkg === basePackageName && ['Device', 'hooks'].includes(importedName)) { |
| 203 | + const newImport = j.importDeclaration( |
| 204 | + [j.importNamespaceSpecifier(j.identifier((spec.local?.name as string) || importedName))], |
| 205 | + j.literal(`${basePackageName}/${importedName}`), |
| 206 | + ); |
| 207 | + j(importPath).insertBefore(newImport); |
| 208 | + isDirty = true; |
| 209 | + return; |
| 210 | + } |
| 211 | + |
| 212 | + if (('importKind' in spec && spec.importKind === 'type') || importPath.node.importKind === 'type') { |
| 213 | + newImport.importKind = 'type'; |
| 214 | + } |
| 215 | + |
| 216 | + j(importPath).insertBefore(newImport); |
| 217 | + isDirty = true; |
| 218 | + }); |
| 219 | + j(importPath).remove(); |
| 220 | + }); |
| 221 | + }); |
| 222 | + |
| 223 | + return isDirty ? root.toSource() : undefined; |
| 224 | +} |
0 commit comments