forked from antonmedv/fx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reduce.go
70 lines (59 loc) · 1.15 KB
/
reduce.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
package main
import (
_ "embed"
"fmt"
"os"
"os/exec"
"path"
"github.com/antonmedv/fx/internal/engine"
)
//go:embed npm/index.js
var src []byte
func reduce(fns []string) {
if _, ok := os.LookupEnv("FX_JS"); ok {
engine.Reduce(fns)
return
}
var deno bool
bin, err := exec.LookPath("node")
if err != nil {
bin, err = exec.LookPath("deno")
if err != nil {
engine.Reduce(fns)
return
}
deno = true
}
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)
}
}
env := os.Environ()
var args []string
if deno {
args = []string{"run", "-A", script}
env = append(env, "V8_FLAGS=--max-old-space-size=16384")
} else {
args = []string{script}
env = append(env, "NODE_OPTIONS=--max-old-space-size=16384")
}
args = append(args, fns...)
cmd := exec.Command(bin, args...)
cmd.Env = env
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)
}
}