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

Incorrect handling of binary content from stdout #43

Closed
Glebka opened this issue Jun 3, 2018 · 2 comments
Closed

Incorrect handling of binary content from stdout #43

Glebka opened this issue Jun 3, 2018 · 2 comments

Comments

@Glebka
Copy link
Contributor

Glebka commented Jun 3, 2018

Hi.

gulp-exec plugin corrupts binary data that come from stdout. See example gulpfile.js below.

gulp.task("extractAlpha", function() {
  var options = {
    continueOnError: false,
    pipeStdout: true,
    encoding: 'binary'
  };
  return gulp
    .src("./*.png")
    .pipe(
        exec(
          "convert <%= file.path %> -alpha extract -",
          options
        )
      )    
    .pipe(gulp.dest('out/'));    
});

Here you can see extractAlpha task that uses convert CLI tool from ImageMagick distribution. It extracts alpha channel as a separate image and outputs the result to the stdout.
When I run this task I get corrupted image in the output folder. Note, that I've specified encoding: 'binary' option for exec but it doesn't help.

The root cause of the issue is here:

file.contents = new Buffer(stdout); // FRAGILE: if it wasn't a buffer it is now

Providing an encoding when creating a Buffer should fix the issue.

Will create PR for this soon.

This issue occurs on:
Windows 8.1, NodeJS v8.11.2

Glebka added a commit to Glebka/gulp-exec that referenced this issue Jun 3, 2018
@robrich
Copy link
Owner

robrich commented Jun 3, 2018

Thanks for the PR. It also sounds like you've outgrown gulp-exec as gulp spends a lot of time loading the file contents and piping things around which you're not using. You may want to swap to:

const exec = require('child_process').exec;
const glob = require('glob');
const promisify = require('util').promisify;

const execAsPromise = promisify(exec);

gulp.task("extractAlpha", function(cb) {
  glob("./*.png", function (err, files) {
    let promises = files.map(file => execAsPromise(`convert ${file} -alpha extract > out/${file}`));
    Promise.all(promises).then(cb);
  });
});

@Glebka
Copy link
Contributor Author

Glebka commented Jun 3, 2018

Thanks for advice :) I am new to gulp and just playing with it to understand how it works.
Well, actually I am trying to modify the code of that guy because he uses make for generating shapes from png images. Just trying to replace make with gulp because I am too lazy to install make on Windows :)

@Glebka Glebka closed this as completed Jun 12, 2018
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

2 participants