2024-02-10 15:37:38 +01:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
2024-02-17 21:09:19 +01:00
|
|
|
"bufio"
|
2024-02-10 15:37:38 +01:00
|
|
|
"fmt"
|
2024-02-17 21:09:19 +01:00
|
|
|
"os"
|
|
|
|
"strings"
|
2024-03-06 15:35:22 +01:00
|
|
|
"time"
|
2024-02-14 17:16:41 +01:00
|
|
|
|
2024-02-24 16:39:45 +01:00
|
|
|
"github.com/alecthomas/kong"
|
2024-02-16 19:29:43 +01:00
|
|
|
"github.com/facundoolano/jorge/config"
|
|
|
|
"github.com/facundoolano/jorge/site"
|
2024-02-10 15:37:38 +01:00
|
|
|
)
|
|
|
|
|
2024-02-27 16:30:00 +01:00
|
|
|
const FILE_RW_MODE = 0666
|
|
|
|
const DIR_RWE_MODE = 0777
|
2024-02-17 21:09:19 +01:00
|
|
|
|
2024-02-24 16:39:45 +01:00
|
|
|
type Build struct {
|
|
|
|
ProjectDir string `arg:"" name:"path" optional:"" default:"." help:"Path to the website project to build."`
|
2024-03-14 16:17:37 +01:00
|
|
|
NoMinify bool `help:"Disable file minifying."`
|
2024-02-21 16:56:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read the files in src/ render them and copy the result to target/
|
2024-02-24 16:39:45 +01:00
|
|
|
func (cmd *Build) Run(ctx *kong.Context) error {
|
2024-03-06 15:35:22 +01:00
|
|
|
start := time.Now()
|
|
|
|
|
2024-02-24 16:39:45 +01:00
|
|
|
config, err := config.Load(cmd.ProjectDir)
|
2024-02-21 16:56:22 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-03-14 16:17:37 +01:00
|
|
|
config.Minify = !cmd.NoMinify
|
2024-02-21 16:56:22 +01:00
|
|
|
|
2024-03-06 15:35:22 +01:00
|
|
|
err = site.Build(*config)
|
|
|
|
fmt.Printf("done in %.2fs\n", time.Since(start).Seconds())
|
|
|
|
return err
|
2024-02-21 16:56:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Prompt the user for a string value
|
|
|
|
func Prompt(label string) string {
|
|
|
|
// https://dev.to/tidalcloud/interactive-cli-prompts-in-go-3bj9
|
|
|
|
var s string
|
|
|
|
r := bufio.NewReader(os.Stdin)
|
|
|
|
for {
|
|
|
|
fmt.Fprint(os.Stderr, label+": ")
|
|
|
|
s, _ = r.ReadString('\n')
|
|
|
|
if s != "" {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return strings.TrimSpace(s)
|
|
|
|
}
|