0

我想使用stdin这个函数的参数:http: //graspjs.com/docs/lib/

grasp函数期望这个参数是一个具有相同接口的对象process.stdin。我所拥有的是字符串类型的内存中的一个简单变量。

如何将此变量提供给该函数的标准输入?

var grasp = require('grasp');
var sourceCode = 'if (condititon) { console.log("In the condition"); }';

grasp({
    args: '--equery condititon --replace true',
    stdin: SomethingLikeStringToStdin(sourceCode),
    callback: console.log 
});

预期日志:

if (true) { console.log("In the condition"); }
4

2 回答 2

2

在 Grasp 0.2.0 中,您现在可以input在将 Grasp 用作库时使用新选项,或使用以下两个新辅助函数之一:grasp.searchgrasp.replace. 这将允许你做你想做的事,而不必创建一个假的 StdIn。

文档: http: //graspjs.com/docs/lib/

于 2013-12-18T00:33:25.907 回答
1

process.stdin是一个Readable Stream。期望的是grasp一个可以从中读取数据的流。为了模拟这种行为,您可以使用PassThrough流:它是一个流,您可以将缓冲区字符串写入其中,并且会像任何可读流一样发出此数据。

这是一个使用示例:

var stream = require('stream');
var passthrough = new stream.PassThrough();

grasp({ stdin: passthrough });
passthrough.push('some data');
passthrough.push('some other data');
passthrough.end();
于 2013-11-09T16:19:34.470 回答