cli.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /**
  2. * Command line implementation for CSSComb
  3. *
  4. * Usage example:
  5. * ./node_modules/.bin/csscomb [options] file1 [dir1 [fileN [dirN]]]
  6. */
  7. var fs = require('fs');
  8. var path = require('path');
  9. var program = require('commander');
  10. var vow = require('vow');
  11. var Comb = require('./csscomb');
  12. program
  13. .version(require('../package.json').version)
  14. .usage('[options] <file ...>')
  15. .option('-v, --verbose', 'verbose mode')
  16. .option('-c, --config [path]', 'configuration file path')
  17. .option('-d, --detect', 'detect mode (would return detected options)')
  18. .option('-l, --lint', 'in case some fixes needed returns an error')
  19. .parse(process.argv);
  20. if (!program.args.length) {
  21. console.log('No input paths specified');
  22. program.help();
  23. }
  24. var comb = new Comb();
  25. if (program.detect) {
  26. console.log(JSON.stringify(Comb.detectInFile(program.args[0]), false, 4));
  27. process.exit(0);
  28. }
  29. var config;
  30. var configPath = program.config &&
  31. path.resolve(process.cwd(), program.config) ||
  32. Comb.getCustomConfigPath();
  33. if (!fs.existsSync(configPath)) {
  34. config = require('../config/csscomb.json');
  35. } else if (configPath.match(/\.css$/)) {
  36. config = Comb.detectInFile(configPath);
  37. } else {
  38. config = Comb.getCustomConfig(configPath);
  39. }
  40. if (!config) {
  41. console.log('Configuration file ' + configPath + ' was not found.');
  42. process.exit(1);
  43. }
  44. if (config.template) {
  45. if (fs.existsSync(config.template)) {
  46. var templateConfig = Comb.detectInFile(config.template);
  47. for (var attrname in templateConfig) {
  48. if (!config[attrname]) {
  49. config[attrname] = templateConfig[attrname];
  50. }
  51. }
  52. } else {
  53. console.log('Template configuration file ' + config.template + ' was not found.');
  54. process.exit(1);
  55. }
  56. }
  57. console.time('spent');
  58. config.verbose = program.verbose === true || config.verbose;
  59. config.lint = program.lint;
  60. comb.configure(config);
  61. vow.all(program.args.map(comb.processPath.bind(comb)))
  62. .then(function(changedFiles) {
  63. changedFiles = [].concat.apply([], changedFiles)
  64. .filter(function(isChanged) {
  65. return isChanged !== undefined;
  66. });
  67. for (var i = changedFiles.length, tbchanged = 0; i--;) {
  68. tbchanged += changedFiles[i];
  69. }
  70. var changed = config.lint ? 0 : tbchanged;
  71. if (config.verbose) {
  72. console.log('');
  73. console.log(changedFiles.length + ' file' + (changedFiles.length === 1 ? '' : 's') + ' processed');
  74. console.log(changed + ' file' + (changed === 1 ? '' : 's') + ' fixed');
  75. console.timeEnd('spent');
  76. }
  77. if (config.lint && tbchanged) {
  78. process.exit(1);
  79. }
  80. })
  81. .fail(function(e) {
  82. console.log('stack: ', e.stack);
  83. process.exit(1);
  84. });