mirror of
https://github.com/FranP-code/create-better-t-stack.git
synced 2025-10-12 23:52:15 +00:00
feat(cli): add alchemy and improve cli tooling and structure (#520)
This commit is contained in:
61
apps/cli/src/utils/format-with-biome.ts
Normal file
61
apps/cli/src/utils/format-with-biome.ts
Normal file
@@ -0,0 +1,61 @@
|
||||
import path from "node:path";
|
||||
import { Biome } from "@biomejs/js-api/nodejs";
|
||||
import fs from "fs-extra";
|
||||
import { glob } from "tinyglobby";
|
||||
|
||||
export async function formatProjectWithBiome(projectDir: string) {
|
||||
const biome = new Biome();
|
||||
const { projectKey } = biome.openProject(projectDir);
|
||||
|
||||
biome.applyConfiguration(projectKey, {
|
||||
formatter: {
|
||||
enabled: true,
|
||||
indentStyle: "tab",
|
||||
},
|
||||
javascript: {
|
||||
formatter: {
|
||||
quoteStyle: "double",
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const files = await glob("**/*", {
|
||||
cwd: projectDir,
|
||||
dot: true,
|
||||
absolute: true,
|
||||
onlyFiles: true,
|
||||
});
|
||||
|
||||
for (const filePath of files) {
|
||||
try {
|
||||
const ext = path.extname(filePath).toLowerCase();
|
||||
const supported = new Set([
|
||||
".ts",
|
||||
".tsx",
|
||||
".js",
|
||||
".jsx",
|
||||
".cjs",
|
||||
".mjs",
|
||||
".cts",
|
||||
".mts",
|
||||
".json",
|
||||
".jsonc",
|
||||
".md",
|
||||
".mdx",
|
||||
".css",
|
||||
".scss",
|
||||
".html",
|
||||
]);
|
||||
if (!supported.has(ext)) continue;
|
||||
|
||||
const original = await fs.readFile(filePath, "utf8");
|
||||
const result = biome.formatContent(projectKey, original, { filePath });
|
||||
const content = result?.content;
|
||||
if (typeof content !== "string") continue;
|
||||
if (content.length === 0 && original.length > 0) continue;
|
||||
if (content !== original) {
|
||||
await fs.writeFile(filePath, content);
|
||||
}
|
||||
} catch {}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user