Skip to content

Commit 468a59b

Browse files
authored
fix: upd biome formatter (#1195)
* fix: upd biome formatter * chore: add changeset
1 parent bbaa116 commit 468a59b

4 files changed

Lines changed: 54 additions & 21 deletions

File tree

.changeset/warm-toys-cover.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"lingo.dev": patch
3+
---
4+
5+
Fix Biome formatting

packages/cli/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,8 @@
130130
"@babel/parser": "^7.27.1",
131131
"@babel/traverse": "^7.27.4",
132132
"@babel/types": "^7.27.1",
133-
"@biomejs/js-api": "^0.6.2",
134-
"@biomejs/wasm-nodejs": "^1.8.3",
133+
"@biomejs/js-api": "^3.0.0",
134+
"@biomejs/wasm-nodejs": "^2.2.4",
135135
"@datocms/cma-client-node": "^4.0.1",
136136
"@gitbeaker/rest": "^39.34.3",
137137
"@inkjs/ui": "^2.0.0",

packages/cli/src/cli/loaders/formatters/biome.ts

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ export type BiomeLoaderOptions = {
1010
alwaysFormat?: boolean;
1111
};
1212

13+
// Track warnings shown per file extension to avoid spam
14+
const shownWarnings = new Set<string>();
15+
1316
export default function createBiomeLoader(
1417
options: BiomeLoaderOptions,
1518
): ILoader<string, string> {
@@ -46,32 +49,57 @@ async function formatDataWithBiome(
4649
filePath: string,
4750
options: BiomeLoaderOptions,
4851
): Promise<string> {
52+
let configPath: string | null = null;
53+
4954
try {
5055
const biome = await Biome.create({
5156
distribution: Distribution.NODE,
5257
});
5358

59+
// Open a project (required in v3.0.0+)
60+
const openResult = biome.openProject(".");
61+
const projectKey = openResult.projectKey;
62+
5463
// Load config from biome.json/biome.jsonc if exists
55-
const configPath = await findBiomeConfig(filePath);
64+
configPath = await findBiomeConfig(filePath);
5665
if (!configPath && !options.alwaysFormat) {
5766
return data; // Skip if no config and not forced
5867
}
5968

6069
if (configPath) {
6170
const configContent = await fs.readFile(configPath, "utf-8");
62-
biome.applyConfiguration(JSON.parse(configContent));
71+
try {
72+
const config = JSON.parse(configContent);
73+
biome.applyConfiguration(projectKey, config);
74+
} catch (parseError) {
75+
throw new Error(
76+
`Invalid Biome configuration in ${configPath}: ${parseError instanceof Error ? parseError.message : "JSON parse error"}`,
77+
);
78+
}
6379
}
6480

65-
const formatted = biome.formatContent(data, {
81+
const formatted = biome.formatContent(projectKey, data, {
6682
filePath,
6783
});
6884

6985
return formatted.content;
7086
} catch (error) {
71-
if (error instanceof Error) {
87+
const ext = path.extname(filePath);
88+
89+
// Only show warning once per file extension
90+
if (!shownWarnings.has(ext)) {
91+
shownWarnings.add(ext);
92+
7293
console.log();
73-
console.log("⚠️ Biome formatting failed:", error.message);
94+
console.log(
95+
`⚠️ Biome does not support ${ext} files - skipping formatting`,
96+
);
97+
98+
if (error instanceof Error && error.message) {
99+
console.log(` ${error.message}`);
100+
}
74101
}
102+
75103
return data; // Fallback to unformatted
76104
}
77105
}

pnpm-lock.yaml

Lines changed: 14 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)