|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +const fs = require('fs'); |
| 4 | +const path = require('path'); |
| 5 | + |
| 6 | +const packageDistPath = path.join(__dirname, '..', 'node_modules', '@bitsocialhq', 'bitsocial-react-hooks', 'dist'); |
| 7 | +const logPrefix = '[patch-bitsocial-react-hooks-esm]'; |
| 8 | + |
| 9 | +if (!fs.existsSync(packageDistPath)) { |
| 10 | + console.log(`${logPrefix} Skip: @bitsocialhq/bitsocial-react-hooks dist not found.`); |
| 11 | + process.exit(0); |
| 12 | +} |
| 13 | + |
| 14 | +const relativeImportPattern = /(from\s+|import\s+)(['"])(\.\.?\/[^'"]+)\2/g; |
| 15 | +let touchedFiles = 0; |
| 16 | +let rewrittenImports = 0; |
| 17 | + |
| 18 | +const splitSpecifier = (specifier) => { |
| 19 | + const suffixStart = specifier.search(/[?#]/); |
| 20 | + |
| 21 | + if (suffixStart === -1) { |
| 22 | + return { bareSpecifier: specifier, suffix: '' }; |
| 23 | + } |
| 24 | + |
| 25 | + return { |
| 26 | + bareSpecifier: specifier.slice(0, suffixStart), |
| 27 | + suffix: specifier.slice(suffixStart), |
| 28 | + }; |
| 29 | +}; |
| 30 | + |
| 31 | +const resolveSpecifier = (filePath, specifier) => { |
| 32 | + const { bareSpecifier, suffix } = splitSpecifier(specifier); |
| 33 | + |
| 34 | + if (path.extname(bareSpecifier)) { |
| 35 | + return null; |
| 36 | + } |
| 37 | + |
| 38 | + const absoluteSpecifierPath = path.resolve(path.dirname(filePath), bareSpecifier); |
| 39 | + |
| 40 | + if (fs.existsSync(`${absoluteSpecifierPath}.js`)) { |
| 41 | + return `${bareSpecifier}.js${suffix}`; |
| 42 | + } |
| 43 | + |
| 44 | + if (fs.existsSync(path.join(absoluteSpecifierPath, 'index.js'))) { |
| 45 | + return `${bareSpecifier}/index.js${suffix}`; |
| 46 | + } |
| 47 | + |
| 48 | + return null; |
| 49 | +}; |
| 50 | + |
| 51 | +const patchFile = (filePath) => { |
| 52 | + const source = fs.readFileSync(filePath, 'utf8'); |
| 53 | + let fileImportCount = 0; |
| 54 | + |
| 55 | + const updated = source.replace(relativeImportPattern, (match, prefix, quote, specifier) => { |
| 56 | + const resolvedSpecifier = resolveSpecifier(filePath, specifier); |
| 57 | + |
| 58 | + if (!resolvedSpecifier || resolvedSpecifier === specifier) { |
| 59 | + return match; |
| 60 | + } |
| 61 | + |
| 62 | + fileImportCount += 1; |
| 63 | + return `${prefix}${quote}${resolvedSpecifier}${quote}`; |
| 64 | + }); |
| 65 | + |
| 66 | + if (!fileImportCount) { |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + fs.writeFileSync(filePath, updated, 'utf8'); |
| 71 | + touchedFiles += 1; |
| 72 | + rewrittenImports += fileImportCount; |
| 73 | +}; |
| 74 | + |
| 75 | +const walk = (currentPath) => { |
| 76 | + for (const entry of fs.readdirSync(currentPath, { withFileTypes: true })) { |
| 77 | + const entryPath = path.join(currentPath, entry.name); |
| 78 | + |
| 79 | + if (entry.isDirectory()) { |
| 80 | + walk(entryPath); |
| 81 | + continue; |
| 82 | + } |
| 83 | + |
| 84 | + if (entry.isFile() && entry.name.endsWith('.js')) { |
| 85 | + patchFile(entryPath); |
| 86 | + } |
| 87 | + } |
| 88 | +}; |
| 89 | + |
| 90 | +walk(packageDistPath); |
| 91 | + |
| 92 | +if (!touchedFiles) { |
| 93 | + console.log(`${logPrefix} No relative ESM imports needed patching.`); |
| 94 | + process.exit(0); |
| 95 | +} |
| 96 | + |
| 97 | +console.log(`${logPrefix} Patched ${rewrittenImports} imports across ${touchedFiles} files.`); |
0 commit comments