Add async reducers

sleep-stdin-bug
Anton Medvedev 2 years ago
parent 77b2632aeb
commit 915c1e9fa7

@ -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
}
@ -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)
@ -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*)+\\[]")

@ -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) {

Loading…
Cancel
Save