-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2592807
commit 68efb15
Showing
14 changed files
with
172 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
--- | ||
title: "Commands" | ||
--- | ||
|
||
# Commands | ||
|
||
## `build` | ||
|
||
Generates HTML files to the `dist` directory. | ||
|
||
``` | ||
malta build | ||
``` | ||
|
||
## `preview` | ||
|
||
Runs a preview server on localhost (port 3000) for the generated site. | ||
|
||
``` | ||
malta preview | ||
``` | ||
|
||
``` | ||
malta preview --port 5000 | ||
``` | ||
|
||
### Options | ||
|
||
- `--port` (`-p`): Localhost port (number - `3000` by default) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package main | ||
package build | ||
|
||
import ( | ||
"bytes" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package preview | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"os" | ||
"path" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
func PreviewCommand() int { | ||
port := 3000 | ||
args := parseArgs(os.Args[2:]) | ||
portArg, ok := args["p"] | ||
if !ok { | ||
portArg, ok = args["port"] | ||
} | ||
if ok { | ||
parsedPort, err := strconv.Atoi(portArg) | ||
if err != nil { | ||
fmt.Println("Invalid argument: 'port' must be a number") | ||
return 1 | ||
} | ||
port = parsedPort | ||
} | ||
|
||
http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) { | ||
html, err := os.ReadFile(path.Join("dist", req.URL.Path+".html")) | ||
if err != nil { | ||
html, err = os.ReadFile(path.Join("dist", req.URL.Path, "index.html")) | ||
if err != nil { | ||
data, err := os.ReadFile(path.Join("dist", req.URL.Path)) | ||
if err != nil { | ||
w.WriteHeader(404) | ||
w.Write([]byte("404 - Not found")) | ||
return | ||
} | ||
if strings.HasSuffix(req.URL.Path, ".css") { | ||
w.Header().Set("Content-Type", "text/css") | ||
} | ||
w.WriteHeader(200) | ||
w.Write(data) | ||
return | ||
} | ||
} | ||
w.Header().Set("Content-Type", "text/html") | ||
w.WriteHeader(200) | ||
w.Write(html) | ||
}) | ||
fmt.Printf("Starting server on port %v...\n", port) | ||
err := http.ListenAndServe(fmt.Sprintf(":%v", port), nil) | ||
fmt.Println(err) | ||
return 1 | ||
} | ||
|
||
func parseArgs(argList []string) map[string]string { | ||
args := make(map[string]string) | ||
for i := 0; i < len(argList); i++ { | ||
item := argList[i] | ||
if item == "" { | ||
continue | ||
} | ||
if !strings.HasPrefix(item, "-") { | ||
continue | ||
} | ||
item = strings.TrimLeft(item, "-") | ||
if strings.Contains(item, "=") { | ||
keyValue := strings.Split(item, "=") | ||
key := keyValue[0] | ||
args[key] = "" | ||
if len(keyValue) > 1 { | ||
args[key] = keyValue[1] | ||
} | ||
continue | ||
} | ||
if i+1 == len(argList) || strings.HasPrefix(argList[i+1], "-") { | ||
args[item] = "" | ||
continue | ||
} | ||
args[item] = argList[i+1] | ||
} | ||
return args | ||
} |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/pilcrowOnPaper/malta/commands/build" | ||
"github.com/pilcrowOnPaper/malta/commands/preview" | ||
) | ||
|
||
func main() { | ||
if len(os.Args) == 1 { | ||
fmt.Print(` | ||
Usage: | ||
malta build - build and generate HTML files | ||
malta preview - preview build | ||
`) | ||
os.Exit(0) | ||
} | ||
if os.Args[1] == "build" { | ||
os.Exit(build.BuildCommand()) | ||
} | ||
if os.Args[1] == "preview" { | ||
os.Exit(preview.PreviewCommand()) | ||
} | ||
fmt.Printf("Unknown command: %s\n", os.Args[1]) | ||
os.Exit(1) | ||
} |