2013-11-09 18 views
0

我想使用此函數的stdin參數:http://graspjs.com/docs/lib/字符串變量爲stdin接口

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"); } 

回答

1

process.stdinReadable Streamgrasp期望的是它可以從中讀取數據的流。要模擬這種行爲,您可以使用PassThrough流:它是一種可以將緩衝區字符串寫入的流,並且可以將這些數據發送出去,就像任何可讀流一樣。

下面是一個使用示例:

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

grasp({ stdin: passthrough }); 
passthrough.push('some data'); 
passthrough.push('some other data'); 
passthrough.end(); 
+0

謝謝!它按預期工作。 – mquandalle

2

有了把握0.2.0,使用握作爲一個庫,當你現在可以使用新的input選項,或使用的兩個新的輔助功能之一:grasp.searchgrasp.replace。這將允許你做你想做的,而不必創建一個假的StdIn。

Documentation:http://graspjs.com/docs/lib/

+0

感謝您使用這項新功能@gkz :-) – mquandalle