Files
GiteaMailTemplates/tools/cli/preview_cmd.go
T
KenanZhuandClaude Opus 4.8 0bf982ffb9 refactor(tools): modularize CLI with urfave/cli/v2, externalise template metadata
- Split monolithic build-preview.go into modular packages:
  tools.go (entry), cli/ (list/create/delete/preview commands),
  config/ (JSON loading), preview/ (rendering engine, funcs, locale)
- Extract all template metadata into data/templates_config.json
  as single source of truth — no hardcoded template data in Go
- Replace hand-rolled CLI parsing with github.com/urfave/cli/v2
- Add sensible defaults for --folder (../themes) and --config
  (./data/templates_config.json) — most commands now run bare
- Add type-coercing gt/lt/ge/le template funcs for JSON float64
- Fix AppUrl to gitea.com matching official template convention
- rendered.js now exports __REGISTRY__ and __PARAMS__ alongside
  __RENDERED__ — preview/index.html reads all from single source
- Cross-validate all 11 template data contexts against official
  Gitea source (services/mailer/*.go + templates/mail/*.tmpl)
- Update all docs (README, AGENTS, CONTRIBUTING, 5 languages)
  to reflect new CLI workflow with create scaffolding command

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-04 12:26:44 +08:00

86 lines
2.0 KiB
Go

package cli
import (
"fmt"
"log"
"path/filepath"
"github.com/urfave/cli/v2"
"gitea-mail-templates/tools/config"
"gitea-mail-templates/tools/preview"
)
// PreviewCommand returns the "preview" subcommand.
func PreviewCommand() *cli.Command {
return &cli.Command{
Name: "preview",
Usage: "Pre-render mail templates for preview/testing",
UsageText: `go run . preview [--folder <themes-dir>] [--config <config-file>] [--output <output.js>] all
go run . preview [--folder <themes-dir>] [--config <config-file>] <style-name>...`,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "folder",
Value: "../themes",
Usage: "Path to the themes directory",
},
&cli.StringFlag{
Name: "config",
Value: "./data/templates_config.json",
Usage: "Path to templates_config.json",
},
&cli.StringFlag{
Name: "output",
Usage: "Path to output rendered.js (default: derived from --folder)",
},
},
Action: runPreview,
}
}
func runPreview(c *cli.Context) error {
folder := c.String("folder")
configPath := c.String("config")
outputPath := c.String("output")
if outputPath == "" {
outputPath = filepath.Join(filepath.Dir(folder), "preview", "rendered.js")
}
names := c.Args().Slice()
if len(names) == 0 {
return fmt.Errorf("specify style names or 'all' (e.g. go run . preview all)")
}
cfg, err := config.Load(configPath)
if err != nil {
return err
}
var themeFilter map[string]bool
if len(names) == 1 && names[0] == "all" {
themeFilter = nil
} else {
themeFilter = make(map[string]bool)
for _, name := range names {
themeFilter[name] = true
}
}
result := preview.RenderAll(folder, cfg, themeFilter)
if result == nil {
return fmt.Errorf("no results produced")
}
if err := preview.WriteRenderedJS(result, outputPath); err != nil {
return err
}
preview.PrintDetailedSummary(result, folder, cfg)
log.Printf("Wrote %d themes x %d templates to %s",
len(result.Summaries), len(cfg.Templates), outputPath)
return nil
}