Files
oh-my-pi/packages/react-edit-benchmark/formatter.ts
T
can1357 1bebc8c6fd chore: expanded Biome configuration to include TypeScript files in package root directories
- Expanded Biome configuration to include TypeScript files in package root directories with new glob pattern `packages/*/*.ts`.
2026-02-11 08:12:09 +01:00

86 lines
2.2 KiB
TypeScript

/**
* Prettier formatting utilities for edit benchmarks.
*/
import * as fs from "node:fs/promises";
import * as path from "node:path";
import * as prettier from "prettier";
export const PRETTIER_OPTIONS: prettier.Options = {
printWidth: 100,
tabWidth: 2,
useTabs: false,
semi: true,
singleQuote: true,
quoteProps: "as-needed",
trailingComma: "all",
bracketSpacing: true,
arrowParens: "always",
endOfLine: "lf",
proseWrap: "preserve",
};
const parserByExtension: Partial<Record<string, prettier.BuiltInParserName>> = {
// Benchmark JS fixtures are Flow-typed; pin to flow to avoid parser-dependent formatting drift.
".js": "flow",
".jsx": "flow",
".ts": "typescript",
".tsx": "typescript",
".json": "json",
".jsonc": "json",
".md": "markdown",
".mdx": "mdx",
".yml": "yaml",
".yaml": "yaml",
".css": "css",
".scss": "scss",
".html": "html",
};
async function listFiles(rootDir: string, subPath = ""): Promise<string[]> {
const entries = await fs.readdir(path.join(rootDir, subPath), { withFileTypes: true });
const files: string[] = [];
for (const entry of entries) {
const relativePath = path.join(subPath, entry.name);
if (entry.isDirectory()) {
files.push(...(await listFiles(rootDir, relativePath)));
} else if (entry.isFile()) {
files.push(relativePath);
}
}
return files.sort();
}
export interface FormatResult {
formatted: string;
didFormat: boolean;
}
export async function formatContent(filePath: string, content: string): Promise<FormatResult> {
const parser = parserByExtension[path.extname(filePath).toLowerCase()];
if (!parser) {
return { formatted: content, didFormat: false };
}
try {
const formatted = await prettier.format(content, { ...PRETTIER_OPTIONS, parser });
return { formatted, didFormat: true };
} catch {
return { formatted: content, didFormat: false };
}
}
export async function formatDirectory(rootDir: string): Promise<void> {
const files = await listFiles(rootDir);
for (const file of files) {
const fullPath = path.join(rootDir, file);
const content = await Bun.file(fullPath).text();
const result = await formatContent(fullPath, content);
if (result.didFormat && result.formatted !== content) {
await Bun.write(fullPath, result.formatted);
}
}
}