Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prepend and append single value with stream #735

Closed
ORESoftware opened this issue Jul 17, 2017 · 2 comments
Closed

Prepend and append single value with stream #735

ORESoftware opened this issue Jul 17, 2017 · 2 comments

Comments

@ORESoftware
Copy link

ORESoftware commented Jul 17, 2017

This regards Node.js versions >= 4.0.0

Say I have a file with this content:

var foo = 'bar';

I want to read this file in, wrap it in a function and replace "bar" with "star"

something like this =>

Test.create(function(){
   var foo = 'star';
});

so I can use a transform stream (replace stream to replace bar with star) like so:

fs.createReadStream(<file1>)
.pipe(new ReplaceStream('bar','star')).pipe(fs.createWriteStream(<file2>));

but my question is - what might be the recommended way to wrap the whole thing in a function? Is there a streamlined way that someone can think of do that?

Something like:

const writable = fs.createWriteStream(<file2>)
writable.write('Test.create(function(){');
fs.createReadStream(<file1>, {end:false})
.pipe(new ReplaceStream('bar','star')).pipe(writable, {end:false});
writable.end('})');

is that about right?

@ORESoftware
Copy link
Author

The problem with my code above, is I still get a "write after end" error...even though I have declared {end:false} in two places. Why would that be?

the error is caused by this line:
writable.end('})');

@ORESoftware
Copy link
Author

ORESoftware commented Jul 18, 2017

I used this, which seems to work:

    const writable = fs.createWriteStream(fileObj.mappedPath);
    writable.write("\nconst suman = require('suman');\n");
    writable.write("const Test = suman.init(module);\n\n");
    writable.write('Test.create(function(describe, it, before, after, beforeEach, afterEach){\n');

    fs.createReadStream(fileObj.originalPath)
        .pipe(new ReplaceStream('bar','star'))
        .pipe(writable)
        .once('finish', function () {
          fs.appendFile(fileObj.mappedPath, '\n\n});\n', cb);
        });

not that thrilled with having to use fs.appendFile at the end, but it should work, I think.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant