| 1 | import { resolve } from 'node:path'; |
| 2 | |
| 3 | const normalizePath = (filePath: string): string => filePath.replace(/\\/g, '/'); |
| 4 | |
| 5 | export const rewriteLegacyCoreDtsPath = (filePath: string): string => { |
| 6 | const normalizedPath = normalizePath(filePath); |
| 7 | const legacyPathMarker = '/dist/js/'; |
| 8 | const markerIndex = normalizedPath.indexOf(legacyPathMarker); |
| 9 | |
| 10 | if (markerIndex === -1) { |
| 11 | return filePath; |
| 12 | } |
| 13 | |
| 14 | const distRoot = normalizedPath.slice(0, markerIndex + '/dist'.length); |
| 15 | const relativePath = normalizedPath.slice(markerIndex + legacyPathMarker.length); |
| 16 | |
| 17 | return resolve(distRoot, relativePath); |
| 18 | }; |
| 19 | |
| 20 | export const rewriteLegacyPluginDtsPath = (filePath: string, pluginName: string): string | null => { |
| 21 | const normalizedPath = normalizePath(filePath); |
| 22 | |
| 23 | if (normalizedPath.endsWith(`/${pluginName}.d.ts`)) { |
| 24 | return filePath; |
| 25 | } |
| 26 | |
| 27 | if (!normalizedPath.endsWith('/index.d.ts')) { |
| 28 | return null; |
| 29 | } |
| 30 | |
| 31 | const pluginDirectoryMarker = `/${pluginName}/`; |
| 32 | const markerIndex = normalizedPath.indexOf(pluginDirectoryMarker); |
| 33 | |
| 34 | if (markerIndex === -1) { |
| 35 | return null; |
| 36 | } |
| 37 | |
| 38 | return resolve(normalizedPath.slice(0, markerIndex), `${pluginName}.d.ts`); |
| 39 | }; |
| 40 |