Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 1 addition & 12 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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

6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@

.DS_Store
._*


.nyc_output/
node_modules
npm-debug.log
package-lock.json
6 changes: 2 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
146 changes: 72 additions & 74 deletions index.js
Original file line number Diff line number Diff line change
@@ -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();
}
});
};
78 changes: 39 additions & 39 deletions package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
Loading