diff --git a/.editorconfig b/.editorconfig index 3f83807..a11603b 100644 --- a/.editorconfig +++ b/.editorconfig @@ -4,19 +4,8 @@ root = true [*] charset = utf-8 -indent_size = 4 +indent_size = 2 indent_style = space end_of_line = lf insert_final_newline = true trim_trailing_whitespace = true - -[*.md] -trim_trailing_whitespace = false -insert_final_newline = false - -[*.xml] -insert_final_newline = false - -[*.json] -insert_final_newline = false - diff --git a/.gitignore b/.gitignore index 55fda48..71415de 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,9 @@ + +.DS_Store +._* + + +.nyc_output/ node_modules npm-debug.log package-lock.json diff --git a/.travis.yml b/.travis.yml index ba91125..e2bb579 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,7 +7,5 @@ node_js: - 10 - 11 -after_script: - - NODE_ENV=test istanbul cover ./node_modules/mocha/bin/_mocha --report lcovonly -- -R spec - - cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js - - rm -rf ./coverage +after_success: + - npm run cover diff --git a/README.md b/README.md index 8252b28..25e1f72 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,7 @@ gulp-custom-callback ============= [![Build Status](https://travis-ci.org/Tiross/gulp-custom-callback.svg?branch=master)](https://travis-ci.org/Tiross/gulp-custom-callback) +[![Coverage Status](https://coveralls.io/repos/github/Tiross/gulp-custom-callback/badge.svg?branch=master)](https://coveralls.io/github/Tiross/gulp-custom-callback?branch=master) [![Greenkeeper badge](https://badges.greenkeeper.io/Tiross/gulp-custom-callback.svg)](https://greenkeeper.io/) Add own callback to streaming diff --git a/index.js b/index.js index cbfa363..532502d 100644 --- a/index.js +++ b/index.js @@ -1,90 +1,88 @@ -var through = require('through2'); -var gutil = require('gulp-util'); -var PluginError = gutil.PluginError; var lodash = require('lodash'); +var PluginError = require('plugin-error'); +var through = require('through2'); const PLUGIN_NAME = 'gulp-callback'; module.exports = function (transformFunction, flushFunction, options) { - - if (!transformFunction && !flushFunction) { - var message = [ - 'You have specified neither a valid transformFunction callback function', - 'nor a valid flushFunction callback function', - ].join(' '); - - throw new PluginError(PLUGIN_NAME, message); - } - - if (transformFunction && typeof transformFunction !== 'function') { - throw new PluginError(PLUGIN_NAME, 'transformFunction callback is not a function'); + if (!transformFunction && !flushFunction) { + var message = [ + 'You have specified neither a valid transformFunction callback function', + 'nor a valid flushFunction callback function', + ].join(' '); + + throw new PluginError(PLUGIN_NAME, message); + } + + if (transformFunction && typeof transformFunction !== 'function') { + throw new PluginError(PLUGIN_NAME, 'transformFunction callback is not a function'); + } + + if (flushFunction && typeof flushFunction !== 'function') { + if (!options && typeof flushFunction === 'object') { + options = flushFunction; + flushFunction = null; + } else if (options) { + throw new PluginError(PLUGIN_NAME, 'flushFunction callback is not a function'); } - - if (flushFunction && typeof flushFunction !== 'function') { - if (!options && typeof flushFunction === 'object') { - options = flushFunction; - flushFunction = null; - } else if (options) { - throw new PluginError(PLUGIN_NAME, 'flushFunction callback is not a function'); - } + } + + if (options && typeof options !== 'object') { + throw new PluginError(PLUGIN_NAME, 'options is not an options object'); + } + + options = lodash.defaults(options || {}, { + once: false + }); + var fireOnce = false; + var streamOptions = options.streamOptions = lodash.defaults(options.streamOptions || {}, { + objectMode: true + }); + + return through(streamOptions, function (file, enc, callback) { + var self = this; + + if (file.isNull() || file.isDirectory()) { + this.push(file); + return callback(); } - if (options && typeof options !== 'object') { - throw new PluginError(PLUGIN_NAME, 'options is not an options object'); + if (file.isStream()) { + return this.emit('error', new PluginError(PLUGIN_NAME, 'Streaming not supported')); } - options = lodash.defaults(options || {}, { - once: false - }); - var fireOnce = false; - var streamOptions = options.streamOptions = lodash.defaults(options.streamOptions || {}, { - objectMode: true - }); + var transformСallback = function (error, newFile, append) { + if (options.once) { + fireOnce = true; + } - return through(streamOptions, function (file, enc, callback) { - var self = this; + if (!error) { + if (newFile) { + self.push(newFile); - if (file.isNull() || file.isDirectory()) { - this.push(file); - return callback(); - } + if (append) { + self.push(file); + } - if (file.isStream()) { - return this.emit('error', new PluginError(PLUGIN_NAME, 'Streaming not supported')); - } - - var transformСallback = function (error, newFile, append) { - if (options.once) { - fireOnce = true; - } - - if (!error) { - if (newFile) { - self.push(newFile); - - if (append) { - self.push(file); - } - - callback(); - } else { - callback(null, file); - } - } else { - return self.emit('error', new PluginError(PLUGIN_NAME, error)); - } - }; - - if (!fireOnce) { - transformFunction.call(this, file, enc, transformСallback, options); + callback(); } else { - callback(null, file); + callback(null, file); } - }, function (callback) { - if (flushFunction) { - flushFunction.call(this, callback, options); - } else { - callback(); - } - }); + } else { + return self.emit('error', new PluginError(PLUGIN_NAME, error)); + } + }; + + if (!fireOnce) { + transformFunction.call(this, file, enc, transformСallback, options); + } else { + callback(null, file); + } + }, function (callback) { + if (flushFunction) { + flushFunction.call(this, callback, options); + } else { + callback(); + } + }); }; diff --git a/package.json b/package.json index e2d5ce3..da0609b 100644 --- a/package.json +++ b/package.json @@ -1,41 +1,41 @@ { - "name": "gulp-custom-callback", - "version": "2.0.2", - "description": "Custom callback for streams", - "license": "MIT", - "repository": "Tiross/gulp-custom-callback", - "author": { - "name": "Alexander Krasnoyarov", - "email": "info@itgalaxy.company", - "url": "http://itgalaxy.company" - }, - "engines": { - "node": ">=0.10.0" - }, - "scripts": { - "test": "mocha test" - }, - "files": [ - "index.js" - ], - "keywords": [ - "gulp", - "gulpplugin", - "gulpfriendly", - "callback", - "functions" - ], - "dependencies": { - "gulp-util": "~3.0.4", - "through2": "~2.0.3", - "lodash": "~4.17.2" - }, - "devDependencies": { - "mocha": "*", - "should": "*", - "vinyl": "*", - "event-stream": "*", - "istanbul": "~0.4.5", - "coveralls": "~3.0.0" - } + "name": "gulp-custom-callback", + "version": "2.0.2", + "description": "Custom callback for streams", + "license": "MIT", + "repository": "Tiross/gulp-custom-callback", + "author": { + "name": "Alexander Krasnoyarov", + "email": "info@itgalaxy.company", + "url": "http://itgalaxy.company" + }, + "engines": { + "node": ">=0.10.0" + }, + "scripts": { + "cover": "nyc report --reporter=text-lcov | coveralls", + "test": "nyc --reporter=text mocha" + }, + "files": [ + "index.js" + ], + "keywords": [ + "gulp", + "gulpplugin", + "gulpfriendly", + "callback", + "functions" + ], + "dependencies": { + "lodash": "^4.17.11", + "plugin-error": "^1.0.1", + "through2": "^3.0.1" + }, + "devDependencies": { + "coveralls": "^3.0.3", + "mocha": "^6.0.2", + "nyc": "^13.3.0", + "should": "^13.2.3", + "vinyl": "^2.2.0" + } } diff --git a/test/index.js b/test/index.js index 597a46c..cec15cb 100644 --- a/test/index.js +++ b/test/index.js @@ -6,357 +6,356 @@ var plugin = require('./../index'); var should = require('should'); var File = require('vinyl'); -var es = require('event-stream'); -var gutil = require('gulp-util'); var path = require('path'); +var PluginError = require('plugin-error'); var fs = require('fs'); const PLUGIN_NAME = 'gulp-callback'; function createVinyl(fileName, contents) { - var base = path.join(__dirname, 'fixtures'); - var filePath = path.join(base, fileName); - - return new gutil.File({ - cwd: __dirname, - base: base, - path: filePath, - contents: contents || fs.readFileSync(filePath) - }); + var base = path.join(__dirname, 'fixtures'); + var filePath = path.join(base, fileName); + + return new File({ + cwd: __dirname, + base: base, + path: filePath, + contents: contents || fs.readFileSync(filePath) + }); } describe('gulp-callback', function() { - it('should emit error when called plugin without arguments', function (done) { - var error = new gutil.PluginError(PLUGIN_NAME, 'You have specified neither a valid transformFunction callback function nor a valid flushFunction callback function'); - - (function () { - plugin(); - }).should.throw(error); - - done(); + it('should emit error when called plugin without arguments', function (done) { + var error = new PluginError(PLUGIN_NAME, 'You have specified neither a valid transformFunction callback function nor a valid flushFunction callback function'); + + (function () { + plugin(); + }).should.throw(error); + + done(); + }); + + it('should emit error when transformFunction is not function', function (done) { + var error = new PluginError(PLUGIN_NAME, 'transformFunction callback is not a function'); + + (function () { + plugin({}, {}); + }).should.throw(error); + + done(); + }); + + it('should emit error when flushFunction callback is not a function', function (done) { + var error = new PluginError(PLUGIN_NAME, 'flushFunction callback is not a function'); + + (function () { + plugin( + function () {}, + 'test', + {} + ); + }).should.throw(error); + + done(); + }); + + it('should pass when second argument is options', function (done) { + var fakeFile = createVinyl('fakefile'); + + var defaultOptions = { + testProperty: 'testProperty' + }; + var transformFunction = function (file, enc, callback, options) { + options.should.containDeep(defaultOptions); + callback(); + }; + var stream = plugin(transformFunction, defaultOptions); + + stream.on('data', function () {}); + + stream.once('end', done); + + stream.write(fakeFile); + stream.end(); + }); + + it('should emit error when options is not object', function (done) { + var error = new PluginError(PLUGIN_NAME, 'options is not an options object'); + + (function () { + plugin( + function () {}, + function () {}, + function () {} + ); + }).should.throw(error); + + done(); + }); + + it('should pass options.streamOptions equivalently streamOptions', function (done) { + var fakeFile = createVinyl('fakefile'); + + var transformFunction = function (file, enc, callback, options) { + callback(); + }; + var streamOptions = { + allowHalfOpen: true + }; + var stream = plugin(transformFunction, { + streamOptions: streamOptions }); - it('should emit error when transformFunction is not function', function (done) { - var error = new gutil.PluginError(PLUGIN_NAME, 'transformFunction callback is not a function'); - - (function () { - plugin({}, {}); - }).should.throw(error); + stream.once('data', function (data) { + this.allowHalfOpen.should.equal(streamOptions.allowHalfOpen) + }); - done(); - }); + stream.once('end', done); - it('should emit error when flushFunction callback is not a function', function (done) { - var error = new gutil.PluginError(PLUGIN_NAME, 'flushFunction callback is not a function'); + stream.write(fakeFile); + stream.end(); + }); - (function () { - plugin( - function () {}, - 'test', - {} - ); - }).should.throw(error); + it('should pass file when it isNull()', function (done) { + var transformFunction = function () { } + var stream = plugin(transformFunction); + var emptyFile = { + isNull: function () { return true; } + }; - done(); + stream.once('data', function (data) { + data.should.equal(emptyFile); }); - it('should pass when second argument is options', function (done) { - var fakeFile = createVinyl('fakefile'); + stream.once('end', done); - var defaultOptions = { - testProperty: 'testProperty' - }; - var transformFunction = function (file, enc, callback, options) { - options.should.containDeep(defaultOptions); - callback(); - }; - var stream = plugin(transformFunction, defaultOptions); + stream.write(emptyFile); + stream.end(); + }); - stream.on('data', function () {}); + it('should pass file when it isDirectory()', function (done) { + var transformFunction = function () { } + var stream = plugin(transformFunction); + var emptyFile = { + isNull: function () { return false; }, + isDirectory: function () { return true; } + }; - stream.once('end', done); - - stream.write(fakeFile); - stream.end(); + stream.once('data', function (data) { + data.should.equal(emptyFile); }); - it('should emit error when options is not object', function (done) { - var error = new gutil.PluginError(PLUGIN_NAME, 'options is not an options object'); + stream.once('end', done); - (function () { - plugin( - function () {}, - function () {}, - function () {} - ); - }).should.throw(error); + stream.write(emptyFile); + stream.end(); + }); - done(); - }); + it('should emit error when file isStream()', function (done) { + var transformFunction = function () { } + var stream = plugin(transformFunction); + var streamFile = { + isNull: function () { return false; }, + isDirectory: function () { return false; }, + isStream: function () { return true; } + }; - it('should pass options.streamOptions equivalently streamOptions', function (done) { - var fakeFile = createVinyl('fakefile'); + stream.on('error', function (error) { + error.message.should.equal('Streaming not supported'); + done(); + }); - var transformFunction = function (file, enc, callback, options) { - callback(); - }; - var streamOptions = { - allowHalfOpen: true - }; - var stream = plugin(transformFunction, { - streamOptions: streamOptions - }); + stream.write(streamFile); + stream.end(); + }); - stream.once('data', function (data) { - this.allowHalfOpen.should.equal(streamOptions.allowHalfOpen) - }); + it('should pass with only transformFunction', function (done) { + var fakeFile = createVinyl('fakefile'); - stream.once('end', done); + var transformFunction = function (file, enc, callback, options) { + callback(); + }; + var stream = plugin(transformFunction); - stream.write(fakeFile); - stream.end(); + stream.on('data', function (data) { + data.contents.toString().should.equal("streamwiththosecontents\n"); }); - it('should pass file when it isNull()', function (done) { - var transformFunction = function () { } - var stream = plugin(transformFunction); - var emptyFile = { - isNull: function () { return true; } - }; - - stream.once('data', function (data) { - data.should.equal(emptyFile); - }); + stream.once('end', done); - stream.once('end', done); + stream.write(fakeFile); + stream.end(); + }); - stream.write(emptyFile); - stream.end(); + it('should pass with transformFunction with multiple files', function (done) { + var files = [ + createVinyl('fakefile'), + createVinyl('fakefile1') + ].map(function (file) { + return file; }); - it('should pass file when it isDirectory()', function (done) { - var transformFunction = function () { } - var stream = plugin(transformFunction); - var emptyFile = { - isNull: function () { return false; }, - isDirectory: function () { return true; } - }; + var transformFunction = function (file, enc, callback, options) { + callback(); + }; + var stream = plugin(transformFunction); - stream.once('data', function (data) { - data.should.equal(emptyFile); - }); + stream.once('end', done); - stream.once('end', done); - - stream.write(emptyFile); - stream.end(); + stream.on('data', function (data) { + if (path.basename(data.path) === 'fakefile') { + data.contents.toString().should.equal("streamwiththosecontents\n"); + } + if (path.basename(data.path) === 'fakefile1') { + data.contents.toString().should.equal("streamwiththosecontents1\n"); + } }); - it('should emit error when file isStream()', function (done) { - var transformFunction = function () { } - var stream = plugin(transformFunction); - var streamFile = { - isNull: function () { return false; }, - isDirectory: function () { return false; }, - isStream: function () { return true; } - }; - - stream.on('error', function (error) { - error.message.should.equal('Streaming not supported'); - done(); - }); - - stream.write(streamFile); - stream.end(); + files.forEach(function (file) { + stream.write(file); }); - it('should pass with only transformFunction', function (done) { - var fakeFile = createVinyl('fakefile'); - - var transformFunction = function (file, enc, callback, options) { - callback(); - }; - var stream = plugin(transformFunction); - - stream.on('data', function (data) { - data.contents.toString().should.equal("streamwiththosecontents\n"); - }); + stream.end(); + }); - stream.once('end', done); - - stream.write(fakeFile); - stream.end(); + it('should pass with transformFunction with once true arguments', function (done) { + var files = [ + createVinyl('fakefile'), + createVinyl('fakefile1') + ].map(function (file) { + return file; }); - it('should pass with transformFunction with multiple files', function (done) { - var files = [ - createVinyl('fakefile'), - createVinyl('fakefile1') - ].map(function (file) { - return file; - }); - - var transformFunction = function (file, enc, callback, options) { - callback(); - }; - var stream = plugin(transformFunction); - - stream.once('end', done); - - stream.on('data', function (data) { - if (path.basename(data.path) === 'fakefile') { - data.contents.toString().should.equal("streamwiththosecontents\n"); - } - if (path.basename(data.path) === 'fakefile1') { - data.contents.toString().should.equal("streamwiththosecontents1\n"); - } - }); - - files.forEach(function (file) { - stream.write(file); - }); - - stream.end(); + var countFired = 0; + var transformFunction = function (file, enc, callback, options) { + countFired++; + callback(); + }; + var stream = plugin(transformFunction, { + once: true }); - it('should pass with transformFunction with once true arguments', function (done) { - var files = [ - createVinyl('fakefile'), - createVinyl('fakefile1') - ].map(function (file) { - return file; - }); - - var countFired = 0; - var transformFunction = function (file, enc, callback, options) { - countFired++; - callback(); - }; - var stream = plugin(transformFunction, { - once: true - }); - - stream.on('data', function () { }); - - stream.on('end', function () { - countFired.should.equal(1); - done(); - }); - - files.forEach(function (file) { - stream.write(file); - }); - - stream.end(); + stream.on('data', function () { }); + + stream.on('end', function () { + countFired.should.equal(1); + done(); }); - it('should emit error when in transformFunction if pass first(error) argument', function (done) { - var fakeFile = createVinyl('fakefile'); - var error = new gutil.PluginError(PLUGIN_NAME, 'test error'); + files.forEach(function (file) { + stream.write(file); + }); - var transformFunction = function (file, enc, callback, options) { - callback('test error'); - }; - var stream = plugin(transformFunction); + stream.end(); + }); + it('should emit error when in transformFunction if pass first(error) argument', function (done) { + var fakeFile = createVinyl('fakefile'); + var error = new PluginError(PLUGIN_NAME, 'test error'); - stream.on('error', function (error) { - error.message.should.equal('test error'); - done(); - }); + var transformFunction = function (file, enc, callback, options) { + callback('test error'); + }; + var stream = plugin(transformFunction); - stream.write(fakeFile); - stream.end(); + stream.on('error', function (error) { + error.message.should.equal('test error'); + done(); }); - it('should pass when in transformFunction second argument new file', function (done) { - var fakeFile = createVinyl('fakefile'); + stream.write(fakeFile); - var transformFunction = function (file, enc, callback, options) { - var fakeFile = createVinyl('fakefile1'); - callback(null, fakeFile); - }; - var stream = plugin(transformFunction); + stream.end(); + }); - stream.once('end', done); + it('should pass when in transformFunction second argument new file', function (done) { + var fakeFile = createVinyl('fakefile'); - stream.on('data', function (data) { - data.contents.toString().should.equal("streamwiththosecontents1\n"); - }); + var transformFunction = function (file, enc, callback, options) { + var fakeFile = createVinyl('fakefile1'); + callback(null, fakeFile); + }; + var stream = plugin(transformFunction); - stream.write(fakeFile); + stream.once('end', done); - stream.end(); + stream.on('data', function (data) { + data.contents.toString().should.equal("streamwiththosecontents1\n"); }); - it('should pass when in transformFunction second argument new file and third append true', function (done) { - var fakeFile = createVinyl('fakefile'); + stream.write(fakeFile); - var transformFunction = function (file, enc, callback, options) { - var fakeFile1 = createVinyl('fakefile1'); - callback(null, fakeFile1, true); - }; - var stream = plugin(transformFunction); + stream.end(); + }); - stream.once('end', done); + it('should pass when in transformFunction second argument new file and third append true', function (done) { + var fakeFile = createVinyl('fakefile'); - stream.on('data', function (data) { - if (path.basename(data.path) === 'fakefile') { - data.contents.toString().should.equal("streamwiththosecontents\n"); - } - if (path.basename(data.path) === 'fakefile1') { - data.contents.toString().should.equal("streamwiththosecontents1\n"); - } - }); + var transformFunction = function (file, enc, callback, options) { + var fakeFile1 = createVinyl('fakefile1'); + callback(null, fakeFile1, true); + }; + var stream = plugin(transformFunction); - stream.write(fakeFile); + stream.once('end', done); - stream.end(); + stream.on('data', function (data) { + if (path.basename(data.path) === 'fakefile') { + data.contents.toString().should.equal("streamwiththosecontents\n"); + } + if (path.basename(data.path) === 'fakefile1') { + data.contents.toString().should.equal("streamwiththosecontents1\n"); + } }); - it('should pass with transformFunction and flushFunction', function (done) { - var fakeFile = createVinyl('fakefile'); + stream.write(fakeFile); - var transformFunction = function (file, enc, callback, options) { - callback(); - }; - var flushFunction = function (callback, options) { - this.push(fakeFile); - callback(); - }; - var stream = plugin(transformFunction, flushFunction); + stream.end(); + }); - stream.on('data', function (data) { - data.contents.toString().should.equal("streamwiththosecontents\n"); - }); + it('should pass with transformFunction and flushFunction', function (done) { + var fakeFile = createVinyl('fakefile'); - stream.once('end', done); + var transformFunction = function (file, enc, callback, options) { + callback(); + }; + var flushFunction = function (callback, options) { + this.push(fakeFile); + callback(); + }; + var stream = plugin(transformFunction, flushFunction); - stream.write(fakeFile); - stream.end(); + stream.on('data', function (data) { + data.contents.toString().should.equal("streamwiththosecontents\n"); }); - it('should pass options equal arguments options in flushFunction', function (done) { - var fakeFile = createVinyl('fakefile'); + stream.once('end', done); - var optionsDefault = { - testProperty: 'testProperty' - }; - var transformFunction = function (file, enc, callback, options) { - callback(); - }; - var flushFunction = function (callback, options) { - options.should.containDeep(optionsDefault); - callback(); - }; - var stream = plugin(transformFunction, flushFunction, optionsDefault); + stream.write(fakeFile); + stream.end(); + }); - stream.on('data', function () { }); + it('should pass options equal arguments options in flushFunction', function (done) { + var fakeFile = createVinyl('fakefile'); - stream.once('end', done); + var optionsDefault = { + testProperty: 'testProperty' + }; + var transformFunction = function (file, enc, callback, options) { + callback(); + }; + var flushFunction = function (callback, options) { + options.should.containDeep(optionsDefault); + callback(); + }; + var stream = plugin(transformFunction, flushFunction, optionsDefault); - stream.write(fakeFile); - stream.end(); - }); + stream.on('data', function () { }); + + stream.once('end', done); + + stream.write(fakeFile); + stream.end(); + }); });