Search code examples
shellgulplibsass

How to execute a CLI program (libsass sassc) with Gulp.js -> create a stream, capture stdout and stderr


This is a setup for running the libsass "sassc" binary directly (shown below) using Gulp.js, for which I am currently using the plugin gulp-run. This was after much tinkering, as I'm not a node or gulp expert at all, and it still has a few problems.

The sassc binary normally takes input and output file arguments (e.g ../bin/sassc file.scss > file.css), but it can also be run with the -s argument to accept stdin, and so can be passed a file from gulp.src() as it is here. It seems under these conditions, to successfully pass on its result as stdout, if I understand correctly. A file called "test.css" is written in the same directory. Note that without the 'verbosity' setting on the gulp-run command as it is, the resulting CSS will be output to the terminal. Here that is suppressed and the result is passed down the chain; however this still does not seem to create a proper output stream because I've had no success adding processes such as gulp-autoprefixer inline after this. Furthermore, I need a way to capture stderr (if there is one) to a log file (right now if there's an error it outputs in the console).

Any guidance appreciated. I may also be using the wrong plugin for it with gulp-run. There are also gulp-exec and gulp-shell, and possibly others but I don't really know what difference to look for.

var gulp         = require('gulp');
var run          = require('gulp-run');
var rename       = require("gulp-rename");

gulp.task('sassc', function () {
  gulp.src('test.scss')
    .pipe(run('../bin/sassc -s', {verbosity: 1}))
    .on('error', function (err) { this.end(); })
    .pipe(rename(function (path) { path.extname = ".css"; }))
    .pipe(gulp.dest('.'))
});

gulp.task('watch', function () {
    gulp.watch('test.scss', ['sassc']);
});

gulp.task('default', ['watch']);

Solution

  • maybe gulp-log-capture (https://www.npmjs.com/package/gulp-log-capture) will be of some help in capturing stdout