使用脚本初始化Node.js,然后提供控制台

时间:2021-08-20 21:59:29

For testing purposes, I would like to make it possible to spawn a Node.js console that reads commands from STDIN but before runs a bootstrap script to initialize several variables in the local scope.

出于测试目的,我希望能够生成一个Node.js控制台,该控制台从STDIN读取命令,但在运行引导脚本以初始化本地范围内的多个变量之前。

Does node provide a specific option for this?

节点是否为此提供了特定选项?

I tried to do the trick with the following bash script, but unfortunately, it outputs nothing:

我尝试使用以下bash脚本来完成这个技巧,但遗憾的是,它没有输出任何内容:

{ cat bootstrap.js; cat; } | node

1 个解决方案

#1


1  

As it turned out, the script quoted in the question is only missing the node option -i that causes node to

事实证明,问题中引用的脚本只缺少导致节点的节点选项-i

always enter the REPL even if stdin does not appear to be a terminal

即使stdin似乎不是终端,也总是输入REPL

As a consequence, this does the trick:

因此,这就是诀窍:

cat bootstrap.js - | node -i

The behavior is still not optimal though as STDIN is printed twice.

尽管STDIN打印两次,但行为仍然不是最佳的。

See https://*.com/a/23037234/449012 for another solution that behaves just like the default console.

有关另一种行为与默认控制台类似的解决方案,请参阅https://*.com/a/23037234/449012。

#1


1  

As it turned out, the script quoted in the question is only missing the node option -i that causes node to

事实证明,问题中引用的脚本只缺少导致节点的节点选项-i

always enter the REPL even if stdin does not appear to be a terminal

即使stdin似乎不是终端,也总是输入REPL

As a consequence, this does the trick:

因此,这就是诀窍:

cat bootstrap.js - | node -i

The behavior is still not optimal though as STDIN is printed twice.

尽管STDIN打印两次,但行为仍然不是最佳的。

See https://*.com/a/23037234/449012 for another solution that behaves just like the default console.

有关另一种行为与默认控制台类似的解决方案,请参阅https://*.com/a/23037234/449012。