/
root.go
261 lines (234 loc) · 6.68 KB
/
root.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package cmd
import (
"fmt"
"io/ioutil"
"log"
"os"
"path"
"path/filepath"
"runtime"
"github.com/elsaland/elsa/core"
"github.com/elsaland/elsa/core/options"
"github.com/elsaland/elsa/module"
"github.com/elsaland/elsa/packager"
"github.com/elsaland/elsa/util"
"github.com/fatih/color"
"github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
)
var homeDir, _ = homedir.Dir()
var installSite = path.Join(homeDir, "./.elsa/")
// Elsa functions expected to be passed into cmd
type Elsa struct {
Run func(opt options.Options)
Dev func(og string, opt options.Options)
Bundle func(file string, minify bool, config *module.Config) string
}
// Execute start the CLI
func Execute(elsa Elsa) {
// TODO: need to come to a concrete conclusion
// Load mod.toml (if exists)
config, err := module.ConfigLoad()
util.Check(err)
color.NoColor = config.Options.NoColor
var fsFlag bool
var netFlag bool
var minifyFlag bool
var envFlag bool
var installName string
// Root command
var rootCmd = &cobra.Command{
Use: "",
Short: "",
Run: func(cmd *cobra.Command, args []string) {
core.Repl()
},
}
// Run subcommand
var runCmd = &cobra.Command{
Use: "run [file]",
Short: "Run a JavaScript and TypeScript source file",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
if len(args) >= 0 {
bundle := elsa.Bundle(args[0], true, config)
env := options.Environment{
NoColor: config.Options.NoColor,
Args: args[1:],
RunTests: false,
}
opt := options.Options{
SourceFile: args[0],
Source: bundle,
Perms: &options.Perms{fsFlag, netFlag, envFlag},
Env: env,
}
elsa.Run(opt)
}
},
}
// --fs,--net, --env flags
runCmd.Flags().BoolVar(&fsFlag, "fs", false, "Allow file system access")
runCmd.Flags().BoolVar(&netFlag, "net", false, "Allow net access")
runCmd.Flags().BoolVar(&envFlag, "env", false, "Allow Environment Variables access")
// dev subcommand to run script in development mode
var devCmd = &cobra.Command{
Use: "dev [file]",
Short: "Run a script in development mode.",
Long: `Run a script in development mode. It enables type-checking using the inbuilt TypeScript compiler.`,
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
if len(args) >= 0 {
bundle := elsa.Bundle(args[0], true, config)
env := options.Environment{
NoColor: config.Options.NoColor,
Args: args[1:],
RunTests: false,
}
opt := options.Options{
SourceFile: args[0],
Source: bundle,
Perms: &options.Perms{
Fs: true,
Env: true,
Net: true,
},
Env: env,
}
og, _ := ioutil.ReadFile(args[0])
elsa.Dev(string(og), opt)
}
},
}
// bundle subcommand to bundle a source file
var bundleCmd = &cobra.Command{
Use: "bundle [file]",
Short: "Bundle your script to a single JavaScript file",
Long: `Bundle your script to a single JavaScript file. It utilizes esbuild for super fast bundling.`,
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
if len(args) >= 0 {
out := elsa.Bundle(args[0], minifyFlag, config)
fmt.Println(out)
}
},
}
// --minify flag for bundling
bundleCmd.Flags().BoolVarP(&minifyFlag, "minify", "m", false, "Minify the output bundle")
// pkg subcommand for trigger the packager
var pkgCmd = &cobra.Command{
Use: "pkg [file]",
Short: "Package your script to a standalone executable.",
Long: `Package your script to a standalone executable.`,
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
if len(args) >= 0 {
packager.PkgSource(args[0])
}
},
}
// test subcommand to run test files
var testCmd = &cobra.Command{
Use: "test",
Short: "Run tests for your Elsa scripts.",
Long: `Run tests for your Elsa scripts. All files matching *_test.js are run.`,
Run: func(cmd *cobra.Command, args []string) {
env := options.Environment{
NoColor: config.Options.NoColor,
Args: args,
RunTests: true,
}
opt := options.Options{
Perms: &options.Perms{fsFlag, netFlag, envFlag},
Env: env,
}
tests := CollectTests()
for _, test := range tests {
opt.SourceFile = test
bundle := elsa.Bundle(test, true, config)
opt.Source = bundle
elsa.Run(opt)
}
},
}
// --net, --fs, --env perms
testCmd.Flags().BoolVar(&fsFlag, "fs", false, "Allow file system access")
testCmd.Flags().BoolVar(&netFlag, "net", false, "Allow net access")
testCmd.Flags().BoolVar(&envFlag, "env", false, "Allow Environment Variables access")
// install subcommand to bundle and shebang to PATH env
var installCmd = &cobra.Command{
Use: "install",
Short: "Install an Elsa module.",
Long: `Install an Elsa module.`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) >= 0 {
out := elsa.Bundle(args[0], true, config)
bundleLoc := path.Join(os.TempDir(), installName+".js")
err := ioutil.WriteFile(bundleLoc, []byte(out), 0777)
if err != nil {
panic(err)
}
scriptFile := path.Join(installSite, installName)
// add .cmd in script for windows
isWindows(&scriptFile, scriptFile+".cmd")
err = ioutil.WriteFile(scriptFile, []byte(shebang(bundleLoc)), 0777)
if err != nil {
panic(err)
}
fmt.Println("Installation complete.")
}
},
}
installCmd.Flags().StringVar(&installName, "name", "00", "Executable name of the installed script")
// Add subcommands to root command
rootCmd.AddCommand(bundleCmd, runCmd, pkgCmd, devCmd, testCmd, installCmd)
// Execute! :)
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func shebang(loc string) string {
exec := `
#!/bin/sh
elsa "run" "%s" "$@"`
// for windows
isWindows(&exec, `@elsa "run" "%s" %*`)
return fmt.Sprintf(exec, loc)
}
// replace a string if it is windows
func isWindows(toChange *string, replacement string) {
if runtime.GOOS == "windows" {
*toChange = replacement
}
}
// match test files
func matchedFiles(name string) bool {
matchedJS, err := filepath.Match("*_test.js", name)
matchedTS, err := filepath.Match("*_test.ts", name)
matchedJSTest, err := filepath.Match("*.test.js", name)
matchedTSTest, err := filepath.Match("*.test.ts", name)
if err != nil {
log.Fatal(err)
}
return (matchedJS || matchedTS || matchedTSTest || matchedJSTest)
}
// CollectTests files
func CollectTests() []string {
var testFiles []string
e := filepath.Walk(".", func(path string, info os.FileInfo, err error) error {
if err == nil {
if err != nil {
return nil
}
if matchedFiles(info.Name()) {
testFiles = append(testFiles, path)
}
}
return nil
})
if e != nil {
log.Fatal(e)
}
return testFiles
}