check-safety/check_presets.go
Alex Dunmow cd88c808b0 initial: standalone check-safety module hoisted from CMS
Static safety/lint runner for the BlockNinja codebase. ~25 invariant
checks across Go and frontend sources. Was at git.dev.alexdunmow.com:block/ninja
in backend/cmd/check-safety/ until the 2026-06-06 consolidation moved
the BlockNinja repos under a shared ~/src/blockninja/ parent.

This repo is the standalone extraction:
- Own go.mod (git.dev.alexdunmow.com/block/check-safety, go 1.26.4)
- Vendored internal/{helpers,theme} from CMS (Go's internal/ rule
  blocks cross-module imports; vendoring is the workaround)
- CLI contract unchanged: `check-safety <target-dir> [--flags]`
- CMS Makefile shells into ../check-safety for safety-check /
  install-safety-checker targets

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-06-06 13:04:02 +08:00

70 lines
2.1 KiB
Go

package main
import (
"fmt"
"os"
"path/filepath"
)
func init() {
register(Check{
Seq: 210,
ID: "21",
Title: "Plugin presets.json validation",
Run: func(ctx *ScanContext, rep *Reporter) {
// Check 21: Plugin presets.json validation
fmt.Println("\n=== Check 21: Plugin presets.json validation ===")
var presetViolations []presetViolation
presetsChecked := 0
// Check internal plugins
if ctx.includeCoreTargets {
internalPluginsDir := filepath.Join(ctx.backendDir, "internal", "plugins")
if dirExists(internalPluginsDir) {
entries, _ := os.ReadDir(internalPluginsDir)
for _, entry := range entries {
if !entry.IsDir() {
continue
}
pluginDir := filepath.Join(internalPluginsDir, entry.Name())
if !fileExists(filepath.Join(pluginDir, "presets.json")) {
continue
}
presetsChecked++
for _, v := range checkPresets(pluginDir) {
v.file = prefixDisplayPath("plugins/"+entry.Name(), v.file)
presetViolations = append(presetViolations, v)
}
}
}
}
// Check external plugin roots
for _, target := range ctx.resolvedPluginTargets {
vs := checkPresets(target.root)
if len(vs) == 0 && len(findPresetsFiles(target.root)) > 0 {
presetsChecked++
}
for _, v := range vs {
presetsChecked++
v.file = prefixDisplayPath(target.display, v.file)
presetViolations = append(presetViolations, v)
}
}
if len(presetViolations) > 0 {
fmt.Printf(" FAIL: %d presets.json validation error(s):\n", len(presetViolations))
for _, v := range presetViolations {
fmt.Printf(" %s — %s\n", v.file, v.message)
}
fmt.Println("\n Fix: Ensure presets.json matches the theme.Theme struct types.")
fmt.Println(" Common issues: numeric values where strings are expected (e.g., fontWeightBase: 400 → \"400\")")
rep.Fail()
} else if presetsChecked > 0 {
fmt.Printf(" OK: All presets.json files are valid (%d checked)\n", presetsChecked)
} else {
fmt.Println(" OK: No presets.json files found in scanned targets")
}
fmt.Println()
},
})
}