-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
267 lines (231 loc) · 8.4 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package main
import (
"encoding/json"
"fmt"
"os"
"os/exec"
"strconv"
"strings"
"github.com/charmbracelet/huh"
"github.com/charmbracelet/huh/spinner"
)
type PackageManager string
const (
Npm PackageManager = "npm"
Pnpm PackageManager = "pnpm"
Yarn PackageManager = "yarn"
Bun PackageManager = "bun"
)
type EslintMigrationStatus string
const (
EslintNotMigrated EslintMigrationStatus = "not migrated"
EslintMigrated EslintMigrationStatus = "migrated"
EslintMigratedWithRules EslintMigrationStatus = "migrated with inspired rules"
)
type BiomeConfig struct {
PackageManager PackageManager
InitBiome bool
IntegrateVCS bool
MigrateEslint EslintMigrationStatus
MigratePrettier bool
Monorepo bool
}
type BiomeJSON struct {
Schema *string `json:"$schema,omitempty"`
OrganizeImports *Formatter `json:"organizeImports,omitempty"`
Vcs *Vcs `json:"vcs,omitempty"`
Linter *Linter `json:"linter,omitempty"`
Formatter *Formatter `json:"formatter,omitempty"`
}
type Formatter struct {
Enabled bool `json:"enabled,omitempty"`
}
type Linter struct {
Enabled bool `json:"enabled,omitempty"`
Rules Rules `json:"rules,omitempty"`
}
type Rules struct {
Recommended bool `json:"recommended,omitempty"`
}
type Vcs struct {
Enabled bool `json:"enabled,omitempty"`
ClientKind string `json:"clientKind,omitempty"`
UseIgnoreFile bool `json:"useIgnoreFile,omitempty"`
DefaultBranch string `json:"defaultBranch,omitempty"`
}
func runCommandWithSpinner(s *spinner.Spinner, cmd *exec.Cmd, title, errMsg string) {
fmt.Printf("Running command: %s\n", strings.Join(cmd.Args, " "))
_ = s.Title(title).Action(func() {
output, err := cmd.CombinedOutput()
if err != nil {
fmt.Printf("%s: %s\n%s\n", errMsg, err.Error(), string(output))
os.Exit(1)
}
}).Run()
}
func runEslintMigrateCommand(config BiomeConfig, accessible bool) {
var eslintCmd *exec.Cmd
if config.MigrateEslint != EslintNotMigrated {
switch config.PackageManager {
case Npm:
if config.MigrateEslint == EslintMigratedWithRules {
eslintCmd = exec.Command("npx", "@biomejs/biome", "migrate", "eslint", "--write", "--include-inspired")
} else {
eslintCmd = exec.Command("npx", "@biomejs/biome", "migrate", "eslint", "--write")
}
case Pnpm:
if config.MigrateEslint == EslintMigratedWithRules {
eslintCmd = exec.Command("pnpm", "biome", "migrate", "eslint", "--write", "--include-inspired")
} else {
eslintCmd = exec.Command("pnpm", "biome", "migrate", "eslint", "--write")
}
case Yarn:
if config.MigrateEslint == EslintMigratedWithRules {
eslintCmd = exec.Command("yarn", "biome", "migrate", "eslint", "--write", "--include-inspired")
} else {
eslintCmd = exec.Command("yarn", "biome", "migrate", "eslint", "--write")
}
case Bun:
if config.MigrateEslint == EslintMigratedWithRules {
eslintCmd = exec.Command("bunx", "@biomejs/biome", "migrate", "eslint", "--write", "--include-inspired")
} else {
eslintCmd = exec.Command("bunx", "@biomejs/biome", "migrate", "eslint", "--write")
}
}
runCommandWithSpinner(spinner.New().Accessible(accessible), eslintCmd, "Migrating Eslint...", "Error migrating Eslint")
}
}
func runPrettierMigrateCommand(config BiomeConfig, accessible bool) {
var prettierCmd *exec.Cmd
if config.MigratePrettier {
switch config.PackageManager {
case Npm:
prettierCmd = exec.Command("npx", "@biomejs/biome", "migrate", "prettier", "--write")
case Pnpm:
prettierCmd = exec.Command("pnpm", "biome", "migrate", "prettier", "--write")
case Yarn:
prettierCmd = exec.Command("yarn", "biome", "migrate", "prettier", "--write")
case Bun:
prettierCmd = exec.Command("bunx", "@biomejs/biome", "migrate", "prettier", "--write")
}
// TODO: Only JSON configurations are supported. Need to warn user before running the migration or convert the Prettier configuration to JSON.
runCommandWithSpinner(spinner.New().Accessible(accessible), prettierCmd, "Migrating Prettier...", "Error migrating Prettier")
}
}
func configureVersionControl() {
// Read biome.json file
biomeJsonData, readError := os.ReadFile("biome.json")
if readError != nil {
fmt.Println("Error reading biome.json:", readError)
os.Exit(1)
}
// Unmarshal JSON data
var biomeConfigJson BiomeJSON
parseError := json.Unmarshal(biomeJsonData, &biomeConfigJson)
if parseError != nil {
fmt.Println("Error parsing biome.json:", parseError)
os.Exit(1)
}
// Append the VCS configuration
biomeConfigJson.Vcs = &Vcs{
Enabled: bool(true),
ClientKind: "git",
UseIgnoreFile: bool(true),
DefaultBranch: "main",
}
// Marshal the updated configuration
updatedBiomeJsonData, marshalError := json.MarshalIndent(biomeConfigJson, "", " ")
if marshalError != nil {
fmt.Println("Error generating updated biome.json:", marshalError)
os.Exit(1)
}
// Write the updated configuration back to biome.json
writeError := os.WriteFile("biome.json", updatedBiomeJsonData, 0644)
if writeError != nil {
fmt.Println("Error writing updated biome.json:", writeError)
os.Exit(1)
}
}
func main() {
var config BiomeConfig
// Should we run in accessible mode?
accessible, _ := strconv.ParseBool(os.Getenv("ACCESSIBLE"))
form := huh.NewForm(
huh.NewGroup(
huh.NewSelect[PackageManager]().
Options(huh.NewOptions(Npm, Pnpm, Yarn, Bun)...).
Title("Choose your package manager").
Value(&config.PackageManager),
huh.NewConfirm().
Title("Is this a monorepo?").
Value(&config.Monorepo),
huh.NewConfirm().
Title("Initialize Biome?").
Value(&config.InitBiome),
huh.NewConfirm().
Title("Integrate with Version Control System?").
Value(&config.IntegrateVCS),
huh.NewSelect[EslintMigrationStatus]().
Title("Migrate ESlint?").
Options(
huh.NewOptions(EslintNotMigrated, EslintMigrated, EslintMigratedWithRules)...,
).
Value(&config.MigrateEslint),
huh.NewConfirm().
Title("Migrate Prettier?").
Value(&config.MigratePrettier),
),
)
err := form.Run()
if err != nil {
fmt.Println("Uh oh:", err)
os.Exit(1)
}
if config.InitBiome {
var installCmd *exec.Cmd
var initCmd *exec.Cmd
switch config.PackageManager {
case Npm:
if config.Monorepo {
installCmd = exec.Command("npm", "install", "-W", "--save-dev", "--save-exact", "@biomejs/biome")
} else {
installCmd = exec.Command("npm", "install", "--save-dev", "--save-exact", "@biomejs/biome")
}
initCmd = exec.Command("npx", "@biomejs/biome", "init")
case Pnpm:
if config.Monorepo {
installCmd = exec.Command("pnpm", "add", "-r", "--save-dev", "--save-exact", "@biomejs/biome")
} else {
installCmd = exec.Command("pnpm", "add", "--save-dev", "--save-exact", "@biomejs/biome")
}
initCmd = exec.Command("pnpm", "biome", "init")
case Yarn:
if config.Monorepo {
installCmd = exec.Command("yarn", "add", "-W", "--dev", "--exact", "@biomejs/biome")
} else {
installCmd = exec.Command("yarn", "add", "--dev", "--exact", "@biomejs/biome")
}
initCmd = exec.Command("yarn", "biome", "init")
case Bun:
installCmd = exec.Command("bun", "add", "--dev", "--exact", "@biomejs/biome")
initCmd = exec.Command("bunx", "@biomejs/biome", "init")
}
runCommandWithSpinner(spinner.New().Accessible(accessible), installCmd, "Installing Biome...", "Error installing Biome")
runCommandWithSpinner(spinner.New().Accessible(accessible), initCmd, "Initializing Biome...", "Error initializing Biome")
}
if config.IntegrateVCS {
configureVersionControl()
}
runEslintMigrateCommand(config, accessible)
runPrettierMigrateCommand(config, accessible)
fmt.Println("\nBiome setup is now complete. For more information, please visit:")
fmt.Println("\t- Get started: https://biomejs.dev/guides/getting-started/")
fmt.Println("\t- Migrate Eslint and Prettier: https://biomejs.dev/guides/migrate-eslint-prettier/")
fmt.Println("\nYou should check these recipes:")
fmt.Println("\t- Continuous Integration: https://biomejs.dev/recipes/continuous-integration/")
fmt.Println("\t- Git Hooks: https://biomejs.dev/recipes/git-hooks/")
fmt.Println("\nIf you encounter any issues, please open issue them at:")
fmt.Println("\t- https://github.com/jellydn/biome-interactive")
fmt.Println("\nContributions are welcome! If you would like to improve the project, feel free to open a pull request.")
fmt.Println("\nIt's time to remove EsLint and Prettier from your devDependencies and its config files. Enjoy using Biome!")
}