我想使用这个函数的stdin参数:http://graspjs.com/docs/lib/。
grasp函数期望该参数是一个具有与process.stdin相同接口的对象。我所拥有的是字符串类型内存中的一个简单变量。
如何将这个变量给这个函数的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"); }发布于 2013-11-09 16:19:34
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-12-18 00:33:25
使用抓取0.2.0,您现在可以在将抓取作为库时使用新的input选项,或者使用两个新的助手函数之一:grasp.search和grasp.replace。这将允许您做您想做的,而不必创建一个假的StdIn。
文档:http://graspjs.com/docs/lib/
https://stackoverflow.com/questions/19878078
复制相似问题