fes

Free Easy Site
Log | Files | Refs | README | LICENSE

new.go (2723B)


      1 package new
      2 
      3 import (
      4 	"fes/modules/config"
      5 	"fes/modules/ui"
      6 	"fmt"
      7 	"os"
      8 	"os/exec"
      9 	"os/user"
     10 	"path/filepath"
     11 	"strings"
     12 )
     13 
     14 /* try to get git user, if not system user */
     15 func getName() string {
     16 	out, err := exec.Command("git", "config", "user.name").Output()
     17 	if err == nil {
     18 		s := strings.TrimSpace(string(out))
     19 		if s != "" {
     20 			return s
     21 		}
     22 	}
     23 	u, err := user.Current()
     24 	if err == nil && u.Username != "" {
     25 		return u.Username
     26 	}
     27 	return "unknown"
     28 }
     29 
     30 /* helper function for writing files */
     31 func write(path string, format string, args ...any) error {
     32 	dir := filepath.Dir(path)
     33 	if err := os.MkdirAll(dir, 0755); err != nil {
     34 		panic(err)
     35 	}
     36 	f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
     37 	if err != nil {
     38 		panic(err)
     39 	}
     40 	defer f.Close()
     41 	_, err = fmt.Fprintf(f, format, args...)
     42 	return err
     43 }
     44 
     45 /* creates a hello world project */
     46 func Project(dir string) error {
     47 	if err := os.Mkdir(dir, 0755); err != nil {
     48 		return err
     49 	}
     50 	if err := os.Chdir(dir); err != nil {
     51 		return err
     52 	}
     53 
     54 	if *config.Docker {
     55 		write("docker-compose.yml", `services:
     56   %s:
     57     image: git.vxserver.dev/fsd/fes:latest
     58     ports:
     59       - "3000:3000"
     60     volumes:
     61       - ./app:/app`, dir)
     62 		if err := os.Mkdir("app", 0755); err != nil {
     63 			return err
     64 		}
     65 		if err := os.Chdir("app"); err != nil {
     66 			return err
     67 		}
     68 	}
     69 
     70 	name := getName()
     71 
     72 	write("www/index.lua", `local fes = require("fes")
     73 local site = fes.fes()
     74 
     75 -- site.copyright = fes.util.copyright("https://example.com", "%s")
     76 
     77 site:h1("Hello, World!")
     78 
     79 return site`, name)
     80 	write("Fes.toml", `[app]
     81 
     82 name = "%s"
     83 version = "0.0.1"
     84 authors = ["%s"]`, dir, name)
     85 	write("README.md", strings.ReplaceAll(`# %s
     86 
     87 $$$$$$
     88 fes new %s
     89 $$$$$$
     90 
     91 > **Know what you are doing?** Delete this file. Have fun!
     92 
     93 ## Project Structure
     94 
     95 Inside your Fes project, you'll see the following directories and files:
     96 
     97 $$$$$$
     98 .
     99 ├── Fes.toml
    100 ├── README.md
    101 └── www
    102     └── index.lua
    103 $$$$$$
    104 
    105 Fes looks for $$.lua$$ files in the $$www/$$ directory. Each file is exposed as a route based on its file name.
    106 
    107 ## Commands
    108 
    109 All commands are run from the root of the project, from a terminal:
    110 
    111 | Command                   | Action                                           |
    112 | :------------------------ | :----------------------------------------------- |
    113 | $$fes run .$$               | Runs the project at $$.$$                          |
    114 
    115 ## What to learn more?
    116 
    117 Check out [Fes's docs](https://docs.vxserver.dev/static/fes.html).`, "$$", "`"), dir, dir)
    118 
    119 	ui.Hint("you can run this with `fes run %s`", dir)
    120 
    121 	fmt.Println("Created new Fes project at", func () string {
    122 		if cwd, err := os.Getwd(); err != nil {
    123 			return dir
    124 		} else {
    125 			return cwd
    126 		}
    127 	}())
    128 
    129 	return nil
    130 }