index.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*!
  2. * gulp-csscomb | https://github.com/koistya/gulp-csscomb
  3. * Copyright (c) Konstantin Tarkus (@koistya). See LICENSE.txt
  4. */
  5. 'use strict';
  6. var Comb = require('csscomb');
  7. var fs = require('fs');
  8. var gutil = require('gulp-util');
  9. var path = require('path');
  10. var through = require('through2');
  11. var PluginError = gutil.PluginError;
  12. // Constants
  13. var PLUGIN_NAME = 'gulp-csscomb';
  14. var SUPPORTED_EXTENSIONS = ['.css', '.sass', '.scss', '.less'];
  15. // Plugin level function (dealing with files)
  16. function Plugin(configPath, options) {
  17. if (arguments.length == 1 && typeof configPath === 'object') {
  18. options = configPath;
  19. configPath = options.configPath;
  20. } else if (arguments.length == 2 && typeof options === 'boolean') {
  21. options = { verbose: options }; // for backward compatibility
  22. }
  23. options = options || {};
  24. configPath = configPath || null;
  25. var verbose = options.verbose || false;
  26. //var lint = options.lint || false; // TODO: Report about found issues in style sheets
  27. // Create a stream through which each file will pass
  28. var stream = through.obj(function(file, enc, cb) {
  29. if (file.isNull()) {
  30. // Do nothing
  31. } else if (file.isStream()) {
  32. this.emit('error', new PluginError(PLUGIN_NAME, 'Streams are not supported!'));
  33. return cb();
  34. } else if (file.isBuffer() && SUPPORTED_EXTENSIONS.indexOf(path.extname(file.path)) !== -1) {
  35. if (verbose) {
  36. gutil.log(PLUGIN_NAME, 'Processing ' + gutil.colors.magenta(file.path));
  37. }
  38. if (configPath && !fs.existsSync(configPath)) {
  39. this.emit('error', new PluginError(PLUGIN_NAME, 'Configuration file not found: ' + gutil.colors.magenta(configPath)));
  40. return cb();
  41. }
  42. configPath = Comb.getCustomConfigPath(configPath || path.join(path.dirname(file.path), '.csscomb.json'));
  43. var config = Comb.getCustomConfig(configPath);
  44. if (verbose) {
  45. gutil.log(PLUGIN_NAME, 'Using configuration file ' + gutil.colors.magenta(configPath));
  46. }
  47. var comb = new Comb(config || 'csscomb');
  48. var syntax = options.syntax || file.path.split('.').pop();
  49. try {
  50. var output = comb.processString(
  51. file.contents.toString('utf8'), {
  52. syntax: syntax,
  53. filename: file.path
  54. });
  55. file.contents = new Buffer(output);
  56. } catch (err) {
  57. this.emit('error', new PluginError(PLUGIN_NAME, file.path + '\n' + err));
  58. }
  59. }
  60. // make sure the file goes through the next gulp plugin
  61. this.push(file);
  62. // tell the stream engine that we are done with this file
  63. return cb();
  64. });
  65. // Return the file stream
  66. return stream;
  67. }
  68. // Export the plugin main function
  69. module.exports = Plugin;