fast and minimal static site generator
ssg

Add pre-build and post-build commands

+88 -7
+27 -1
commands/build.go
··· 229 229 // Core builder function. Converts markdown to html, 230 230 // copies over non .md files, etc. 231 231 func Build() error { 232 + if err := preBuild(); err != nil { 233 + return err 234 + } 232 235 fmt.Print("vite: building... ") 233 236 pages := Pages{} 234 237 if err := pages.initPages(); err != nil { ··· 286 289 if err := util.CopyDir(StaticDir, buildStatic); err != nil { 287 290 return err 288 291 } 292 + fmt.Print("done\n") 289 293 290 - fmt.Print("done\n") 294 + if err := postBuild(); err != nil { 295 + return err 296 + } 297 + return nil 298 + } 299 + 300 + func postBuild() error { 301 + for _, cmd := range config.Config.PostBuild { 302 + fmt.Println("vite: running post-build command:", cmd) 303 + if err := util.RunCmd(cmd); err != nil { 304 + return err 305 + } 306 + } 307 + return nil 308 + } 309 + 310 + func preBuild() error { 311 + for _, cmd := range config.Config.PreBuild { 312 + fmt.Println("vite: running pre-build command:", cmd) 313 + if err := util.RunCmd(cmd); err != nil { 314 + return err 315 + } 316 + } 291 317 return nil 292 318 }
+34 -4
config/config.go
··· 20 20 type ConfigYaml struct { 21 21 Title string `yaml:"title"` 22 22 Desc string `yaml:"description"` 23 - DefaultTemplate string `yaml:"default-template"` 23 + DefaultTemplate string `yaml:"-"` 24 24 Author struct { 25 25 Name string `yaml:"name"` 26 26 Email string `yaml:"email"` 27 27 } `yaml:"author"` 28 - URL string `yaml:"url"` 29 - // Prebuild []string `yaml:"prebuild"` 30 - // Postbuild []string `yaml:"postbuild"` 28 + URL string `yaml:"url"` 29 + PreBuild []string `yaml:"preBuild"` 30 + PostBuild []string `yaml:"postBuild"` 31 + } 32 + 33 + // For backward compat with `default-template` 34 + func (c *ConfigYaml) UnmarshalYAML(value *yaml.Node) error { 35 + type Alias ConfigYaml // Create an alias to avoid recursion 36 + 37 + var aux Alias 38 + 39 + if err := value.Decode(&aux); err != nil { 40 + return err 41 + } 42 + 43 + // Handle the DefaultTemplate field 44 + var temp struct { 45 + DefaultTemplate1 string `yaml:"default-template"` 46 + DefaultTemplate2 string `yaml:"defaultTemplate"` 47 + } 48 + if err := value.Decode(&temp); err != nil { 49 + return err 50 + } 51 + 52 + if temp.DefaultTemplate1 != "" { 53 + aux.DefaultTemplate = temp.DefaultTemplate1 54 + } else { 55 + aux.DefaultTemplate = temp.DefaultTemplate2 56 + } 57 + 58 + *c = ConfigYaml(aux) // Assign the unmarshalled values back to the original struct 59 + 60 + return nil 31 61 } 32 62 33 63 func (c *ConfigYaml) parseConfig() error {
+4 -2
readme
··· 29 29 type ConfigYaml struct { 30 30 Title string `yaml:"title"` 31 31 Desc string `yaml:"description"` 32 - DefaultTemplate string `yaml:"default-template"` 32 + DefaultTemplate string `yaml:"-"` 33 33 Author struct { 34 34 Name string `yaml:"name"` 35 35 Email string `yaml:"email"` 36 36 } `yaml:"author"` 37 - URL string `yaml:"url"` 37 + URL string `yaml:"url"` 38 + PreBuild []string `yaml:"preBuild"` 39 + PostBuild []string `yaml:"postBuild"` 38 40 } 39 41 40 42 Example config: https://git.icyphox.sh/site/tree/config.yaml
+23
util/run.go
··· 1 + package util 2 + 3 + import ( 4 + "fmt" 5 + "os/exec" 6 + "strings" 7 + ) 8 + 9 + func RunCmd(cmd string, args ...string) error { 10 + // Split the command into the executable and its arguments 11 + parts := strings.Fields(cmd) 12 + if len(parts) == 0 { 13 + return fmt.Errorf("error: is there an empty command?") 14 + } 15 + 16 + execCmd := exec.Command(parts[0], parts[1:]...) 17 + 18 + output, err := execCmd.CombinedOutput() 19 + if err != nil { 20 + return fmt.Errorf("error: command %q failed with %v: %s", cmd, err, output) 21 + } 22 + return nil 23 + }