0

I'd like to use the stdin parameter of this function: http://graspjs.com/docs/lib/.

The grasp function expects this parameter to be an object with the same interface that process.stdin. And what I have is a simple variable in memory of type string.

How can I give this variable to the stdin input of this function?

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

Expected log:

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

2 Answers 2

2

With Grasp 0.2.0, you can now use the new input option when using Grasp as a library, or use one of the two new helper functions: grasp.search and grasp.replace. This will allow you to do what you want without having to create a fake StdIn.

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for this new functionality @gkz :-)
1

process.stdin is a Readable Stream. What grasp expects is a stream that it can read data from. To simulate this behavior, you can use a PassThrough stream: it's a stream that you can write strings of buffer to, and which will emit this data out, as any readable stream would.

Here is an usage example:

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.