UNPKG

@reliverse/rse

Version:

@reliverse/rse is your all-in-one companion for bootstrapping and improving any kind of projects (especially web apps built with frameworks like Next.js) — whether you're kicking off something new or upgrading an existing app. It is also a little AI-power

46 lines (45 loc) 1.55 kB
import path from "@reliverse/pathkit"; import fs from "@reliverse/relifso"; import { relinka } from "@reliverse/relinka"; import { glob } from "tinyglobby"; export async function convertTypeDefinitions(projectPath, targetStyle) { relinka( "info", `Converting type definitions to ${targetStyle} style in ${projectPath}` ); const files = await glob("**/*.{ts,tsx}", { cwd: path.resolve(projectPath) }); for (const file of files) { const filePath = path.join(projectPath, file); const content = await fs.readFile(filePath, "utf-8"); let updatedContent = content; if (targetStyle === "type") { updatedContent = content.replace( /interface\s+(\w+)(\s*extends\s*[^{]+)?\s*{([^}]*)}/g, (_match, name, extends_, body) => { const extendsClause = extends_ ? extends_.replace("extends", "&") : ""; return `type ${name} = ${extendsClause}{${body}}`; } ); } else { updatedContent = content.replace( /type\s+(\w+)\s*=\s*(?:{([^}]*)}|(\w+))/g, (match, name, objectBody, simpleType) => { if (simpleType) { return match; } return `interface ${name} {${objectBody}}`; } ); updatedContent = updatedContent.replace( /interface\s+(\w+)\s*{\s*}\s*&\s*(\w+)/g, "interface $1 extends $2" ); } if (content !== updatedContent) { await fs.writeFile(filePath, updatedContent, "utf-8"); relinka("info", `Updated type definitions in ${filePath}`); } } }