-
Notifications
You must be signed in to change notification settings - Fork 450
Expand file tree
/
Copy pathcli.js
More file actions
192 lines (163 loc) · 4.37 KB
/
cli.js
File metadata and controls
192 lines (163 loc) · 4.37 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
'use strict';
/**
* Command line implementation for CSSComb
*
* Usage example:
* ./node_modules/.bin/csscomb [options] [file1 [dir1 [fileN [dirN]]]]
*/
var fs = require('fs');
var parseArgs = require('minimist');
var path = require('path');
var os = require('os');
var Comb = require('./csscomb');
var Errors = require('./errors');
var getInputData = new 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);
});
});
var comb = new Comb();
var options = getOptions();
if (options.help) {
displayHelp();
process.exit(0);
}
if (options.detect) {
const config = detectConfig();
process.stdout.write(config);
process.exit(0);
}
var config = getConfig();
comb.configure(config);
if (options['tty-mode'] || process.stdin.isTTY) {
processFiles(options._);
} else {
processSTDIN();
}
function getOptions() {
var parserOptions = {
boolean: ['help', 'lint', 'verbose', 'tty-mode'],
alias: {
config: 'c',
detect: 'd',
lint: 'l',
help: 'h',
verbose: 'v',
'tty-mode': 't'
}
};
return parseArgs(process.argv.slice(2), parserOptions);
}
function displayHelp() {
var help = [
'NAME',
' csscomb — Lint and fix style errors in css files',
'',
'SYNOPSIS',
' csscomb [options] file.css',
' cat file.css | csscomb [options] -',
'',
'OPTIONS',
' -c, --config [path]',
' Path to configuration file.',
' -d, --detect',
' Run the tool in detect mode, returning detected options.',
' -l, --lint',
' Run the tool in linter mode, without modifying files.',
' -h, --help',
' Display help message.',
' -v, --verbose',
' Whether to print logging info.',
' -t, --tty-mode',
' Run the tool in TTY mode using external app (e.g. IDE).',
''
];
process.stdout.write(help.join(os.EOL));
}
function detectConfig() {
const config = Comb.detectInFile(options.detect);
return JSON.stringify(config, false, 4);
}
function getConfig() {
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) {
const errorMessage = Errors.configParsingError(configPath);
process.stderr.write(errorMessage);
process.exit(1);
}
applyTemplate(config);
if (options.verbose) config.verbose = options.verbose;
if (options.lint) config.lint = options.lint;
return config;
}
function applyTemplate(config) {
if (!config.template) return;
if (!fs.existsSync(config.template)) {
const errorMessage = Errors.missingTemplateFile(config.template);
process.stderr.write(errorMessage);
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 processFiles(files) {
const promises = files.map(file => {
return comb.processPath(file);
});
Promise.all(promises).catch(error => {
process.stderr.write(error.stack);
process.exit(1);
}).then(c => {
c = [].concat.apply([], c);
var tbchanged = c.filter(isChanged => {
return isChanged !== undefined;
}).reduce((a, b) => {
return a + b;
}, 0);
var changed = options.lint ? 0 : tbchanged;
if (options.verbose) {
let message = [
`${c.length} file${c.length === 1 ? '' : 's'} processed`,
`${changed} file${changed === 1 ? '' : 's'} fixed`,
''
].join(os.EOL);
process.stdout.write(message);
}
if (options.lint && tbchanged) {
process.exit(1);
}
process.exit(0);
});
}
function processSTDIN() {
getInputData.then(processInputData);
}
function processInputData(input) {
comb.processString(input).catch(e => {
process.stderr.write(e.message);
process.exit(1);
}).then(output => {
process.stdout.write(output);
process.exit(0);
});
}