From 7e7113888d649d54adcc0ab5791a39548bd93e59 Mon Sep 17 00:00:00 2001 From: Radko Dinev Date: Sat, 30 Aug 2014 09:55:11 +0300 Subject: [PATCH 1/3] Move syntax check functionality to an external lib file --- tasks/lib/check.js | 56 ++++++++++++++++++++++++++++++++++++++++++++++ tasks/sass.js | 50 +++++------------------------------------ 2 files changed, 61 insertions(+), 45 deletions(-) create mode 100644 tasks/lib/check.js diff --git a/tasks/lib/check.js b/tasks/lib/check.js new file mode 100644 index 0000000..d538bbf --- /dev/null +++ b/tasks/lib/check.js @@ -0,0 +1,56 @@ +'use strict'; + +var path = require('path'); +var async = require('async'); +var chalk = require('chalk'); +var spawn = require('win-spawn'); + +exports.init = function (grunt, numCPUs) { + var exports = {}; + + exports.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(); + }); + }; + + return exports; +}; diff --git a/tasks/sass.js b/tasks/sass.js index 600082a..d01bd2e 100644 --- a/tasks/sass.js +++ b/tasks/sass.js @@ -6,6 +6,7 @@ * Licensed under the MIT license. */ 'use strict'; + var path = require('path'); var dargs = require('dargs'); var numCPUs = require('os').cpus().length || 1; @@ -31,50 +32,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(); @@ -93,7 +50,10 @@ module.exports = function (grunt) { } if (options.check) { - checkFiles(this.filesSrc, options, cb); + require("./lib/check") + .init(grunt, numCPUs) + .checkFiles(this.filesSrc, options, cb); + return; } From 24aa90c6ebb7c93130a14d908fc7f5fe10eef447 Mon Sep 17 00:00:00 2001 From: Radko Dinev Date: Sat, 30 Aug 2014 15:50:12 +0300 Subject: [PATCH 2/3] Address code review comments --- AUTHORS | 3 +- tasks/lib/check.js | 93 ++++++++++++++++++++++------------------------ tasks/sass.js | 12 +----- 3 files changed, 48 insertions(+), 60 deletions(-) diff --git a/AUTHORS b/AUTHORS index 9608692..35bb56d 100644 --- a/AUTHORS +++ b/AUTHORS @@ -1 +1,2 @@ -Sindre Sorhus (http://github.com/sindresorhus) +Sindre Sorhus (https://github.com/sindresorhus) +Radko Dinev (https://github.com/radkodinev) diff --git a/tasks/lib/check.js b/tasks/lib/check.js index d538bbf..8df5206 100644 --- a/tasks/lib/check.js +++ b/tasks/lib/check.js @@ -4,53 +4,48 @@ var path = require('path'); var async = require('async'); var chalk = require('chalk'); var spawn = require('win-spawn'); - -exports.init = function (grunt, numCPUs) { - var exports = {}; - - exports.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(); - }); - }; - - return exports; +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(); + }); }; diff --git a/tasks/sass.js b/tasks/sass.js index d01bd2e..7719bb9 100644 --- a/tasks/sass.js +++ b/tasks/sass.js @@ -1,10 +1,3 @@ -/* - * grunt-contrib-sass - * http://gruntjs.com/ - * - * Copyright (c) 2013 Sindre Sorhus, contributors - * Licensed under the MIT license. - */ 'use strict'; var path = require('path'); @@ -50,10 +43,9 @@ module.exports = function (grunt) { } if (options.check) { - require("./lib/check") - .init(grunt, numCPUs) - .checkFiles(this.filesSrc, options, cb); + options.numCPUs = numCPUs; + require('./lib/check')(this.filesSrc, options, cb); return; } From aa3035aa4551fad045409597ca7453e825c90916 Mon Sep 17 00:00:00 2001 From: Radko Dinev Date: Sat, 30 Aug 2014 16:01:29 +0300 Subject: [PATCH 3/3] Address code review comments (2) --- tasks/sass.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tasks/sass.js b/tasks/sass.js index 7719bb9..1e6eea1 100644 --- a/tasks/sass.js +++ b/tasks/sass.js @@ -8,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); @@ -45,7 +47,7 @@ module.exports = function (grunt) { if (options.check) { options.numCPUs = numCPUs; - require('./lib/check')(this.filesSrc, options, cb); + checkFilesSyntax(this.filesSrc, options, cb); return; }