Skip to content

Commit

Permalink
feat: --log-file flag
Browse files Browse the repository at this point in the history
  • Loading branch information
Aylur committed Nov 18, 2024
1 parent 3dd3eac commit 7dff0fe
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
27 changes: 25 additions & 2 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"ags/lib"
"fmt"
"io"
"os"
"path/filepath"

Expand All @@ -12,6 +13,7 @@ import (
var (
gtk4 bool
targetDir string
logFile string
args []string
)

Expand Down Expand Up @@ -53,6 +55,7 @@ when no positional argument is given

f.StringArrayVarP(&args, "arg", "a", []string{}, "cli args to pass to gjs")
f.StringVar(&tsconfig, "tsconfig", "", "path to tsconfig.json")
f.StringVar(&logFile, "log-file", "", "file to redirect the stdout of gjs to")
f.MarkHidden("tsconfig")
}

Expand Down Expand Up @@ -88,6 +91,20 @@ func getAppEntry(dir string) string {
return infile
}

func logging() (io.Writer, io.Writer, *os.File) {
if logFile == "" {
return os.Stdout, os.Stderr, nil
}

lib.Mkdir(filepath.Dir(logFile))
file, err := os.OpenFile(logFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
if err != nil {
lib.Err(err)
}

return io.MultiWriter(os.Stdout, file), io.MultiWriter(os.Stderr, file), file
}

func run(infile string) {
outfile := getOutfile()
lib.Bundle(infile, outfile, tsconfig, "")
Expand All @@ -97,14 +114,20 @@ func run(infile string) {
}

args = append([]string{"-m", outfile}, args...)
stdout, stderr, file := logging()
gjs := lib.Exec("gjs", args...)
gjs.Stdout = os.Stdout
gjs.Stderr = os.Stderr
gjs.Stdin = os.Stdin
gjs.Dir = filepath.Dir(infile)

gjs.Stdout = stdout
gjs.Stderr = stderr

// TODO: watch and restart
if err := gjs.Run(); err != nil {
lib.Err(err)
}

if file != nil {
file.Close()
}
}
2 changes: 1 addition & 1 deletion version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.0.1
2.1.0

0 comments on commit 7dff0fe

Please sign in to comment.