Skip to content

Commit

Permalink
Add async reducers
Browse files Browse the repository at this point in the history
  • Loading branch information
antonmedv committed Apr 22, 2022
1 parent 77b2632 commit 915c1e9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
27 changes: 24 additions & 3 deletions pkg/reducer/nodejs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,23 @@ import (
"fmt"
"os"
"os/exec"
"path"
"regexp"
"strings"
)

func CreateNodejs(args []string) *exec.Cmd {
cmd := exec.Command("node", "-e", nodejs(args))
cmd := exec.Command("node", "--input-type=module", "-e", nodejs(args))
nodePath, exist := os.LookupEnv("NODE_PATH")
if exist {
cmd.Dir = path.Dir(nodePath)
}
cmd.Env = os.Environ()
cmd.Env = append(cmd.Env, "NODE_OPTIONS=--max-old-space-size=8192")
workingDir, err := os.Getwd()
if err == nil {
cmd.Env = append(cmd.Env, "FX_CMD="+workingDir)
}
return cmd
}

Expand Down Expand Up @@ -59,12 +68,15 @@ func nodejs(args []string) string {
default:
rs += fmt.Sprintf(
`
f = function ()
let f = function ()
{ return %v }
.call(x)
x = typeof f === 'function' ? f(x) : f
`, a)
}
rs += `
x = await x
`
// Generate a beautiful error message.
rs += " } catch (e) {\n"
pre, post, pointer := trace(args, i)
Expand All @@ -74,7 +86,16 @@ func nodejs(args []string) string {
)
rs += " }\n"
}
return fmt.Sprintf(templateJs, rs)

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)
}

var flatMapRegex = regexp.MustCompile("^(\\.\\w*)+\\[]")
Expand Down
18 changes: 10 additions & 8 deletions pkg/reducer/reduce.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
const os = require('os')
const fs = require('fs')
const path = require('path')
import os from 'node:os'
import fs from 'node:fs'
import path from 'node:path'
import {createRequire} from 'node:module'
const require = createRequire(process.cwd())

try {
require(path.join(os.homedir(), '.fxrc'))
} catch (err) {
if (err.code !== 'MODULE_NOT_FOUND') throw err
}
// .fxrc.js %v

void async function () {
if (process.env.FX_CWD) {
process.chdir(process.env.FX_CWD)
}

let buffer = ''
process.stdin.setEncoding('utf8')
for await (let chunk of process.stdin) {
Expand Down

0 comments on commit 915c1e9

Please sign in to comment.