项目作者: dutchenkoOleg

项目描述 :
Check the file before process it in your Gulp plugin
高级语言: JavaScript
项目地址: git://github.com/dutchenkoOleg/gulp-not-supported-file.git
创建时间: 2017-05-24T15:02:25Z
项目社区:https://github.com/dutchenkoOleg/gulp-not-supported-file

开源协议:MIT License

下载


gulp-not-supported-file

npm
es2015-blue.svg)
license
Build Status

Not a Gulp plugin,
but for Gulp plugin developers.
Check the file before process it in your Gulp plugin

js-happiness-style

What is this and why it was created?

Most of Gulp plugins for compiling/rendering static files use through2 for processing. And first step of each code is a testing file
- it is not null
- it is not stream
- it is not …
And after this checkouts we may work with file.

Little example

  1. const through2 = require('through2');
  2. const PluginError = require('plugin-error');
  3. const PLUGIN_NAME = 'my-plugin';
  4. function myGulpPLugin(options) {
  5. // process options if need
  6. // ...
  7. // processing
  8. return through2.obj(function(file, enc, cb) {
  9. if (file.isNull()) {
  10. return cb(null, file);
  11. }
  12. if (file.isStream()) {
  13. return cb(new PluginError(PLUGIN_NAME, 'Streaming not supported'));
  14. }
  15. if (!file.contents.length) {
  16. return cb(null, file);
  17. }
  18. // and other if and if
  19. // ...
  20. // and then work with it
  21. });
  22. }

I’m tired of writing the same code every time.
So I wrote it once and wrapped it in a tiny module.


How it works

Call this module with your file and with your plugin error handler. Module will return result:

  • false if the file is suitable for work
  • Array if the file failed the test. Array will contain arguments. First of them is text status name of fail and next arguments for through2 callback.

Status list

  • 'isDirectory' - will be error
  • 'isNull' - will be error
  • 'isStream' - will be error
  • 'isEmpty' - skip file
  • 'isUnderscore' - skip file

Usage example

  1. const through2 = require('through2');
  2. const PluginError = require('plugin-error');
  3. const PLUGIN_NAME = 'my-plugin';
  4. const notSupportedFile = require('gulp-not-supported-file');
  5. // ---------------------------
  6. // private method plugin error
  7. function pluginError (data, errorOptions) {
  8. return new PluginError(PLUGIN_NAME, data, errorOptions);
  9. }
  10. // core plugin method
  11. function myGulpPlugin(options) {
  12. // process options if need
  13. // ...
  14. // processing
  15. return through2.obj(function (file, enc, cb) {
  16. let notSupported = notSupportedFile(file, pluginError);
  17. if (Array.isArray(notSupported)) {
  18. notSupported.shift(); // or with saving -> let failStatus = notSupported.shift();
  19. return cb(...notSupported); // or es5 apply -> cb.apply(null, notSupported);
  20. }
  21. // work with file if passed
  22. // ...
  23. });
  24. }
  25. module.exports = myGulpPlugin;

Module also has few options

Options are passed by the third argument and must be an object

  1. let notSupported = notSupportedFile(file, pluginError, options);

noUnderscore

type boolean /
default true

File with empty content will be skipped and not using in stream next.

You will receive message in console if it happens
Example of log:

no-empty log example

noEmpty

type boolean /
default true

File with empty content will be skipped and not using in stream next.
Return ['isEmpty']
Note! Spaces, tabs and newlines will be treated as empty content.

You will receive message in console if it happens_stream next.
Example of log:

no-empty log example

silent

type boolean /
default false

No logs about noEmpty and noUnderscore files


Installing

  1. npm install --save gulp-not-supported-file
  2. # or using yarn cli
  3. yarn add gulp-not-supported-file

Tests

  1. npm test for testing code style and run mocha tests
  2. npm run happiness-fix for automatically fix most of problems with code style

Changelog

Please read CHANGELOG.md

Contributing

Please read CONTRIBUTING.md