-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.js
More file actions
289 lines (225 loc) · 8.06 KB
/
build.js
File metadata and controls
289 lines (225 loc) · 8.06 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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
"use strict";
var path = require('path');
var fs = require('fs-extra');
var byline = require('byline');
var util = require('util');
var stream = require('stream');
var Transform = stream.Transform;
var trim = function(str) {
if (!str) {
return str;
}
return str.replace(/^[\s\xA0]+/, '').replace(/[\s\xA0]+$/, '');
};
var parseColorFunction = function(colorFunc, result) {
var startIdx = colorFunc.indexOf('(');
var endIdx = colorFunc.lastIndexOf(')');
var funcName = trim(colorFunc.substring(0, startIdx));
var funcParams = trim(colorFunc.substring(startIdx + 1, endIdx));
var lastComma = funcParams.lastIndexOf(',');
var colorParam = funcParams.substring(0, lastComma);
var valueParam = funcParams.substring(lastComma + 1, funcParams.length);
colorParam = trim(colorParam);
valueParam = trim(valueParam);
if (colorParam.indexOf('lighten') >= 0 ||
colorParam.indexOf('darken') >= 0 ||
colorParam.indexOf('adjust-hue') >= 0) {
parseColorFunction(funcParams, result);
result.func.push({name: funcName, value: valueParam});
} else {
result.color = colorParam;
result.func.push({name: funcName, value: valueParam});
}
};
var isValidOperand = function(operand) {
return operand[0] === '$' || !isNaN(parseFloat(operand, 10));
};
var processCalcExpression = function(line) {
var openIdx = line.indexOf('(');
var closeIdx = line.lastIndexOf(')');
if (openIdx >= 0 && closeIdx > 0) {
var firstLinePart = line.substring(0, openIdx + 1);
var secondLinePart = line.substring(closeIdx, line.length);
var exp = line.substring(openIdx + 1, closeIdx);
var addOperatorIdx = exp.lastIndexOf('+');
var subOperatorIdx = exp.lastIndexOf('-');
var multOperatorIdx = exp.lastIndexOf('*');
var divOperatorIdx = exp.lastIndexOf('/');
var operatorIdx = Math.max(addOperatorIdx, subOperatorIdx, multOperatorIdx, divOperatorIdx);
console.log('Check if we have a calc value for line : ' + line);
console.log('firstLinePart: ' + firstLinePart);
console.log('secondLinePart: ' + secondLinePart);
console.log('exp: ' + exp);
console.log('operatorIdx: ' + operatorIdx);
if (operatorIdx > 0) {
var operator = exp[operatorIdx];
var operand1 = trim(exp.substr(0, operatorIdx));
var operand2 = trim(exp.substr(operatorIdx + 1, exp.length));
// Check operand is number or variable
if (!isValidOperand(operand1) || operand1.indexOf(' ') > 0 || operand1.indexOf(',') >= 0 || operand1.indexOf('(') >= 0
|| !isValidOperand(operand2) || operand2.indexOf(' ') > 0 || operand2.indexOf(',') >= 0 || operand2.indexOf('(') >= 0) {
// ignore
//console.log('ignore line: ' + line);
} else {
console.log('operator: ' + operator);
console.log('operand1: ' + operand1);
console.log('operand2: ' + operand2);
if (operand1 && operand2 && operand1.length > 0 && operand2.length > 0) {
line = firstLinePart + 'calc(' + operand1 + ' ' + operator + ' ' + operand2 + ')' + secondLinePart;
console.log('line: ' + line);
}
}
}
}
return line;
};
var ConvertToPostCSS = function(options) {
// allow use without new
if (!(this instanceof ConvertToPostCSS)) {
return new ConvertToPostCSS(options);
}
// init Transform
Transform.call(this, options);
};
util.inherits(ConvertToPostCSS, Transform);
ConvertToPostCSS.prototype._transform = function(chunk, encoding, callback) {
// TODO: fixme @fonf-face in glyphicons.css (remove manually to test)
var firstLinePart, secondLinePart;
var line = chunk.toString();
//var trimmedLine = trim(line);
// Remove inline comment
var commentIdx = line.indexOf('//');
if (commentIdx>= 0) {
line = line.substring(0, commentIdx);
}
// Fix icon-font-path (yes this is ugly)
if (line === '$icon-font-path: if($bootstrap-sass-asset-helper, "bootstrap/", "../fonts/bootstrap/") !default;') {
line = '$icon-font-path: "../fonts/bootstrap/");';
}
// Remove !default
line = line.replace('!default', '');
// Replace "@mixin mixin-name($var1, $var2: default) {"
// by "@define-mixin mixin-name $var1, $var2: default {"
if (line.indexOf('@mixin ') >= 0) {
line = line.replace('@mixin ', '@define-mixin ');
line = line.replace('...', '');
line = line.replace('(', ' ');
line = line.replace(')', '');
}
//@include gradient-vertical($start-color: $btn-color, $end-color: darken($btn-color, 12%));
//@include alert-variant($alert-success-bg, $alert-success-border, $alert-success-text);
// Replace @include mixin-name($var1, $var2);
// by @mixin mixin-name var1, var2 {}
if (line.indexOf('@include ') >= 0) {
line = line.replace('@include ', '@mixin ');
line = line.replace(/'/g, '');
line = line.replace('(', ' ');
line = line.replace(')', '');
line = line.replace(';', ' {}');
}
// Remove @extend (FIXME)
if (line.indexOf('@extend ') >= 0) {
line = '';
}
// Replace #{$var} by $(var)
line = line.replace(/\#\{\$([a-z]+)\}/ig, '$($1)');
// Special case of import deps (yes this is ugly)
if (line === '@import "mixins/vendor-prefixes";') {
line = '@import "mixins/border-radius";\n' + line;
} else if (line === '@import "mixins/progress-bar";') {
line = '@import "mixins/gradients";\n' + line;
} else if (line === '@import "mixins/border-radius";') {
line = '';
} else if (line === '@import "mixins/gradients";') {
line = '';
}
// Color
var lightenIdx = line.indexOf('lighten');
var darkenIdx = line.indexOf('darken');
// TODO: adjust-hue
var minIdx = lightenIdx;
//var indexLength = 'lighten'.length;
if (darkenIdx >= 0 && (minIdx < 0 || darkenIdx < minIdx)) {
minIdx = darkenIdx;
//indexLength = 'darken'.length;
}
if (minIdx >= 0) {
firstLinePart = line.substring(0, minIdx);
secondLinePart = line.substring(minIdx, line.length);
//console.log('firstLinePart: ' + firstLinePart);
//console.log('secondLinePart: ' + secondLinePart);
var result = {
color: null,
func: []
};
parseColorFunction(secondLinePart, result);
var color = 'color(' + result.color;
result.func.forEach(function(func) {
var value = func.value;
if (value[0] !== '-' || value[0] !== '+') {
value = '+' + value;
}
if (func.name === 'lighten') {
color += ' lightness(' + value + ')';
} else if (func.name === 'darken') {
color += ' blackness(' + value + ')';
} else if (func.name === 'adjust-hue') {
color += ' hue(' + value + ')';
} else {
console.error('color func ' + func.name + ' not supported');
//throw new Error('color func ' + func.name + ' not supported');
}
});
color += ');';
line = firstLinePart + color;
//console.log('result: ' + line);
}
// Calc
line = processCalcExpression(line);
if (line !== '') {
this.push(line + '\n');
}
callback();
};
var cleanFiles = function(directory) {
fs.readdirSync(directory).forEach(function(fileName) {
var file = path.join(directory, fileName);
var stat = fs.statSync(file);
if (stat.isDirectory()) {
// Recursive call
cleanFiles(file);
} else {
var cleanFileName;
var extIndex = fileName.lastIndexOf('.');
if (extIndex < 0) {
cleanFileName = fileName;
} else {
cleanFileName = fileName.substring(0, extIndex);
}
if (cleanFileName[0] === '_') {
cleanFileName = cleanFileName.substring(1, cleanFileName.length);
}
var newFile = path.join(directory, cleanFileName + '.css');
var convertToPostCSS = new ConvertToPostCSS();
byline(fs.createReadStream(file))
.pipe(convertToPostCSS)
.pipe(fs.createWriteStream(newFile));
fs.unlink(file);
}
});
};
var line = ' font-size: ($font-size-base - 1); ';
console.log(processCalcExpression(line));
var bootstrapSassModule = path.join(process.cwd(), 'node_modules/bootstrap-sass');
if (!fs.existsSync(bootstrapSassModule)) {
throw new Error('bootstrap-sass module not found, missing npm install?');
}
var bootstrapAssets = path.join(bootstrapSassModule, 'assets');
if (!fs.existsSync(bootstrapAssets)) {
throw new Error('assets directory not found, bootstrap-sass version not supported. Try downgrade');
}
var copyAssets = path.join(process.cwd(), 'assets');
fs.emptyDirSync(copyAssets);
fs.copySync(bootstrapAssets, copyAssets);
var styles = path.join(copyAssets, 'stylesheets');
cleanFiles(styles);