-
Notifications
You must be signed in to change notification settings - Fork 4
/
cssorder.js
70 lines (59 loc) · 1.92 KB
/
cssorder.js
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
var str = '';
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', function (data) {
str += data;
});
// Deal with cssorder custom config
function handleOrderCustom (content) {
var orderConfig;
try {
orderConfig = JSON.parse(process.argv[4]);
} catch (e) {
orderConfig = null;
}
// handle options
if (orderConfig) {
//Every block add newline
if(orderConfig["block-newline"]){
// Deal with multiple comment include css block {}
content = content.replace(/\}[\s\S][^\}+\*\/]+?\{/g, function(match){
// remove unnecessary newline,keep single newline
match = match.replace(/\n(?=\n)/g, '');
match = match.replace(/\}/, '');
return '}\n' + match;
});
// add \n between } and /*
// don't handle same line
content = content.replace(/\}\n[\s\S][^\}+\{+\*\/]+?\/\*/g, function(match){
// remove unnecessary newline,keep single newline
match = match.replace(/\n/g, '');
match = match.replace(/\}/, '');
return '}\n\n' + match;
});
}
}
return content;
}
process.stdin.on('end', function () {
var CSSOrder = require('./node_modules/cssorder/src/cssorder'),
order = new CSSOrder(),
syntax = process.argv[2],
config, ordered;
try {
config = JSON.parse(process.argv[3]);
} catch (e) {
config = null;
}
config = config ||
CSSOrder.getCustomConfig() ||
CSSOrder.getConfig('csscomb');
ordered = order.configure(config).processText(str, {syntax: syntax});
// Handle cssorder custom config
try {
ordered = handleOrderCustom(ordered);
} catch (e) {
throw new Error('CSSOrder custom config error');
}
process.stdout.write(ordered);
});