-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
158 lines (125 loc) · 4.22 KB
/
index.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
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
const path = require('path');
const fs = require('fs');
const glob = require('glob');
const ROOT = ':root';
const VAR = ' var';
const CALC = ' calc';
const OPENING_PARENTHESES = '{';
const CLOSING_PARENTHESES = '}';
const OPENING_BRACKET = '(';
const CLOSING_BRACKET = ')';
const SPACE = ' ';
const EMPTY = '';
function replaceAt(string, index, char = SPACE) {
if (index > string.length - 1) return string;
return string.substr(0, index) + char + string.substr(index + 1);
}
function cleanupRoot(content) {
let rootIndex;
while ((rootIndex = content.indexOf(ROOT)) !== -1) {
let rootOpening = content.indexOf(OPENING_PARENTHESES, rootIndex),
opening = 0,
closing = 0;
for (let i = rootOpening; i < content.length; i++) {
if (content[i] === OPENING_PARENTHESES) {
opening++;
}
else if (content[i] === CLOSING_PARENTHESES) {
closing++;
if (closing === opening) { //we found root closing parentheses
content = replaceAt(content, rootOpening);
content = replaceAt(content, i);
break;
}
}
}
content = content.replace(ROOT, EMPTY);
}
return content;
}
function cleanupVar(content) {
let variableIndex;
while ((variableIndex = content.indexOf(VAR)) !== -1) {
let variableOpening = content.indexOf(OPENING_BRACKET, variableIndex),
opening = 0,
closing = 0;
for (let i = variableOpening; i < content.length; i++) {
if (content[i] === OPENING_BRACKET) {
opening++;
}
else if (content[i] === CLOSING_BRACKET) {
closing++;
if (closing === opening) { //we found var closing bracket
content = replaceAt(content, variableOpening);
content = replaceAt(content, i, EMPTY);
break;
}
}
}
content = content.replace(VAR, EMPTY);
}
return content;
}
function cleanupCalc(content) {
let calcIndex;
while (calcIndex = content.indexOf(CALC) !== -1) {
let calcOpening = content.indexOf(OPENING_BRACKET, calcIndex),
opening = 0,
closing = 0;
for (let i = calcOpening; i < content.length; i++) {
if (content[i] === OPENING_BRACKET) {
opening++;
}
else if (content[i] === CLOSING_BRACKET) {
closing++;
if (closing === opening) { //we found root closing parentheses
content = replaceAt(content, calcOpening);
content = replaceAt(content, i, EMPTY);
break;
}
}
}
content = content.replace(CALC, EMPTY);
}
return content;
}
function replaceVars(content){
const pattern = '([^a-zA-Z0-9])(--([a-zA-Z0-9]+))';
let variableRegexp = new RegExp(pattern, "g");
content = content.replace(variableRegexp, '$1$$$3');
return content;
}
function processContent(content){
content = cleanupRoot(content);
content = cleanupVar(content);
content = cleanupCalc(content);
content = replaceVars(content);
return content;
}
module.exports = {
convert: function (src, dest) {
return new Promise((resolve, reject) => {
let content = fs.readFileSync(src, 'utf8');
content = processContent(content);
if (dest) {
let outputFile = fs.createWriteStream(dest);
outputFile.once('open', function (fd) {
outputFile.write(content);
outputFile.end();
});
outputFile.on('close', function () {
resolve(dest)
});
} else {
let outputFile = fs.createWriteStream(src);
outputFile.once('open', function (fd) {
outputFile.write(content);
outputFile.end();
});
outputFile.on('close', function () {
resolve(src);
});
}
});
}
};