1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | 'use strict';
|
9 | var path = require('path');
|
10 | var dargs = require('dargs');
|
11 | var numCPUs = require('os').cpus().length;
|
12 | var async = require('async');
|
13 | var chalk = require('chalk');
|
14 | var spawn = require('win-spawn');
|
15 |
|
16 | module.exports = function (grunt) {
|
17 | var bannerCallback = function (filename, banner) {
|
18 | var content;
|
19 | grunt.log.verbose.writeln('Writing CSS banner for ' + filename);
|
20 |
|
21 | content = grunt.file.read(filename);
|
22 | grunt.file.write(filename, banner + grunt.util.linefeed + content);
|
23 | };
|
24 |
|
25 | grunt.registerMultiTask('sass', 'Compile Sass to CSS', function () {
|
26 | var cb = this.async();
|
27 | var options = this.options();
|
28 | var passedArgs;
|
29 | var bundleExec;
|
30 | var banner;
|
31 |
|
32 |
|
33 | if (options.banner) {
|
34 | banner = options.banner;
|
35 | delete options.banner;
|
36 | }
|
37 |
|
38 | passedArgs = dargs(options, ['bundleExec']);
|
39 | bundleExec = options.bundleExec;
|
40 |
|
41 | async.eachLimit(this.files, numCPUs, function (file, next) {
|
42 | var src = file.src[0];
|
43 | if (typeof src !== 'string') {
|
44 | src = file.orig.src[0];
|
45 | }
|
46 |
|
47 | if (!grunt.file.exists(src)) {
|
48 | grunt.log.warn('Source file "' + src + '" not found.');
|
49 | return next();
|
50 | }
|
51 |
|
52 | if (path.basename(src)[0] === '_') {
|
53 | return next();
|
54 | }
|
55 |
|
56 | var args = [
|
57 | src,
|
58 | file.dest,
|
59 | '--load-path', path.dirname(src)
|
60 | ].concat(passedArgs);
|
61 |
|
62 | if (bundleExec) {
|
63 | args.unshift('bundle', 'exec');
|
64 | }
|
65 |
|
66 |
|
67 | if (path.extname(src) === '.css') {
|
68 | args.push('--scss');
|
69 | }
|
70 |
|
71 |
|
72 | grunt.file.write(file.dest, '');
|
73 |
|
74 | var cp = spawn('sass', args, {stdio: 'inherit'});
|
75 |
|
76 | cp.on('error', function (err) {
|
77 | grunt.warn(err);
|
78 | });
|
79 |
|
80 | cp.on('close', function (code) {
|
81 | if (code === 127) {
|
82 | return grunt.warn(
|
83 | 'You need to have Ruby and Sass installed and in your PATH for\n' +
|
84 | 'this task to work. More info:\n' +
|
85 | 'https://github.com/gruntjs/grunt-contrib-sass'
|
86 | );
|
87 | }
|
88 |
|
89 | if (code > 0) {
|
90 | return grunt.warn('Exited with error code ' + code);
|
91 | }
|
92 |
|
93 |
|
94 | if (banner) {
|
95 | bannerCallback(file.dest, banner);
|
96 | }
|
97 |
|
98 | grunt.log.writeln('File ' + chalk.cyan(file.dest) + ' created.');
|
99 | next();
|
100 | });
|
101 | }, cb);
|
102 | });
|
103 | };
|