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