|
module.exports = function( grunt ) { |
|
"use strict"; |
|
|
|
var sassBuildArgsArray, |
|
sassCompile; |
|
|
|
sassBuildArgsArray = function( options ) { |
|
var args = [ 'sass' ]; |
|
|
|
for (var key in options) { |
|
if (options[key] === true) { |
|
args.push( '--' + key ); |
|
} |
|
} |
|
return args || []; |
|
}; |
|
|
|
sassCompile = function( args, cb ) { |
|
//grunt.log.writeflags( args ); |
|
var child = grunt.util.spawn({ |
|
cmd: args.shift(), |
|
args: args |
|
}, function (err, result, code) { |
|
if (code === 127) { |
|
return grunt.warn( |
|
'You need to have Ruby and Compass installed ' + |
|
'and in your system PATH for this task to work. ' + |
|
'More info: https://github.com/gruntjs/grunt-contrib-compass' |
|
); |
|
} |
|
// `compass compile` exits with 1 when it has nothing to compile |
|
// https://github.com/chriseppstein/compass/issues/993 |
|
cb((code === 0 || !/Nothing to compile/g.test(result.stdout)) || result.stderr); |
|
}); |
|
child.stdout.pipe(process.stdout); |
|
child.stderr.pipe(process.stderr); |
|
}; |
|
|
|
grunt.registerMultiTask('sass', 'Compile Sass to CSS.', function() { |
|
var args, options, cb, sassFolder, cssFolder; |
|
|
|
options = this.options(); |
|
cb = this.async(); |
|
|
|
if (options.sassFolder.charAt( options.sassFolder.length -1 ) === '/') { |
|
sassFolder = options.sassFolder; |
|
} else { |
|
sassFolder = options.sassFolder + '/'; |
|
} |
|
if (options.cssFolder.charAt( options.cssFolder.length -1 ) === '/') { |
|
cssFolder = options.cssFolder; |
|
} else { |
|
cssFolder = options.cssFolder + '/'; |
|
} |
|
|
|
args = sassBuildArgsArray( options ); |
|
|
|
this.files.forEach( function ( file ) { |
|
var argsFiles = [], filepath, files; |
|
|
|
filepath = file.orig.src.filter( function( filename ) { |
|
var filepath; |
|
|
|
filepath = sassFolder + filename; |
|
|
|
if (!grunt.file.exists( filepath )) { |
|
grunt.log.warn('Source file "' + filepath + '" not found.'); |
|
return false; |
|
} else { |
|
return filepath; |
|
} |
|
}); |
|
|
|
// copy array |
|
for (var i = 0; i < args.length; i++) { |
|
argsFiles[i] = args[i]; |
|
} |
|
|
|
files = sassFolder + filepath + ':' + cssFolder + file.dest; |
|
|
|
argsFiles.push( files ); |
|
sassCompile( argsFiles, cb); |
|
}); |
|
}); |
|
}; |