Files
HiveGrid-Wireframes/hivegrid-cloud/scripts/swap-vendor-icon-imports.mjs
T

79 lines
2.7 KiB
JavaScript

// Repoints every import from a vendor icon library (lucide-react,
// @radix-ui/react-icons, @heroicons/react/24/outline) to the generated Subframe
// Feather compatibility shim at ~/components/ui/icons/vendor.
//
// Named specifiers (incl. `type X` and `Foo as Bar` aliases) are preserved; when
// a file imports from more than one vendor source they are merged into a single
// import statement to avoid duplicate-import lint errors.
//
// Run AFTER scripts/migrate-vendor-icons-to-feather.mjs, from repo root:
// node scripts/swap-vendor-icon-imports.mjs
import { readFileSync, writeFileSync, readdirSync, statSync } from "node:fs"
import { join, dirname } from "node:path"
import { fileURLToPath } from "node:url"
const __dirname = dirname(fileURLToPath(import.meta.url))
const repoRoot = join(__dirname, "..")
const appRoot = join(repoRoot, "apps/web/app")
const VENDOR_SOURCES = ["lucide-react", "@radix-ui/react-icons", "@heroicons/react/24/outline"]
const SHIM = "~/components/ui/icons/vendor"
function walk(dir, acc = []) {
for (const entry of readdirSync(dir)) {
if (entry === "node_modules" || entry.startsWith(".")) continue
const full = join(dir, entry)
const st = statSync(full)
if (st.isDirectory()) walk(full, acc)
else if (/\.(tsx?|jsx?)$/.test(entry)) acc.push(full)
}
return acc
}
// matches a single-line named import from one of the vendor sources
const importLineRe = new RegExp(
`^[ \\t]*import\\s*(?:type\\s+)?\\{([^}]*)\\}\\s*from\\s*["'](?:${VENDOR_SOURCES.map((s) => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")).join("|")})["'];?[ \\t]*\\r?\\n`,
"gm"
)
let filesChanged = 0
let importsRewritten = 0
for (const file of walk(appRoot)) {
const src = readFileSync(file, "utf8")
const matches = [...src.matchAll(importLineRe)]
if (matches.length === 0) continue
// collect specifiers across all vendor imports in this file, de-duplicated
const specs = []
const seen = new Set()
for (const m of matches) {
for (const raw of m[1].split(",")) {
const spec = raw.trim().replace(/\s+/g, " ")
if (!spec) continue
if (!seen.has(spec)) {
seen.add(spec)
specs.push(spec)
}
}
}
const merged = `import { ${specs.join(", ")} } from "${SHIM}";\n`
// replace the first vendor import with the merged statement, drop the rest
let replacedFirst = false
const out = src.replace(importLineRe, () => {
if (!replacedFirst) {
replacedFirst = true
return merged
}
return ""
})
writeFileSync(file, out, "utf8")
filesChanged++
importsRewritten += matches.length
}
console.log(`Repointed vendor icon imports in ${filesChanged} files (${importsRewritten} import statements) -> ${SHIM}`)