-
Notifications
You must be signed in to change notification settings - Fork 141
/
sass.js
109 lines (91 loc) · 2.98 KB
/
sass.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
'use strict';
var path = require('path');
var os = require('os');
var dargs = require('dargs');
var async = require('async');
var chalk = require('chalk');
var spawn = require('cross-spawn');
var which = require('which');
var checkFilesSyntax = require('./lib/check');
var concurrencyCount = (os.cpus().length || 1) * 4;
module.exports = function (grunt) {
var checkBinary = function (cmd, errMsg) {
try {
which.sync(cmd);
} catch (err) {
return grunt.warn(
'\n' + errMsg + '\n' +
'More info: https://github.com/gruntjs/grunt-contrib-sass\n'
);
}
};
grunt.registerMultiTask('sass', 'Compile Sass to CSS', function () {
var cb = this.async();
var options = this.options();
if (options.bundleExec) {
checkBinary('bundle',
'bundleExec options set but no Bundler executable found in your PATH.'
);
} else {
checkBinary('sass',
'You need to have Ruby and Sass installed and in your PATH for this task to work.'
);
}
if (options.check) {
options.concurrencyCount = concurrencyCount;
checkFilesSyntax(this.filesSrc, options, cb);
return;
}
var passedArgs = dargs(options, {
excludes: ['bundleExec'],
ignoreFalse: true
});
async.eachLimit(this.files, concurrencyCount, function (file, next) {
var src = file.src[0];
if (path.basename(src)[0] === '_') {
return next();
}
var args = [
src,
file.dest
].concat(passedArgs);
if (options.update) {
// When the source file hasn't yet been compiled SASS will write an empty file.
// If this is the first time the file has been written we treat it as if `update` was not passed
if (!grunt.file.exists(file.dest)) {
// Find where the --update flag is and remove it
args.splice(args.indexOf('--update'), 1);
} else {
// The first two elements in args are the source and destination files,
// which are used to build a path that SASS recognizes, i.e. "source:destination"
args.push(args.shift() + ':' + args.shift());
}
}
var bin = 'sass';
if (options.bundleExec) {
bin = 'bundle';
args.unshift('exec', 'sass');
}
// If we're compiling scss or css files
if (path.extname(src) === '.css') {
args.push('--scss');
}
// Make sure grunt creates the destination folders if they don't exist
if (!grunt.file.exists(file.dest)) {
grunt.file.write(file.dest, '');
}
grunt.verbose.writeln('Command: ' + bin + ' ' + args.join(' '));
var cp = spawn(bin, args, {stdio: 'inherit'});
cp.on('error', grunt.warn);
cp.on('close', function (code) {
if (code > 0) {
grunt.warn('Exited with error code ' + code);
next();
return;
}
grunt.verbose.writeln('File ' + chalk.cyan(file.dest) + ' created');
next();
});
}, cb);
});
};