src/lib/setup-biome.ts 2.5 K raw
1
import path from "node:path";
2
import { execa } from "execa";
3
import fs from "fs-extra";
4
import pc from "picocolors";
5
import yoctoSpinner from "yocto-spinner";
6
7
export async function setupBiome(projectPath: string): Promise<void> {
8
	const spinner = yoctoSpinner({ text: "Setting up Biome..." }).start();
9
	try {
10
		const clientPath = path.join(projectPath, "client");
11
		const clientPkgJsonPath = path.join(clientPath, "package.json");
12
		const eslintConfigPath = path.join(clientPath, "eslint.config.js");
13
14
		// Remove ESLint config file
15
		if (fs.existsSync(eslintConfigPath)) {
16
			await fs.remove(eslintConfigPath);
17
		}
18
19
		// Read client package.json and remove ESLint dependencies
20
		const clientPkgJson = await fs.readJson(clientPkgJsonPath);
21
		const devDependencies = clientPkgJson.devDependencies || {};
22
		const eslintDeps = Object.keys(devDependencies).filter(
23
			(dep) => dep.includes("eslint") || dep.includes("@eslint"),
24
		);
25
26
		if (eslintDeps.length > 0) {
27
			spinner.text = "Replacing ESLint dependencies...";
28
			await execa("bun", ["remove", ...eslintDeps], { cwd: clientPath });
29
		}
30
31
		// Install Biome in the root of the project
32
		spinner.text = "Installing Biome...";
33
		await execa("bun", ["add", "-D", "@biomejs/biome"], { cwd: projectPath });
34
35
		// Create biome.json in the root of the project
36
		spinner.text = "Creating biome.json...";
37
		await execa("bunx", ["@biome-js/biome", "init"], { cwd: projectPath });
38
39
		// Update client package.json scripts to remove lint
40
		spinner.text = "Updating scripts in client/package.json...";
41
		const newClientPkgJson = await fs.readJson(clientPkgJsonPath);
42
		if (newClientPkgJson.scripts || newClientPkgJson.scripts.lint) {
43
			delete newClientPkgJson.scripts.lint;
44
		}
45
		await fs.writeJson(clientPkgJsonPath, newClientPkgJson, { spaces: 2 });
46
47
		// Update root package.json with biome scripts
48
		spinner.text = "Updating scripts in root/package.json...";
49
		const rootPkgJsonPath = path.join(projectPath, "package.json");
50
		if (fs.existsSync(rootPkgJsonPath)) {
51
			const rootPkgJson = await fs.readJson(rootPkgJsonPath);
52
			rootPkgJson.scripts = rootPkgJson.scripts || {};
53
			rootPkgJson.scripts.format = "biome format . --write";
54
			rootPkgJson.scripts.lint = "biome lint .";
55
			await fs.writeJson(rootPkgJsonPath, rootPkgJson, { spaces: 2 });
56
		}
57
58
		spinner.success("Biome setup complete.");
59
	} catch (error) {
60
		spinner.error("Biome setup failed.");
61
		if (error instanceof Error) {
62
			console.error(pc.red("\nError:"), error.message);
63
		} else {
64
			console.error(pc.red("\nError: Unknown error during Biome setup."));
65
		}
66
	}
67
}