compass.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * grunt-contrib-compass
  3. * http://gruntjs.com/
  4. *
  5. * Copyright (c) 2014 Sindre Sorhus, contributors
  6. * Licensed under the MIT license.
  7. */
  8. 'use strict';
  9. var binVersionCheck = require('bin-version-check');
  10. module.exports = function (grunt) {
  11. var compass = require('./lib/compass').init(grunt);
  12. function compile(args, cb) {
  13. var child = grunt.util.spawn({
  14. cmd: args.shift(),
  15. args: args
  16. }, function (err, result, code) {
  17. if (code === 127) {
  18. grunt.warn(
  19. 'You need to have Ruby and Compass installed ' +
  20. 'and in your system PATH for this task to work. ' +
  21. 'More info: https://github.com/gruntjs/grunt-contrib-compass'
  22. );
  23. }
  24. // `compass compile` exits with 1 and outputs "Nothing to compile"
  25. // on stderr when it has nothing to compile.
  26. // https://github.com/chriseppstein/compass/issues/993
  27. // Don't fail the task in this situation.
  28. if (code === 1 && !/Nothing to compile|Compass can't find any Sass files to compile/g.test(result.stderr)) {
  29. grunt.warn('↑');
  30. }
  31. cb();
  32. });
  33. if (child) {
  34. child.stdout.pipe(process.stdout);
  35. child.stderr.pipe(process.stderr);
  36. }
  37. }
  38. grunt.registerMultiTask('compass', 'Compile Sass to CSS using Compass', function () {
  39. var options = this.options();
  40. var cb = this.async();
  41. // display compilation time
  42. if (!options.clean) {
  43. options.time = true;
  44. }
  45. // create a function to retroactively add a banner to the top of the
  46. // generated files, if specified
  47. var bannerCallback = compass.buildBannerCallback(grunt, options);
  48. // create a temporary config file if there are 'raw' options or
  49. // settings not supported as CLI arguments
  50. var configContext = compass.buildConfigContext(options);
  51. // get the array of arguments for the compass command
  52. var args = compass.buildArgsArray(options);
  53. configContext(function (err, path) {
  54. if (err) {
  55. grunt.warn(err);
  56. }
  57. if (path) {
  58. args.push('--config', path);
  59. }
  60. binVersionCheck(args[0], '>=0.12.2', function (err) {
  61. if (err) {
  62. grunt.warn(err);
  63. }
  64. compile(args, function () {
  65. bannerCallback();
  66. cb();
  67. });
  68. });
  69. });
  70. });
  71. };