diff --git a/README.md b/README.md index 2c2c8fe..66a5b47 100644 --- a/README.md +++ b/README.md @@ -49,9 +49,15 @@ A string value that is used to specify custom-sort-order.json file path. ```js grunt.initConfig({ - csscomb: { + foo : { + files: { + 'dest/resorted-foo.css': ['src/foo.css'], + }, + }, + bar : { files: { 'dest/resorted-foo.css': ['src/foo.css'], + 'dest/resorted-bar.css': ['src/bar.css'], }, }, }) @@ -76,7 +82,7 @@ grunt.initConfig({ ## Release History -+ v0.5.0: Add error handling. ++ v0.5.0: Enable multiple files. + v0.4.0: Move to csscomb's repository. + v0.3.0: Fix sort option bug. + v0.2.0: Fix bugs. diff --git a/package.json b/package.json index 78f1dde..344f5b9 100644 --- a/package.json +++ b/package.json @@ -28,6 +28,9 @@ "scripts": { "test": "grunt test" }, + "dependencies": { + "async": "~0.2.9" + }, "devDependencies": { "grunt-contrib-jshint": "~0.6.0", "grunt-contrib-clean": "~0.4.0", diff --git a/tasks/csscomb.js b/tasks/csscomb.js index 8d3c8fa..51993f7 100644 --- a/tasks/csscomb.js +++ b/tasks/csscomb.js @@ -6,61 +6,62 @@ * Licensed under the MIT license. */ 'use strict'; -module.exports = function(grunt) { +module.exports = function (grunt) { - grunt.registerMultiTask('csscomb', 'Sorting CSS properties in specific order.', function() { - var fs = require('fs'), + grunt.registerMultiTask('csscomb', 'Sorting CSS properties in specific order.', function () { + + var async = require('async'), + fs = require('fs'), path = require('path'), - exec = require('child_process').exec; - var command, - done = this.async(), realPath = path.dirname(fs.realpathSync(__filename)), - cssComb = 'php ' + realPath + '/lib/csscomb.php', - fileSrc = '', - fileDest = '', - fileSort = '', - options = this.options({ - sortOrder: null - }); + done = this.async(); - if (options.sortOrder !== null) { - if (grunt.file.exists(options.sortOrder)) { - fileSort = ' -s ' + options.sortOrder; - } else { - grunt.log.error('Custom sort .json file not found.'); - return false; - } - } + async.eachSeries(this.files, function (file, next) { + var args = [], + child = { + cmd: 'php', + args: args + }, + options = grunt.task.current.options({ + sortOrder: null + }); - function puts(error, stdout, stderr) { - if (error !== null) { - grunt.log.error(error); + args.push(realPath + '/lib/csscomb.php'); - } else { - grunt.log.ok(stdout); + if (options.sortOrder !== null) { + if (grunt.file.exists(options.sortOrder)) { + args.push('-s', options.sortOrder); + } else { + grunt.log.error('Custom sort .json file not found.'); + return false; + } } - } - this.files.forEach(function(file) { - fileSrc = file.src.filter(function(filepath) { + var fileSrc = file.src.filter(function (filepath) { // Remove nonexistent files (it's up to you to filter or warn here). if (!grunt.file.exists(filepath)) { grunt.log.warn('Source file "' + filepath + '" not found.'); return false; } else { - return true; + return filepath; } - }).map(function(filepath) { - return filepath; }); - fileSrc = ' -i ' + fileSrc; + args.push('-i', fileSrc); + if (file.dest !== null) { - fileDest = ' -o ' + file.dest; + args.push('-o', file.dest); } - command = cssComb + fileSort + fileSrc + fileDest; - exec(command, puts); - grunt.verbose.writeln('`' + command + '` was initiated.'); - }); - + + grunt.util.spawn(child, function (error, result, code) { + if (error !== null) { + grunt.log.error(error); + } else { + grunt.log.ok(result); + next(); + } + }); + grunt.verbose.ok('`php ' + child.args.join(' ') + '` was initiated.'); + }, done); + }); }; \ No newline at end of file diff --git a/test/fixtures/multi1.css b/test/fixtures/multi1.css index 3c53943..c3944af 100644 --- a/test/fixtures/multi1.css +++ b/test/fixtures/multi1.css @@ -1,13 +1,13 @@ .multi1 { - text-align: center; - border-color: 1px #000 solid; - background-color: red; - height: 100px; + z-index: 100; display: block; - font-weight: bold; - width: 100px; + visibility: hidden; max-height: 44px; + width: 100px; + height: 100px; + border-color: 1px #000 solid; + background-color: red; vertical-align: 5px; - visibility: hidden; - z-index: 100; + text-align: center; + font-weight: bold; } \ No newline at end of file diff --git a/test/fixtures/multi2.css b/test/fixtures/multi2.css index 300aee4..2ec4d0b 100644 --- a/test/fixtures/multi2.css +++ b/test/fixtures/multi2.css @@ -1,13 +1,13 @@ .multi2 { - width: 10px; - border: 1px #fff solid; position: absolute; - z-index: 10; - margin: 10px; - left: 10px; - height: 10px; - float: left; top: 10px; + left: 10px; + z-index: 10; display: table; + float: left; + margin: 10px; padding: 10px; + width: 10px; + height: 10px; + border: 1px #fff solid; } \ No newline at end of file diff --git a/test/fixtures/style.css b/test/fixtures/style.css index 1b94f30..1ebc083 100644 --- a/test/fixtures/style.css +++ b/test/fixtures/style.css @@ -1,8 +1,8 @@ .comb { - text-align: center; width: 100px; - background-color: red; height: 100px; - display: block; + background-color: red; + text-align: center; font-weight: bold; + display: block; } \ No newline at end of file