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

37 lines (36 loc) 1.33 kB
import path from "@reliverse/pathkit"; import fs from "@reliverse/relifso"; import { relinka } from "@reliverse/relinka"; import { glob } from "tinyglobby"; export async function convertImportStyle(projectPath, targetStyle) { relinka("info", `Converting to ${targetStyle} style in ${projectPath}`); const files = await glob("**/*.{js,jsx,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 === "import") { updatedContent = content.replace( /(?:const|let|var)\s*{\s*([^}]+)}\s*=\s*require\(['"]([^'"]+)['"]\)/g, 'import { $1 } from "$2"' ).replace( /(?:const|let|var)\s+(\w+)\s*=\s*require\(['"]([^'"]+)['"]\)/g, 'import $1 from "$2"' ); } else { updatedContent = content.replace( /import\s*{\s*([^}]+)}\s*from\s*['"]([^'"]+)['"]/g, "const { $1 } = require('$2')" ).replace( /import\s+(\w+)\s+from\s+['"]([^'"]+)['"]/g, "const $1 = require('$2')" ); } if (content !== updatedContent) { await fs.writeFile(filePath, updatedContent, "utf-8"); relinka("info", `Updated import style in ${filePath}`); } } }