Skip to content

Commit

Permalink
Close GH-154: Move syntax check functionality to an external lib file.
Browse files Browse the repository at this point in the history
  • Loading branch information
radkodinev authored and sindresorhus committed Aug 30, 2014
1 parent 81197ee commit a875fe0
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 53 deletions.
3 changes: 2 additions & 1 deletion AUTHORS
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
Sindre Sorhus (http://github.com/sindresorhus)
Sindre Sorhus (https://github.com/sindresorhus)
Radko Dinev (https://github.com/radkodinev)
51 changes: 51 additions & 0 deletions tasks/lib/check.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
'use strict';

var path = require('path');
var async = require('async');
var chalk = require('chalk');
var spawn = require('win-spawn');
var grunt = require('grunt');

module.exports = function (files, options, cb) {
var failCount = 0;
var filesToCheck = files.filter(function (src) {
return path.basename(src)[0] !== '_' && grunt.file.exists(src);
});

async.eachLimit(filesToCheck, options.numCPUs, function (src, next) {
var bin;
var args;

if (options.bundleExec) {
bin = 'bundle';
args = ['exec', 'sass', '--check', src];
} else {
bin = 'sass';
args = ['--check', src];
}

grunt.verbose.writeln('Command: ' + bin + ' ' + args.join(' '));

grunt.verbose.writeln('Checking file ' + chalk.cyan(src) + ' syntax.');
spawn(bin, args, { stdio: 'inherit' })
.on('error', grunt.warn)
.on('close', function (code) {
if (code > 0) {
failCount++;
grunt.log.error('Checking file ' + chalk.cyan(src) + ' - ' + chalk.red('failed') + '.');
} else {
grunt.verbose.ok('Checking file ' + chalk.cyan(src) + ' - ' + chalk.green('passed') + '.');
}

next();
});
}, function () {
if (failCount > 0) {
grunt.warn('Sass check failed for ' + failCount + ' files.');
} else {
grunt.log.ok('All ' + chalk.cyan(filesToCheck.length) + ' files passed.');
}

cb();
});
};
58 changes: 6 additions & 52 deletions tasks/sass.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
/*
* grunt-contrib-sass
* http://gruntjs.com/
*
* Copyright (c) 2013 Sindre Sorhus, contributors
* Licensed under the MIT license.
*/
'use strict';

var path = require('path');
var dargs = require('dargs');
var numCPUs = require('os').cpus().length || 1;
Expand All @@ -14,6 +8,8 @@ var chalk = require('chalk');
var spawn = require('win-spawn');
var which = require('which');

var checkFilesSyntax = require('./lib/check');

module.exports = function (grunt) {
var bannerCallback = function (filename, banner) {
grunt.verbose.writeln('Writing CSS banner for ' + filename);
Expand All @@ -31,50 +27,6 @@ module.exports = function (grunt) {
}
};

var checkFiles = function (files, options, cb) {
var failCount = 0;
var filesToCheck = files.filter(function (src) {
return path.basename(src)[0] !== '_' && grunt.file.exists(src);
});

async.eachLimit(filesToCheck, numCPUs, function (src, next) {
var bin;
var args;

if (options.bundleExec) {
bin = 'bundle';
args = ['exec', 'sass', '--check', src];
} else {
bin = 'sass';
args = ['--check', src];
}

grunt.verbose.writeln('Command: ' + bin + ' ' + args.join(' '));

grunt.verbose.writeln('Checking file ' + chalk.cyan(src) + ' syntax.');
spawn(bin, args, { stdio: 'inherit' })
.on('error', grunt.warn)
.on('close', function (code) {
if (code > 0) {
failCount++;
grunt.log.error('Checking file ' + chalk.cyan(src) + ' - ' + chalk.red('failed') + '.');
} else {
grunt.verbose.ok('Checking file ' + chalk.cyan(src) + ' - ' + chalk.green('passed') + '.');
}

next();
});
}, function () {
if (failCount > 0) {
grunt.warn('Sass check failed for ' + failCount + ' files.');
} else {
grunt.log.ok('All ' + chalk.cyan(filesToCheck.length) + ' files passed.');
}

cb();
});
};

grunt.registerMultiTask('sass', 'Compile Sass to CSS', function () {
var cb = this.async();
var options = this.options();
Expand All @@ -93,7 +45,9 @@ module.exports = function (grunt) {
}

if (options.check) {
checkFiles(this.filesSrc, options, cb);
options.numCPUs = numCPUs;

checkFilesSyntax(this.filesSrc, options, cb);
return;
}

Expand Down

0 comments on commit a875fe0

Please sign in to comment.