Skip to content

Instantly share code, notes, and snippets.

@tobie
Created November 25, 2014 16:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tobie/d4878fc1441a5f6199df to your computer and use it in GitHub Desktop.
Save tobie/d4878fc1441a5f6199df to your computer and use it in GitHub Desktop.
Checking input file exists and isn't empty before browserifying.
var fs = require("fs");
var browserify = require("browserify");
var through2 = require("through2");
module.exports = function(input, output, callback) {
var inputstream = fs.createReadStream(input).on("error", callback);
var isEmpty = through2(
function (chunk, enc, cb) {
if (this._notEmpty || (/\S/).test(chunk)) this._notEmpty = true;
cb(null, chunk);
},
function (cb) {
if (this._notEmpty) cb();
else cb(new Error('Empty File: "' + input + '".'));
}
).on("error", callback);
browserify(inputstream.pipe(isEmpty), {}).bundle()
.on("error", callback)
.pipe(fs.createWriteStream(output))
.on("error", callback)
.on("finish", callback);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment