update for watch, support test on watch

This commit is contained in:
Star 2024-10-27 00:43:48 +08:00
parent 308a8afc62
commit 61cebe7ecc
2 changed files with 80 additions and 25 deletions

32
main.go
View File

@ -64,11 +64,11 @@ var commands = []Command{
// {"login", "l", "", "login to apigo.cloud", login},
// {"deploy", "dp", "", "deploy project to apigo.cloud", depolyProject},
{"run", "r", "[args...]", "run project", runProject},
{"buildrun", "rr", "[args...]", "build and run project", buildRunProject},
{"watch", "w", "[args...]", "run and watch project", watchProject},
{"buildwatch", "ww", "[args...]", "build and run and watch project", buildWatchProject},
{"runWithBuild", "rb", "[args...]", "build and run project", buildRunProject},
{"runWithWatch", "rw", "[args...]", "watch and run project", watchRunProject},
{"test", "t", "[args...]", "test project", testProject},
{"buildtest", "tt", "[args...]", "build and test project", buildTestProject},
{"testWithBuild", "tb", "[args...]", "build and test project", buildTestProject},
{"testWithWatch", "tw", "[args...]", "watch and test project", watchTestProject},
{"tidy", "td", "", "tidy project, run go mod tidy", tidyProject},
{"check", "ck", "", "check go modules version", checkProject},
{"export", "e", "", "export typescript code for dev", exportForDev},
@ -744,6 +744,11 @@ func buildRunProject(args []string) bool {
return runProject(args)
}
func watchRunProject(args []string) bool {
args = append([]string{"-w"}, args...)
return runProject(args)
}
func testProject(args []string) bool {
args = append([]string{"test"}, args...)
return runProject(args)
@ -755,16 +760,21 @@ func buildTestProject(args []string) bool {
return runProject(args)
}
func watchProject(args []string) bool {
args = append([]string{"-w"}, args...)
func watchTestProject(args []string) bool {
args = append([]string{"-w", "test"}, args...)
return runProject(args)
}
func buildWatchProject(args []string) bool {
buildProject(args)
args = append([]string{"-w"}, args...)
return runProject(args)
}
// func watchProject(args []string) bool {
// args = append([]string{"-w"}, args...)
// return runProject(args)
// }
// func buildWatchProject(args []string) bool {
// buildProject(args)
// args = append([]string{"-w"}, args...)
// return runProject(args)
// }
func exportForDev(args []string) bool {
return runProject([]string{"-export"})

View File

@ -26,8 +26,6 @@ var appName = "{{.Name}}"
var appVersion = "{{.Version}}"
var idFixMatcher = regexp.MustCompile(`[^a-zA-Z0-9_]`)
// TODO embed .CacheFiles
func init() {
{{- range $alias, $pkg := .ModuleAlias }}
gojs.Alias("{{$alias}}", "{{$pkg}}")
@ -47,7 +45,6 @@ func main() {
if appVersion == "" {
appVersion = "0.0.1"
}
cmd := ""
id := ""
mainFile := _mainFile
@ -133,35 +130,83 @@ func main() {
if u.FileExists("tests") {
testPath = "tests"
}
for _, f := range u.ReadDirN(testPath) {
if !f.IsDir && strings.HasSuffix(f.Name, "_test.js") {
if r, err := gojs.RunFile(f.FullName, mainArgs...); err != nil {
fmt.Println(u.BRed("test failed for "+f.FullName), err.Error())
if isWatch {
var wr *gojs.WatchRunner
wr, _ = gojs.Watch([]string{testPath}, []string{"js", "yml", "json"}, func(changes []string) {
if len(changes) == 1 && strings.HasSuffix(changes[0], "_test.js") {
runTest(changes[0], wr)
} else {
fmt.Println(u.Green("test passed for "+f.FullName), r)
runAllTest(testPath, wr)
}
gojs.WaitAll()
}
})
} else {
runAllTest(testPath, nil)
}
gojs.WaitAll()
default:
defer os.Remove(pidFile)
if isWatch {
gojs.WatchRun(mainFile, nil, nil, mainArgs...)
gojs.WatchRun(mainFile, mainArgs...)
} else {
if r, err := gojs.RunFile(mainFile, mainArgs...); err != nil {
log.DefaultLogger.Error("run failed for "+mainFile, "err", err.Error())
} else if r != nil {
log.DefaultLogger.Error("run "+mainFile, "result", r)
}
gojs.WaitAll()
}
gojs.WaitAll()
}
}
func runAllTest(testPath string, wr *gojs.WatchRunner) {
for _, f := range u.ReadDirN(testPath) {
if !f.IsDir && strings.HasSuffix(f.Name, "_test.js") {
runTest(f.Name, wr)
}
}
}
func runTest(filename string, wr *gojs.WatchRunner) {
vm := gojs.New()
if wr != nil {
wr.SetModuleLoader(vm)
}
if _, err := vm.RunFile(filename); err != nil {
fmt.Println(u.BRed("run "+filename), u.Red(err.Error()))
return
}
code := u.ReadFileN(filename)
if testCaseOnStartMatcher.MatchString(code) {
if _, err := vm.RunCode(`onStart()`); err != nil {
fmt.Println(u.Red(filename), u.BRed("onStart"), u.Red(err.Error()))
return
} else {
fmt.Println(u.Green(filename), u.BGreen("onStart"), u.Green("success"))
}
}
for _, m := range testCaseMatcher.FindAllStringSubmatch(code, 1000) {
testCaseName := m[1]
if r, err := vm.RunCode(testCaseName + "()"); err != nil {
fmt.Println(u.Red(filename), u.BRed(testCaseName), u.Red(err.Error()))
} else if r == true {
fmt.Println(u.Green(filename), u.BGreen(testCaseName), u.Green("pass"))
} else {
fmt.Println(u.Red(filename), u.BRed(testCaseName), u.Red(u.StringP(r)))
}
}
if testCaseOnStopMatcher.MatchString(code) {
if _, err := vm.RunCode(`onStop()`); err != nil {
fmt.Println(u.Red(filename), u.BRed("onStop"), u.Red(err.Error()))
} else {
fmt.Println(u.Green(filename), u.BGreen("onStop"), u.Green("success"))
}
}
}
func startProcess(pidFile string, startArgs []string) {
cmd := exec.Command(os.Args[0], startArgs...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
// cmd.Stdout = os.Stdout
// cmd.Stderr = os.Stderr
if err := cmd.Start(); err == nil {
u.WriteFile(pidFile, u.String(cmd.Process.Pid))
log.DefaultLogger.Info("started", "appName", appName, "appVersion", appVersion, "startArgs", startArgs, "pid", cmd.Process.Pid)