forked from csscomb/csscomb.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
243 lines (207 loc) · 5.25 KB
/
cli.js
File metadata and controls
243 lines (207 loc) · 5.25 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
'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 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.fix && process.stdin.isTTY) {
processFiles(options._);
} else if (options.fix) {
processSTDIN();
} else if (process.stdin.isTTY) {
lintFiles(options._);
} else {
lintSTDIN();
}
function getOptions() {
var parserOptions = {
boolean: ['help', 'fix', 'verbose'],
alias: {
config: 'c',
detect: 'd',
fix: 'f',
help: 'h',
verbose: 'v'
}
};
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.',
' -f, --fix',
' Run the tool in fixer mode, modifying files when possible.',
' -h, --help',
' Display help message.',
' -v, --verbose',
' Whether to print logging info.'
];
process.stdout.write(help.join('\n'));
}
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;
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 => {
var tbchanged = c.filter(isChanged => {
return isChanged !== undefined;
}).reduce((a, b) => {
return a + b;
}, 0);
if (config.verbose) {
let message = [
`${c.length} file${c.length === 1 ? '' : 's'} processed`,
`${tbchanged} file${tbchanged === 1 ? '' : 's'} fixed`,
''
].join('\n');
process.stdout.write(message);
}
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);
});
}
function lintFiles(files) {
let anyErrorsFound = false;
const promises = files.map(file => {
return comb.lintPath(file).then(errors => {
if (!errors.length) {
return;
}
anyErrorsFound = true;
const message = formatErrors(file, errors);
process.stdout.write(message);
});
});
Promise.all(promises).catch(error => {
process.stderr.write(error.message);
process.exit(1);
}).then(function() {
if (anyErrorsFound) {
process.exit(1);
} else {
process.exit(0);
}
});
}
function formatErrors(fileName, errors) {
let message = [];
errors.forEach(error => {
message.push(
error.message + ' at ' + fileName + ':' + error.line,
error.context,
'',
''
);
});
return message.join('\n');
}
function lintSTDIN() {
getInputData.then(lintInputData);
}
function lintInputData(input) {
comb.lintString(input).catch(e => {
process.stderr.write(e.message);
process.exit(1);
}).then(output => {
if (!output.length) {
process.exit(0);
} else {
process.stdout.write(output);
process.exit(1);
}
});
}