Add async reducers

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

@ -5,14 +5,23 @@ import (
"fmt" "fmt"
"os" "os"
"os/exec" "os/exec"
"path"
"regexp" "regexp"
"strings" "strings"
) )
func CreateNodejs(args []string) *exec.Cmd { 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 = os.Environ()
cmd.Env = append(cmd.Env, "NODE_OPTIONS=--max-old-space-size=8192") 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 return cmd
} }
@ -59,12 +68,15 @@ func nodejs(args []string) string {
default: default:
rs += fmt.Sprintf( rs += fmt.Sprintf(
` `
f = function () let f = function ()
{ return %v } { return %v }
.call(x) .call(x)
x = typeof f === 'function' ? f(x) : f x = typeof f === 'function' ? f(x) : f
`, a) `, a)
} }
rs += `
x = await x
`
// Generate a beautiful error message. // Generate a beautiful error message.
rs += " } catch (e) {\n" rs += " } catch (e) {\n"
pre, post, pointer := trace(args, i) pre, post, pointer := trace(args, i)
@ -74,7 +86,16 @@ func nodejs(args []string) string {
) )
rs += " }\n" 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*)+\\[]") var flatMapRegex = regexp.MustCompile("^(\\.\\w*)+\\[]")

@ -1,14 +1,16 @@
const os = require('os') import os from 'node:os'
const fs = require('fs') import fs from 'node:fs'
const path = require('path') import path from 'node:path'
import {createRequire} from 'node:module'
const require = createRequire(process.cwd())
try { // .fxrc.js %v
require(path.join(os.homedir(), '.fxrc'))
} catch (err) {
if (err.code !== 'MODULE_NOT_FOUND') throw err
}
void async function () { void async function () {
if (process.env.FX_CWD) {
process.chdir(process.env.FX_CWD)
}
let buffer = '' let buffer = ''
process.stdin.setEncoding('utf8') process.stdin.setEncoding('utf8')
for await (let chunk of process.stdin) { for await (let chunk of process.stdin) {

Loading…
Cancel
Save