@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
38 lines (37 loc) • 1.34 kB
JavaScript
import path from "@reliverse/pathkit";
import fs from "@reliverse/relifso";
import { relinka } from "@reliverse/relinka";
import { glob } from "tinyglobby";
export async function convertQuoteStyle(projectPath, targetQuoteStyle) {
relinka(
"info",
`Converting quotes to ${targetQuoteStyle} quotes 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");
const updatedContent = content.replace(
/(['"])((?:\\\1|(?!\1).)*?)\1/g,
(_match, _quote, content2) => {
const targetQuote = targetQuoteStyle === "single" ? "'" : '"';
const escapedContent = content2.replace(
new RegExp(targetQuote, "g"),
`\\${targetQuote}`
);
const otherQuote = targetQuoteStyle === "single" ? '"' : "'";
const unescapedContent = escapedContent.replace(
new RegExp(`\\\\${otherQuote}`, "g"),
otherQuote
);
return `${targetQuote}${unescapedContent}${targetQuote}`;
}
);
if (content !== updatedContent) {
await fs.writeFile(filePath, updatedContent, "utf-8");
relinka("info", `Updated quotes in ${filePath}`);
}
}
}