Skip to content

Commit

Permalink
check in lib
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonoh89 committed Jul 19, 2016
1 parent 4c8145d commit eaa3404
Show file tree
Hide file tree
Showing 30 changed files with 3,330 additions and 1 deletion.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
lib
lib-cov
node_modules
test/test-coverage.html
Expand Down
149 changes: 149 additions & 0 deletions lib/cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
'use strict';

/**
* Command line implementation for CSSComb
*
* Usage example:
* ./node_modules/.bin/csscomb [options] [file1 [dir1 [fileN [dirN]]]]
*/
var format = require('./format');
var fs = require('fs');
var parseArgs = require('minimist');
var path = require('path');
var vow = require('vow');
var Comb = require('./csscomb');
var comb = new Comb();

var getInputData = new vow.Promise(function (resolve) {
var input = '';
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', function (data) {
input += data;
});
process.stdin.on('end', function () {
resolve(input);
});
});

function processInputData(input) {
try {
process.stdout.write(comb.processString(input));
process.exit(0);
} catch (e) {
process.stderr.write(e.message);
process.exit(1);
}
}

function processSTDIN() {
getInputData.then(processInputData);
}

function processFiles(files, config) {
vow.all(files.map(comb.processPath.bind(comb))).then(function (c) {
c = c.filter(function (isChanged) {
return isChanged !== undefined;
});

var tbchanged = c.reduce(function (a, b) {
return a + b;
}, 0);

var changed = config.lint ? 0 : tbchanged;

if (config.verbose) {
var message = '\n\n ' + c.length + ' file' + (c.length === 1 ? '' : 's') + ' processed\n\n ' + changed + ' file' + (changed === 1 ? '' : 's') + ' fixed\n';
process.stdout.write(format(message));
console.timeEnd('Time spent');
}

if (config.lint && tbchanged) {
process.exit(1);
}

process.exit(0);
}).fail(function (e) {
process.stderr.write(e.stack);
process.exit(1);
});
}

function getOptions() {
var parserOptions = {
boolean: ['verbose', 'lint'],
alias: {
config: 'c',
detect: 'd',
lint: 'l',
verbose: 'v'
}
};
return parseArgs(process.argv.slice(2), parserOptions);
}

function applyTemplate(config) {
if (!config.template) return;

if (!fs.existsSync(config.template)) {
var message = 'Template configuration file ' + config.template + '\n was not found.';
process.stderr.write(format(message));
process.exit(1);
}

var templateConfig = Comb.detectInFile(config.template);
for (var attrname in templateConfig) {
if (templateConfig.hasOwnProperty(attrname) && !config[attrname]) {
config[attrname] = templateConfig[attrname];
}
}
}

function getConfig(options) {
var configPath = options.config && path.resolve(process.cwd(), options.config) || Comb.getCustomConfigPath();

var config;
if (!fs.existsSync(configPath)) {
config = require('../config/csscomb.json');
} else if (configPath.match(/\.css$/)) {
config = Comb.detectInFile(configPath);
} else {
config = Comb.getCustomConfig(configPath);
}

if (!config) {
var message = 'Error parsing configuration file ' + configPath + '.';
process.stderr.write(format(message));
process.exit(1);
}

applyTemplate(config);
if (options.verbose) config.verbose = options.verbose;
if (options.lint) config.lint = options.lint;

return config;
}

function detectConfig(file) {
var config = Comb.detectInFile(file);
config = JSON.stringify(config, false, 4);
process.stdout.write(config);
process.exit(0);
}

console.time('Time spent');

var options = getOptions();

if (options.detect) {
detectConfig(options.detect);
}

var config = getConfig(options);
comb.configure(config);

if (process.stdin.isTTY) {
processFiles(options._, config);
} else {
processSTDIN();
}
Loading

0 comments on commit eaa3404

Please sign in to comment.