forked from hibuno/denomon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
163 lines (127 loc) · 2.93 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
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
package main
import (
"bytes"
"flag"
"fmt"
"os"
"os/exec"
"strings"
"time"
"github.com/fatih/color"
"github.com/radovskyb/watcher"
)
var pid int = 0
var command exec.Cmd
var args []string
var file string = ""
var path string = ""
var permission []string
var hasMainFile bool = false
var mainFile string = ""
func help() {
fmt.Printf("[DENOMON HELP]\n\n")
fmt.Printf("Usage:\n\n")
fmt.Printf("denomon <options> <file>\n\n")
fmt.Printf("Options:\n\n")
fmt.Printf("--version Showing denomon version\n")
fmt.Printf("--help Print this help message\n")
fmt.Printf("--dir Assign directory to watch\n")
fmt.Printf("--allow Assign permission for the files\n\n")
}
func build(file string) bool {
color.Green("[denomon] build: %s", file)
if pid > 0 {
command.Process.Kill()
}
var stderr bytes.Buffer
options := []string{"run", file}
if len(permission) > 0 {
options = []string{"run"}
for i := 0; i < len(permission); i++ {
options = append(options, "--allow-"+permission[i])
}
options = append(options, file)
}
cmd := exec.Command("deno", options...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
fmt.Print("\033[s")
fmt.Print(cmd.Stdout)
fmt.Print("\033[u\033[K")
if err := cmd.Start(); err != nil {
color.Red("[denomon] error: %s", fmt.Sprint(err)+": "+stderr.String())
}
pid = cmd.Process.Pid
command = *cmd
return false
}
func main() {
dir, err := os.Getwd()
if err != nil {
color.Red("[denomon] error: %s", err.Error())
}
dirPtr := flag.String("dir", "", "Assign directory to watch")
helpPtr := flag.Bool("help", false, "Print this help message")
allowPtr := flag.String("allow", "", "Assign permission for the files")
versionPtr := flag.Bool("version", false, "Showing denomon version")
flag.Parse()
if *allowPtr != "" {
allowSlice := strings.Split(*allowPtr, ",")
for i := range allowSlice {
allowSlice[i] = strings.TrimSpace(allowSlice[i])
}
permission = allowSlice
}
args = flag.Args()
if *versionPtr == true {
color.Green("[denomon] version %s", "0.1.1")
return
}
if *helpPtr == true {
help()
return
}
if len(args) > 0 {
file = args[0]
}
path = dir + "/"
if *dirPtr != "" {
path = dir + "/" + *dirPtr + "/"
}
if file != "" {
hasMainFile = true
mainFile = path + file
build(mainFile)
}
color.Cyan("[denomon] watching: %s", path)
w := watcher.New()
w.IgnoreHiddenFiles(true)
go func() {
for {
select {
case event := <-w.Event:
if hasMainFile {
repeat := build(mainFile)
if repeat {
build(mainFile)
}
} else {
repeat := build(event.Path)
if repeat {
build(event.Path)
}
}
case err := <-w.Error:
color.Red("[denomon] error: %s", err.Error())
case <-w.Closed:
return
}
}
}()
if err := w.AddRecursive(path); err != nil {
color.Red("[denomon] error: %s", err.Error())
}
if err := w.Start(time.Millisecond * 100); err != nil {
color.Red("[denomon] error: %s", err.Error())
}
}