Skip to content

Commit 4ea8df2

Browse files
add help command, restructure
1 parent 5cd6f94 commit 4ea8df2

11 files changed

Lines changed: 149 additions & 67 deletions

File tree

.gitignore

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1-
splitscript
2-
node_modules
1+
node_modules
2+
cli
3+
/example/dev
4+
/splitscript/splitscript

cmd.go

Lines changed: 0 additions & 31 deletions
This file was deleted.

cmd/cmd.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/charmbracelet/bubbles/table"
8+
)
9+
10+
type Handle func(args []string)
11+
type Handler struct {
12+
arguments Args
13+
function Handle
14+
description string
15+
}
16+
type Args map[string]string
17+
18+
var commands map[string]Handler = make(map[string]Handler)
19+
20+
func Add(name string, description string, args Args, function Handle) {
21+
commands[name] = Handler{
22+
arguments: args,
23+
function: function,
24+
description: description,
25+
}
26+
}
27+
func Parse() {
28+
name := "help"
29+
args := []string{}
30+
if len(os.Args) > 1 {
31+
name = os.Args[1]
32+
args = os.Args[2:]
33+
}
34+
35+
handler := commands[name]
36+
if handler.arguments == nil || handler.function == nil {
37+
fmt.Println("Command not found")
38+
return
39+
}
40+
handler.function(args)
41+
}
42+
func List() []table.Row {
43+
rows := make([]table.Row, len(commands))
44+
45+
i := 0
46+
for cmd, val := range commands {
47+
rows[i] = []string{cmd, val.description}
48+
i++
49+
}
50+
return rows
51+
}

example/app.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
import discord from '@splitscript.js/discord'
1+
import discord from "@splitscript.js/discord";
22

3-
discord.listen('123')
3+
discord.listen("123");
4+
let iter = 0;
45
setInterval(() => {
5-
console.log("avbc")
6-
}, 1000)
7-
// throw "abc"
6+
console.log("avbc");
7+
iter++;
8+
if (iter === 10) {
9+
throw "abc";
10+
}
11+
}, 1000);

go.mod

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,27 @@ require (
99

1010
require (
1111
github.com/BurntSushi/toml v1.3.2
12+
github.com/charmbracelet/bubbles v0.18.0
1213
github.com/charmbracelet/lipgloss v0.10.0
1314
github.com/evanw/esbuild v0.21.1
1415
gopkg.in/fsnotify.v1 v1.4.7
1516
)
1617

1718
require (
1819
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
20+
github.com/charmbracelet/bubbletea v0.25.0 // indirect
21+
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 // indirect
1922
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
2023
github.com/mattn/go-isatty v0.0.20 // indirect
24+
github.com/mattn/go-localereader v0.0.1 // indirect
2125
github.com/mattn/go-runewidth v0.0.15 // indirect
26+
github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b // indirect
27+
github.com/muesli/cancelreader v0.2.2 // indirect
2228
github.com/muesli/reflow v0.3.0 // indirect
2329
github.com/muesli/termenv v0.15.2 // indirect
2430
github.com/rivo/uniseg v0.4.7 // indirect
31+
golang.org/x/sync v0.1.0 // indirect
2532
golang.org/x/sys v0.20.0 // indirect
33+
golang.org/x/term v0.6.0 // indirect
34+
golang.org/x/text v0.3.8 // indirect
2635
)

go.sum

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,14 @@ github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8
22
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
33
github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=
44
github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
5+
github.com/charmbracelet/bubbles v0.18.0 h1:PYv1A036luoBGroX6VWjQIE9Syf2Wby2oOl/39KLfy0=
6+
github.com/charmbracelet/bubbles v0.18.0/go.mod h1:08qhZhtIwzgrtBjAcJnij1t1H0ZRjwHyGsy6AL11PSw=
7+
github.com/charmbracelet/bubbletea v0.25.0 h1:bAfwk7jRz7FKFl9RzlIULPkStffg5k6pNt5dywy4TcM=
8+
github.com/charmbracelet/bubbletea v0.25.0/go.mod h1:EN3QDR1T5ZdWmdfDzYcqOCAps45+QIJbLOBxmVNWNNg=
59
github.com/charmbracelet/lipgloss v0.10.0 h1:KWeXFSexGcfahHX+54URiZGkBFazf70JNMtwg/AFW3s=
610
github.com/charmbracelet/lipgloss v0.10.0/go.mod h1:Wig9DSfvANsxqkRsqj6x87irdy123SR4dOXlKa91ciE=
11+
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 h1:q2hJAaP1k2wIvVRd/hEHD7lacgqrCPS+k8g1MndzfWY=
12+
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81/go.mod h1:YynlIjWYF8myEu6sdkwKIvGQq+cOckRm6So2avqoYAk=
713
github.com/evanw/esbuild v0.21.1 h1:jCIQQsdFD89gqcB6r/tfgSDPaYpjJT7+f2mCCqRIhvE=
814
github.com/evanw/esbuild v0.21.1/go.mod h1:D2vIQZqV/vIf/VRHtViaUtViZmG7o+kKmlBfVQuRi48=
915
github.com/farmergreg/rfsnotify v0.0.0-20200716145600-b37be6e4177f h1:sRiluoTSaqFH+5cK4AsNX5ttFij+chr0Gj/HG/FkNGs=
@@ -14,9 +20,15 @@ github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69
1420
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
1521
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
1622
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
23+
github.com/mattn/go-localereader v0.0.1 h1:ygSAOl7ZXTx4RdPYinUpg6W99U8jWvWi9Ye2JC/oIi4=
24+
github.com/mattn/go-localereader v0.0.1/go.mod h1:8fBrzywKY7BI3czFoHkuzRoWE9C+EiG4R1k4Cjx5p88=
1725
github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
1826
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
1927
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
28+
github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b h1:1XF24mVaiu7u+CFywTdcDo2ie1pzzhwjt6RHqzpMU34=
29+
github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b/go.mod h1:fQuZ0gauxyBcmsdE3ZT4NasjaRdxmbCS0jRHsrWu3Ho=
30+
github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELUXHmA=
31+
github.com/muesli/cancelreader v0.2.2/go.mod h1:3XuTXfFS2VjM+HTLZY9Ak0l6eUKfijIfMUZ4EgX0QYo=
2032
github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s=
2133
github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8=
2234
github.com/muesli/termenv v0.15.2 h1:GohcuySI0QmI3wN8Ok9PtKGkgkFIk7y6Vpb5PvrY+Wo=
@@ -25,9 +37,16 @@ github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJ
2537
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
2638
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
2739
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
40+
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
41+
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
2842
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
43+
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
2944
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
3045
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
3146
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
47+
golang.org/x/term v0.6.0 h1:clScbb1cHjoCkyRbWwBEUZ5H/tIFu5TAXIqaZD0Gcjw=
48+
golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U=
49+
golang.org/x/text v0.3.8 h1:nAL+RVCQ9uMn3vJZbV+MRnydTJFPf8qqY42YiA6MrqY=
50+
golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
3251
gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4=
3352
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=

main.go

Lines changed: 0 additions & 28 deletions
This file was deleted.

run/run.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
)
1313

1414
var previous int = -1
15+
var closedProcess int = -1
1516

1617
func Run(conf config.Config) {
1718
if previous != -1 {
@@ -22,6 +23,7 @@ func Run(conf config.Config) {
2223
}
2324
p.Signal(syscall.SIGTERM)
2425
fmt.Printf("Killed %d\n", previous)
26+
closedProcess = previous
2527
}
2628
fileToRun, err := utils.GenerateDevFileName(conf, conf.Main)
2729
fmt.Println(utils.Info.Render("Running `" + conf.Main + "`"))
@@ -44,7 +46,9 @@ func Run(conf config.Config) {
4446
fmt.Println(m)
4547
}
4648
node.Wait()
47-
fmt.Println("Finished running, waiting for updates")
49+
if closedProcess != node.Process.Pid {
50+
fmt.Println("Finished running, waiting for updates")
51+
}
4852
}()
4953
stderr, err := node.StderrPipe()
5054
if err != nil {

splitscript/main.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/charmbracelet/bubbles/table"
8+
"github.com/charmbracelet/lipgloss"
9+
"github.com/splitscriptjs/cli/cmd"
10+
"github.com/splitscriptjs/cli/config"
11+
"github.com/splitscriptjs/cli/utils"
12+
)
13+
14+
func main() {
15+
cmd.Add("dev", "Run and watch an app", make(cmd.Args), func(args []string) {
16+
fmt.Println(utils.Block.Render("Splitscript"))
17+
18+
conf, err := config.Read()
19+
if os.IsNotExist(err) {
20+
fmt.Println(utils.Error.Render(err.Error()))
21+
fmt.Println(utils.Warning.Render("Try running `splitscript init`"))
22+
os.Exit(1)
23+
} else if err != nil {
24+
fmt.Println(utils.Error.Render("Couldn't read config"))
25+
fmt.Println(err.Error())
26+
os.Exit(1)
27+
}
28+
watchDir(conf, "./")
29+
})
30+
cmd.Add("help", "View this message", make(cmd.Args), func(_ []string) {
31+
columns := []table.Column{
32+
{Title: "Name", Width: 8},
33+
{Title: "Description", Width: 32},
34+
}
35+
rows := cmd.List()
36+
37+
t := table.New(table.WithColumns(columns), table.WithRows(rows), table.WithHeight(len(rows)-1))
38+
s := table.DefaultStyles()
39+
s.Header = s.Header.
40+
BorderStyle(lipgloss.NormalBorder()).
41+
BorderForeground(lipgloss.Color("240")).
42+
BorderBottom(true).
43+
Bold(false)
44+
45+
t.SetStyles(s)
46+
var box = lipgloss.NewStyle().
47+
BorderStyle(lipgloss.NormalBorder()).
48+
BorderForeground(lipgloss.Color("240"))
49+
fmt.Println(box.Render(t.View()))
50+
})
51+
cmd.Parse()
52+
}

0 commit comments

Comments
 (0)