UNPKG

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