UNPKG

@reliverse/rse-sdk

Version:

@reliverse/rse-sdk allows you to create new plugins for @reliverse/rse CLI, interact with reliverse.org, and even extend your own CLI functionality (you may also try @reliverse/dler-sdk for this case).

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}`); } } }