function csscombFormatter () { var Comb = require('csscomb') var comb = new Comb() comb.configure(require('./.csscomb.json')) return function (content) { return comb.processString(content, { syntax: 'scss' }) } }
function runCommand(data, cb) { var cb_wtf = arguments[arguments.length - 1]; //{ '0': data, '1': null, '2': [Function] } console.log('c', arguments); comb.configure(data.config || CombConfig); var combedCSS = comb.processString(data.css, data.ext); cb_wtf(null, combedCSS); }
formatter.format = function (contents, path, cliOptions) { var name = this.options.name; var config = util.getConfig(name, cliOptions.lookup && path); csscomb.configure(config); var syntax = /\.html?$/.test(path) ? 'css' : path.split('.').pop(); return csscomb .processString(contents, {syntax: syntax}) .catch(function (error) { return cliOptions.debug ? Promise.reject(error) : contents; }); };
formatter.format = function (contents, path, cliOptions) { var name = this.options.name; var config = util.getConfig(name, cliOptions.lookup ? path : ''); csscomb.configure(config); try { contents = csscomb.processString(cssbeautify(contents)); } catch (error) { if (cliOptions.debug) { throw error; } this.emit('error', error); } return contents; };
grunt.registerMultiTask('csscomb', 'Sorting CSS properties in specific order.', function () { var Comb = require('csscomb'), comb = new Comb(), HOME = process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE; function getConfigPath(configPath) { var dirname, parentDirname; configPath = configPath || path.join(process.cwd(), '.csscomb.json'); // If we've finally found a config, return its path: if (grunt.file.exists(configPath)) { return configPath; } dirname = path.dirname(configPath); parentDirname = path.dirname(dirname); // If we are in HOME dir already and yet no config file, quit. // If project is located not under HOME, compare to root instead. // Since there appears to be no good way to get root path in // Windows, assume that if current dir has no parent dir, we're in // root. if (dirname === HOME || dirname === parentDirname) { return; } // If there is no config in this directory, go one level up and look for // a config there: configPath = path.join(parentDirname, '.csscomb.json'); return getConfigPath(configPath); } // Get config file from task's options: var config = grunt.task.current.options().config || getConfigPath(); // Check if config file is set and exists. If not, use default one: if (config && grunt.file.exists(config)) { grunt.log.ok('Using custom config file "' + config + '"...'); config = grunt.file.readJSON(config); } else { grunt.log.ok('Using default config file...'); config = comb.getConfig('csscomb'); } // Configure csscomb: comb.configure(config); this.files.forEach(function (f) { f.src.filter(function (filepath) { // Warn on and remove invalid source files (if nonull was set). if (!grunt.file.exists(filepath)) { grunt.log.warn('Source file "' + filepath + '" not found.'); return false; } else { return true; } }).forEach(function (src) { // Get CSS from a source file: var css = grunt.file.read(src); // Comb it: grunt.log.ok('Sorting file "' + src + '"...'); var syntax = src.split('.').pop(); var combed = comb.processString(css, syntax); grunt.file.write(f.dest, combed); }); }); });
grunt.registerMultiTask( 'wpcss', 'Format style sheets to match WordPress coding standards.', function() { var comb = new Comb(), config = {}, configKeys = {}, configPath = {}, userConfig = {}, options; options = this.options({ commentSpacing: true, config: 'default' }); configPath = path.resolve( __dirname, 'config/' + options.config + '.json' ); if ( fs.existsSync( configPath ) ) { config = require( configPath ); } else { grunt.fatal( 'Invalid config defined.' ); } if ( grunt.file.exists( '.csscomb.json' ) ) { userConfig = grunt.file.readJSON( '.csscomb.json' ); } configKeys = _.keys( config ); config = _.extend( config, userConfig, options ); config = _.pick( config, configKeys ); comb.configure( config ); this.files.forEach(function( f ) { // Read and combine source files into the 'contents' var. var contents = f.src.filter(function( filepath ) { // Warn on and remove invalid source files (if nonull was set). if ( ! grunt.file.exists( filepath ) ) { grunt.log.warn( 'Source file "' + filepath + '" not found.' ); return false; } else { return true; } }).map(function( filepath ) { return grunt.file.read( filepath ).trim(); }).join( '\n\n' ); contents = cssbeautify( contents ); contents = comb.processString( contents ); if ( options.commentSpacing ) { // Add two newlines before all comments that follow a closing curly brace. contents = contents.replace( /}\s+\/\*/g, '}\n\n/*' ); // Collapse extra newlines. contents = contents.replace( /\n{3,}/g, '\n\n' ); // Comments with at least 20 dashes in them will be considered // section headings and should follow two newlines. contents = contents.replace(/\s*(\/\*((?!\*\/)[\s\S])+-{20,}[\s\S]*?\*\/)\s*/g, '\n\n\n$1\n\n'); contents = contents.replace( /\n{4,}/g, '\n\n\n' ); // Trim whitespace at the beginning of the file. contents = contents.replace( /^\s*/, '' ); } grunt.file.write( f.dest, contents ); }); });