/
prettycss
executable file
·171 lines (148 loc) · 4.45 KB
/
prettycss
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
#!/usr/bin/env node
"use strict";
var prettycss = require('../lib/prettycss');
var fs = require('fs');
var OptionParser = require('option-parser');
var parser = new OptionParser();
var options = {
beautifierConfig: {
debug: true},
ignoreAllWarnings: false,
ignoreWarnings: [],
stdinRead: false,
stopOnErrors: false,
stopOnWarnings: false
};
parser.
addOption('c', 'config', 'Specify a JSON config file for beautifier').
argument('filename').
action(function (filename) {
var contents;
contents = fs.readFileSync(filename);
options.beautifierConfig = JSON.parse(contents);
});
parser.
addOption('h', 'help', 'Display this help message').
action(function () {
console.log("Validate and pretty-print a CSS file");
console.log("");
console.log("Usage:");
console.log("\t" + parser.programName() + " [options] filename [filename [...]]");
console.log("");
console.log("Available Options:");
console.log(parser.help());
console.log("");
console.log("When using any of the --stop options, the errors or warnings that caused");
console.log("a problem will be shown to stderr instead and this program will exit with");
console.log("an error code of 1.");
console.log("");
console.log("Warnings can be ignored by their code. You can ignore all of the");
console.log("\"Unsupported Browser\" warnings by using --ignore=browser-unsupported or");
console.log("specify a value like --ignore=browser-unsupported:ie6 to ignore unsupported");
console.log("browser errors just for Internet Explorer 6. All codes can be found");
console.log("in lib/lang/*.js or on the website.");
process.exit(0);
});
parser.
addOption('i', 'ignore', 'Ignore a type of warning').
argument('code').
action(function (arg) {
options.ignoreWarnings.push(arg);
});
parser.
addOption(null, 'ignore-all', 'Ignore all warnings').
action(function () {
options.ignoreAllWarnings = true;
});
parser.
addOption(null, 'stop-on-errors', 'Stop when an error is encountered').
action(function () {
options.stopOnErrors = true;
});
parser.
addOption(null, 'stop-on-warnings', 'Stop when a warning is encountered').
action(function () {
options.stopOnWarnings = true;
});
parser.
addOption('s', 'stop-on-problems', 'Same as --stop-on-errors --stop-on-warnings').
action(function () {
options.stopOnErrors = true;
options.stopOnWarnings = true;
});
var filenames = parser.parse();
filenames.forEach(function (file) {
parseFile(file);
});
if (filenames.length === 0) {
console.error('Please pass filenames on the command line');
console.error('For a list of options, use --help');
}
function getFileContents(filename) {
if (filename !== '-') {
return fs.readFileSync(filename, 'utf-8');
}
if (options.stdinRead) {
throw new Error('Already read from stdin');
}
if (fs.existsSync('/dev/stdin')) {
return fs.readFileSync('/dev/stdin', 'utf-8');
}
throw new Error('/dev/stdin does not exist and must read from stream synchronously');
}
function parseFile(filename) {
var contents = getFileContents(filename);
var result = prettycss.parse(contents, options.beautifierConfig);
var stop = false;
var warningCount = 0;
var errorCount = 0;
var importantProblems = [];
// Filter out ignored warnings and count everything
result.getProblems().forEach(function (item) {
if (item.typeCode === 'error') {
importantProblems.push(item);
errorCount ++;
} else if (!options.ignoreAllWarnings) {
if (! options.ignoreWarnings.some(function (warning) {
if (item.code === warning) {
return true;
}
if (warning.substr(0, item.code.length) === item.code &&
warning.charAt(item.code.length) === ':' &&
warning.substr(item.code.length + 1) === item.more) {
return true;
}
return false;
})) {
importantProblems.push(item);
warningCount ++;
}
}
});
if (warningCount || errorCount) {
var msg;
if (errorCount === 1) {
msg = "There was 1 error ";
} else {
msg = "There were " + errorCount + " errors ";
}
if (warningCount === 1) {
msg += "and 1 warning detected.";
} else {
msg += "and " + warningCount + " warnings detected.";
}
console.error(msg);
}
importantProblems.forEach(function (item) {
stop = true;
var message = item.typeText + ": " + item.message;
if (item.token) {
message += " (" + item.token.content + ", line " + item.token.line + ", char " + item.token.charNum + ")";
}
console.error(message);
});
if (stop) {
process.exit(1);
}
console.log(result.toString());
}