-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
119 lines (106 loc) · 2.86 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package main
import (
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/evanw/esbuild/pkg/api"
"github.com/evanw/esbuild/pkg/cli"
"github.com/osdevisnot/sorvor/pkg/logger"
"github.com/osdevisnot/sorvor/pkg/pkgjson"
"github.com/osdevisnot/sorvor/pkg/sorvor"
)
var version = "development"
func readOptions(pkgJSON *pkgjson.PkgJSON) *sorvor.Sorvor {
var err error
var esbuildArgs []string
osArgs := os.Args[1:]
serv := &sorvor.Sorvor{}
hasOutDir := false
hasLogLevel := false
for _, arg := range osArgs {
switch {
case strings.HasPrefix(arg, "--version"):
logger.Info("sørvør version", version)
os.Exit(0)
case strings.HasPrefix(arg, "--outdir"):
hasOutDir = true
esbuildArgs = append(esbuildArgs, arg)
case strings.HasPrefix(arg, "--log-level"):
hasLogLevel = true
esbuildArgs = append(esbuildArgs, arg)
case strings.HasPrefix(arg, "--host"):
serv.Host = arg[len("--host="):]
case strings.HasPrefix(arg, "--port"):
port, err := strconv.Atoi(arg[len("--port="):])
logger.Fatal(err, "Invalid port value")
serv.Port = ":" + strconv.Itoa(port)
case arg == "--serve":
serv.Serve = true
case arg == "--secure":
serv.Secure = true
case !strings.HasPrefix(arg, "--"):
serv.Entry = arg
default:
esbuildArgs = append(esbuildArgs, arg)
}
}
esbuildArgs = append(esbuildArgs, "--bundle")
if !hasOutDir {
esbuildArgs = append(esbuildArgs, "--outdir=dist")
}
if !hasLogLevel {
esbuildArgs = append(esbuildArgs, "--log-level=warning")
}
serv.BuildOptions, err = cli.ParseBuildOptions(esbuildArgs)
logger.Fatal(err, "Invalid option for esbuild")
serv.BuildOptions.Write = true
if serv.Serve == true {
if serv.Port == "" {
serv.Port = ":1234"
}
serv.BuildOptions.Define = map[string]string{"process.env.NODE_ENV": "'development'"}
} else {
serv.BuildOptions.Define = map[string]string{"process.env.NODE_ENV": "'production'"}
}
if serv.BuildOptions.Format == api.FormatDefault {
serv.BuildOptions.Format = api.FormatESModule
}
if serv.Entry == "" {
serv.Entry = "public/index.html"
}
if serv.Host == "" {
serv.Host = "localhost"
}
if serv.BuildOptions.Platform == api.PlatformNode {
for key := range pkgJSON.Dependencies {
serv.BuildOptions.External = append(serv.BuildOptions.External, key)
}
}
logger.Level = serv.BuildOptions.LogLevel
return serv
}
func main() {
var pkgJSON *pkgjson.PkgJSON
pkg, err := ioutil.ReadFile("package.json")
if err == nil {
pkgJSON, err = pkgjson.Parse(pkg)
}
serv := readOptions(pkgJSON)
err = os.MkdirAll(serv.BuildOptions.Outdir, 0775)
logger.Fatal(err, "Failed to create output directory")
if filepath.Ext(serv.Entry) != ".html" {
if serv.Serve == true {
serv.RunEntry(serv.Entry)
} else {
serv.BuildEntry(serv.Entry)
}
} else {
if serv.Serve == true {
serv.ServeIndex(pkgJSON)
} else {
serv.BuildIndex(pkgJSON)
}
}
}