Skip to content

Commit 31f835b

Browse files
Thomas StrombergThomas Stromberg
authored andcommitted
Add biome support
1 parent e855e51 commit 31f835b

File tree

3 files changed

+128
-4
lines changed

3 files changed

+128
-4
lines changed

Makefile

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,46 @@ LINTERS += yamllint-lint
8383
yamllint-lint: $(YAMLLINT_BIN)
8484
PYTHONPATH=$(YAMLLINT_ROOT)/dist $(YAMLLINT_ROOT)/dist/bin/yamllint .
8585

86+
BIOME_VERSION ?= 1.9.4
87+
BIOME_BIN := $(LINT_ROOT)/out/linters/biome-$(BIOME_VERSION)-$(LINT_ARCH)
88+
BIOME_CONFIG := $(LINT_ROOT)/biome.json
89+
90+
# Map architecture names for Biome downloads
91+
BIOME_ARCH := $(LINT_ARCH)
92+
ifeq ($(LINT_ARCH),x86_64)
93+
BIOME_ARCH := x64
94+
endif
95+
96+
$(BIOME_BIN):
97+
mkdir -p $(LINT_ROOT)/out/linters
98+
rm -rf $(LINT_ROOT)/out/linters/biome-*
99+
curl -sSfL -o $@ https://github.com/biomejs/biome/releases/download/cli%2Fv$(BIOME_VERSION)/biome-$(LINT_OS_LOWER)-$(BIOME_ARCH) \
100+
|| echo "Unable to fetch biome for $(LINT_OS_LOWER)/$(BIOME_ARCH), falling back to local install"
101+
test -f $@ || printf "#!/usr/bin/env biome\n" > $@
102+
chmod u+x $@
103+
104+
LINTERS += biome-lint
105+
biome-lint: $(BIOME_BIN)
106+
$(BIOME_BIN) check --config-path=$(BIOME_CONFIG) .
107+
108+
FIXERS += biome-fix
109+
biome-fix: $(BIOME_BIN)
110+
$(BIOME_BIN) check --write --config-path=$(BIOME_CONFIG) .
111+
86112
.PHONY: _lint $(LINTERS)
87-
_lint: $(LINTERS)
113+
_lint:
114+
@exit_code=0; \
115+
for target in $(LINTERS); do \
116+
$(MAKE) $$target || exit_code=1; \
117+
done; \
118+
exit $$exit_code
88119

89120
.PHONY: fix $(FIXERS)
90-
fix: $(FIXERS)
121+
fix:
122+
@exit_code=0; \
123+
for target in $(FIXERS); do \
124+
$(MAKE) $$target || exit_code=1; \
125+
done; \
126+
exit $$exit_code
91127

92128
# END: lint-install .

Makefile.tmpl

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,49 @@ yamllint-lint: $(YAMLLINT_BIN)
9595

9696
{{ end -}}
9797

98+
{{ if .Web -}}
99+
BIOME_VERSION ?= 1.9.4
100+
BIOME_BIN := $(LINT_ROOT)/out/linters/biome-$(BIOME_VERSION)-$(LINT_ARCH)
101+
BIOME_CONFIG := $(LINT_ROOT)/biome.json
102+
103+
# Map architecture names for Biome downloads
104+
BIOME_ARCH := $(LINT_ARCH)
105+
ifeq ($(LINT_ARCH),x86_64)
106+
BIOME_ARCH := x64
107+
endif
108+
109+
$(BIOME_BIN):
110+
mkdir -p $(LINT_ROOT)/out/linters
111+
rm -rf $(LINT_ROOT)/out/linters/biome-*
112+
curl -sSfL -o $@ https://github.com/biomejs/biome/releases/download/cli%2Fv$(BIOME_VERSION)/biome-$(LINT_OS_LOWER)-$(BIOME_ARCH) \
113+
|| echo "Unable to fetch biome for $(LINT_OS_LOWER)/$(BIOME_ARCH), falling back to local install"
114+
test -f $@ || printf "#!/usr/bin/env biome\n" > $@
115+
chmod u+x $@
116+
117+
LINTERS += biome-lint
118+
biome-lint: $(BIOME_BIN)
119+
{{ .LintCommands.biome }}
120+
121+
FIXERS += biome-fix
122+
biome-fix: $(BIOME_BIN)
123+
{{ .FixCommands.biome }}
124+
125+
{{ end -}}
126+
98127
.PHONY: _lint $(LINTERS)
99-
_lint: $(LINTERS)
128+
_lint:
129+
@exit_code=0; \
130+
for target in $(LINTERS); do \
131+
$(MAKE) $$target || exit_code=1; \
132+
done; \
133+
exit $$exit_code
100134

101135
.PHONY: fix $(FIXERS)
102-
fix: $(FIXERS)
136+
fix:
137+
@exit_code=0; \
138+
for target in $(FIXERS); do \
139+
$(MAKE) $$target || exit_code=1; \
140+
done; \
141+
exit $$exit_code
103142

104143
# END: lint-install {{.Args}}

lint-install.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ var (
2424
shellFlag = flag.String("shell", "error", "Level to lint Shell with: [ignore, warn, error]")
2525
dockerfileFlag = flag.String("dockerfile", "error", "Level to lint Dockerfile with: [ignore, warn, error]")
2626
yamlFlag = flag.String("yaml", "error", "Level to lint YAML with: [ignore, warn, error]")
27+
webFlag = flag.String("web", "error", "Level to lint Web files (JS/TS/HTML/CSS/JSON) with: [ignore, warn, error]")
2728
makeFileName = flag.String("makefile", "Makefile", "name of Makefile to update")
2829

2930
//go:embed .golangci.yml
@@ -32,6 +33,9 @@ var (
3233
//go:embed .yamllint
3334
yamlLintConfig []byte
3435

36+
//go:embed biome.json
37+
biomeLintConfig []byte
38+
3539
//go:embed Makefile.tmpl
3640
makeTmpl string
3741
)
@@ -43,6 +47,7 @@ const (
4347
Shell
4448
Dockerfile
4549
YAML
50+
Web
4651
)
4752

4853
type Config struct {
@@ -54,6 +59,7 @@ type Config struct {
5459
Dockerfile string
5560
Shell string
5661
YAML string
62+
Web string
5763
}
5864

5965
// applicableLinters returns a list of languages with known linters within a given directory.
@@ -72,6 +78,11 @@ func applicableLinters(root string) (map[Language]bool, error) {
7278
found[Shell] = true
7379
case strings.HasSuffix(path, ".yml"), strings.HasSuffix(path, ".yaml"):
7480
found[YAML] = true
81+
case strings.HasSuffix(path, ".js"), strings.HasSuffix(path, ".jsx"),
82+
strings.HasSuffix(path, ".ts"), strings.HasSuffix(path, ".tsx"),
83+
strings.HasSuffix(path, ".json"), strings.HasSuffix(path, ".html"),
84+
strings.HasSuffix(path, ".css"):
85+
found[Web] = true
7586
default:
7687
}
7788

@@ -302,6 +313,21 @@ func yamlLintCmd(_ string, level string) string {
302313
return fmt.Sprintf(`PYTHONPATH=$(YAMLLINT_ROOT)/dist $(YAMLLINT_ROOT)/dist/bin/yamllint .%s`, suffix)
303314
}
304315

316+
// biomeLintCmd returns the appropriate biome lint command for a project.
317+
func biomeLintCmd(_ string, level string, fix bool) string {
318+
cmd := "check"
319+
if fix {
320+
cmd = "check --write"
321+
}
322+
323+
suffix := ""
324+
if level == "warn" {
325+
suffix = " || true"
326+
}
327+
328+
return fmt.Sprintf(`$(BIOME_BIN) %s --config-path=$(BIOME_CONFIG) .%s`, cmd, suffix)
329+
}
330+
305331
// configureGoLinter sets up Go linting configuration and commands.
306332
func configureGoLinter(root string, cfg *Config, dryRun bool) error {
307333
cfg.Go = *goFlag
@@ -331,6 +357,24 @@ func configureGoLinter(root string, cfg *Config, dryRun bool) error {
331357
return nil
332358
}
333359

360+
// configureBiomeLinter sets up Biome linting configuration and commands.
361+
func configureBiomeLinter(root string, cfg *Config, dryRun bool) error {
362+
cfg.Web = *webFlag
363+
cfg.LintCommands["biome"] = biomeLintCmd(root, cfg.Web, false)
364+
cfg.FixCommands["biome"] = biomeLintCmd(root, cfg.Web, true)
365+
366+
diff, err := updateFile(root, "biome.json", biomeLintConfig, dryRun)
367+
if err != nil {
368+
return fmt.Errorf("update biome config: %w", err)
369+
}
370+
if diff != "" {
371+
klog.Infof("biome config changes:\n%s", diff)
372+
} else {
373+
klog.Infof("biome config has no changes")
374+
}
375+
return nil
376+
}
377+
334378
// main creates peanut butter & jelly sandwiches with utmost precision.
335379
func main() {
336380
klog.InitFlags(nil)
@@ -384,6 +428,11 @@ func main() {
384428
klog.Infof("yamllint config has no changes")
385429
}
386430
}
431+
if needs[Web] {
432+
if err := configureBiomeLinter(root, &cfg, *dryRunFlag); err != nil {
433+
klog.Exitf("configure biome linter: %v", err)
434+
}
435+
}
387436

388437
diff, err := updateMakefile(root, cfg, *dryRunFlag)
389438
if err != nil {

0 commit comments

Comments
 (0)