mirror of
https://github.com/antonmedv/fx
synced 2024-11-03 15:40:12 +00:00
Add reducer
This commit is contained in:
parent
3206f5001d
commit
9c81b09720
5
main.go
5
main.go
@ -87,8 +87,11 @@ func main() {
|
||||
}
|
||||
fileName = path.Base(filePath)
|
||||
src = f
|
||||
} else {
|
||||
} else if !stdinIsTty && len(args) == 0 {
|
||||
src = os.Stdin
|
||||
} else {
|
||||
reduce(args)
|
||||
return
|
||||
}
|
||||
|
||||
data, err := io.ReadAll(src)
|
||||
|
59
reduce.go
Normal file
59
reduce.go
Normal file
@ -0,0 +1,59 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path"
|
||||
)
|
||||
|
||||
//go:embed npm/index.js
|
||||
var src []byte
|
||||
|
||||
func reduce(fns []string) {
|
||||
script := path.Join(os.TempDir(), fmt.Sprintf("fx-%v.js", version))
|
||||
_, err := os.Stat(script)
|
||||
if os.IsNotExist(err) {
|
||||
err := os.WriteFile(script, src, 0644)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
deno := false
|
||||
bin, err := exec.LookPath("node")
|
||||
if err != nil {
|
||||
if err != nil {
|
||||
bin, err = exec.LookPath("deno")
|
||||
if err != nil {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "Node.js or Deno is required to run fx with reducers.\n")
|
||||
os.Exit(1)
|
||||
}
|
||||
deno = true
|
||||
}
|
||||
}
|
||||
|
||||
var args []string
|
||||
if deno {
|
||||
args = []string{"run", "-A", script}
|
||||
} else {
|
||||
args = []string{script}
|
||||
}
|
||||
args = append(args, fns...)
|
||||
|
||||
cmd := exec.Command(bin, args...)
|
||||
cmd.Stdin = os.Stdin
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
err = cmd.Run()
|
||||
|
||||
switch err := err.(type) {
|
||||
case nil:
|
||||
os.Exit(0)
|
||||
case *exec.ExitError:
|
||||
os.Exit(err.ExitCode())
|
||||
default:
|
||||
panic(err)
|
||||
}
|
||||
}
|
@ -1,3 +1,3 @@
|
||||
package main
|
||||
|
||||
const version = "30"
|
||||
const version = "30.0.0"
|
||||
|
Loading…
Reference in New Issue
Block a user