2022-04-18 20:00:17 +00:00
|
|
|
package reducer
|
|
|
|
|
|
|
|
import (
|
|
|
|
_ "embed"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2022-04-22 20:28:07 +00:00
|
|
|
"path"
|
2022-04-18 20:00:17 +00:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
func CreateNodejs(args []string) *exec.Cmd {
|
2022-04-22 20:28:07 +00:00
|
|
|
cmd := exec.Command("node", "--input-type=module", "-e", nodejs(args))
|
|
|
|
nodePath, exist := os.LookupEnv("NODE_PATH")
|
|
|
|
if exist {
|
|
|
|
cmd.Dir = path.Dir(nodePath)
|
|
|
|
}
|
2022-04-18 20:00:17 +00:00
|
|
|
cmd.Env = os.Environ()
|
|
|
|
cmd.Env = append(cmd.Env, "NODE_OPTIONS=--max-old-space-size=8192")
|
2022-04-22 20:28:07 +00:00
|
|
|
workingDir, err := os.Getwd()
|
|
|
|
if err == nil {
|
2022-04-23 10:20:29 +00:00
|
|
|
cmd.Env = append(cmd.Env, "FX_CWD="+workingDir)
|
2022-04-22 20:28:07 +00:00
|
|
|
}
|
2022-04-18 20:00:17 +00:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
//go:embed reduce.js
|
|
|
|
var templateJs string
|
|
|
|
|
2022-04-18 20:57:05 +00:00
|
|
|
func nodejs(args []string) string {
|
2022-04-18 20:00:17 +00:00
|
|
|
rs := "\n"
|
|
|
|
for i, a := range args {
|
|
|
|
rs += " try {"
|
|
|
|
switch {
|
|
|
|
case a == ".":
|
|
|
|
rs += `
|
|
|
|
x = function ()
|
|
|
|
{ return this }
|
|
|
|
.call(x)
|
|
|
|
`
|
|
|
|
|
|
|
|
case flatMapRegex.MatchString(a):
|
|
|
|
code := fold(strings.Split(a, "[]"))
|
|
|
|
rs += fmt.Sprintf(
|
|
|
|
`
|
|
|
|
x = (
|
|
|
|
%v
|
|
|
|
)(x)
|
|
|
|
`, code)
|
|
|
|
|
|
|
|
case strings.HasPrefix(a, ".["):
|
|
|
|
rs += fmt.Sprintf(
|
|
|
|
`
|
|
|
|
x = function ()
|
|
|
|
{ return this%v }
|
|
|
|
.call(x)
|
|
|
|
`, a[1:])
|
|
|
|
|
|
|
|
case strings.HasPrefix(a, "."):
|
|
|
|
rs += fmt.Sprintf(
|
|
|
|
`
|
|
|
|
x = function ()
|
|
|
|
{ return this%v }
|
|
|
|
.call(x)
|
|
|
|
`, a)
|
|
|
|
|
|
|
|
default:
|
|
|
|
rs += fmt.Sprintf(
|
|
|
|
`
|
2022-04-22 20:28:07 +00:00
|
|
|
let f = function ()
|
2022-04-18 20:00:17 +00:00
|
|
|
{ return %v }
|
|
|
|
.call(x)
|
2022-04-22 13:16:43 +00:00
|
|
|
x = typeof f === 'function' ? f(x) : f
|
2022-04-18 20:00:17 +00:00
|
|
|
`, a)
|
|
|
|
}
|
2022-04-22 20:28:07 +00:00
|
|
|
rs += `
|
|
|
|
x = await x
|
|
|
|
`
|
2022-04-18 20:00:17 +00:00
|
|
|
// Generate a beautiful error message.
|
|
|
|
rs += " } catch (e) {\n"
|
|
|
|
pre, post, pointer := trace(args, i)
|
|
|
|
rs += fmt.Sprintf(
|
|
|
|
" throw `\\n ${%q} ${%q} ${%q}\\n %v\\n\\n${e.stack || e}`\n",
|
|
|
|
pre, a, post, pointer,
|
|
|
|
)
|
|
|
|
rs += " }\n"
|
|
|
|
}
|
2022-04-22 20:28:07 +00:00
|
|
|
|
|
|
|
fxrc := ""
|
|
|
|
home, err := os.UserHomeDir()
|
|
|
|
if err == nil {
|
|
|
|
b, err := os.ReadFile(path.Join(home, ".fxrc.js"))
|
|
|
|
if err == nil {
|
|
|
|
fxrc = "\n" + string(b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fmt.Sprintf(templateJs, fxrc, rs)
|
2022-04-18 20:00:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var flatMapRegex = regexp.MustCompile("^(\\.\\w*)+\\[]")
|
|
|
|
|
|
|
|
func fold(s []string) string {
|
|
|
|
if len(s) == 1 {
|
|
|
|
return "x => x" + s[0]
|
|
|
|
}
|
|
|
|
obj := s[0]
|
|
|
|
if obj == "." {
|
|
|
|
obj = "x"
|
|
|
|
} else {
|
|
|
|
obj = "x" + obj
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("x => Object.values(%v).flatMap(%v)", obj, fold(s[1:]))
|
|
|
|
}
|