-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
182 lines (160 loc) · 4.05 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package main
import (
"errors"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"time"
"github.com/fsnotify/fsnotify"
)
var watcher *fsnotify.Watcher
// CLI entrypoint
func main() {
// get the input command
commandInput := os.Args[1:]
err := run(commandInput, make(chan bool))
if err != nil {
log.Fatalf(err.Error())
}
return
}
func run(commandInput []string, done chan bool) error {
// check for no command input
if len(commandInput) == 0 {
return errors.New("No arguments supplied")
}
// run the input command initially
clearScreen()
cmd := initializeCmd(commandInput)
err := cmd.Start()
if err != nil {
return err
}
// start watching for file changes
watcher, err = deployWatchers()
if err != nil {
return err
}
defer watcher.Close()
// refresh the command any time a file is changed
err = monitor(cmd, commandInput, done)
if err != nil {
return err
}
return nil
}
//Attempt to clear the terminal screen depending on the OS. Print a warning
//message if the system is not supported
//NOTE: only tested on macOS
var clearScreen = func() {
switch system := runtime.GOOS; system {
case "darwin":
func() {
cmd := exec.Command("clear")
cmd.Stdout = os.Stdout
cmd.Run()
}()
case "linux":
func() {
cmd := exec.Command("clear")
cmd.Stdout = os.Stdout
cmd.Run()
}()
case "windows":
func() {
cmd := exec.Command("cmd", "/c", "cls")
cmd.Stdout = os.Stdout
cmd.Run()
}()
default:
fmt.Println("Platform unsupported! Could not clear the screen")
}
}
// recursively add watchers to directories to capture file change events
func deployWatchers() (*fsnotify.Watcher, error) {
path, err := os.Getwd()
if err != nil {
return nil, err
}
// creates a new file watcher which watches all files for changes in the
// directory that it is placed in
watcher, err = fsnotify.NewWatcher()
if err != nil {
return nil, err
}
// starting at the root of the project, walk each file/directory searching
// for directories to add a watcher to
if err := filepath.Walk(path, watchDir); err != nil {
return nil, err
}
return watcher, nil
}
// watchDir gets run as a walk func, searching for directories to add watchers
func watchDir(path string, fi os.FileInfo, err error) error {
// ignore node_modules/ and .git/ because they cause the watchers to trigger
if strings.Contains(path, "node_modules") || strings.Contains(path, ".git") {
return nil
}
// since fsnotify can watch all the files in a directory, watchers only need
// to be added to each nested directory
if fi.Mode().IsDir() {
return watcher.Add(path)
}
return nil
}
// convert the command input -> an executable command (exec.Command) that
// will output its stdout and stderr to the console
var initializeCmd = func(commandInput []string) *exec.Cmd {
cmd := exec.Command(commandInput[0], commandInput[1:]...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd
}
// reruns the command when a file change is detected
func monitor(cmd *exec.Cmd, commandInput []string, done chan bool) error {
var readyForCommand = true
// watch for events to refresh the command
for {
select {
// watch for events
case event := <-watcher.Events:
if !(event.Op&fsnotify.Write == fsnotify.Write) {
break
}
if readyForCommand {
go pauseMonitoring(&readyForCommand)
// kill the previous commands process before restarting
err := cmd.Process.Kill()
if err != nil {
return err
}
// run the command
clearScreen()
cmd = initializeCmd(commandInput)
err = cmd.Start()
if err != nil {
return err
}
}
// watch for errors
case err := <-watcher.Errors:
return err
// exit when 'done'
case _ = <-done:
return nil
}
}
}
// After an event is triggered, have a waiting period before
// another event can be triggered. Its common for a file save
// to trigger multiple events so creating a short artificial
// delay between triggers makes the program feel smoother
func pauseMonitoring(readyForCommand *bool) {
*readyForCommand = false
time.Sleep(2 * time.Second)
*readyForCommand = true
}