-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
281 lines (242 loc) · 9.16 KB
/
gulpfile.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
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
var TEST_PATH = 'test/sass',
CURRENT_PATH = './',
SOURCE_PATH = 'input',
EXPECTED_OUTPUT_PATH = 'expected-output',
OUTPUT_PATH = 'generated-output',
FWK_ERROR_PREFIX = 'SPOT CSS: ',
FWK_ERROR_PREFIX_NEW = 'SPOT CSS [',
FULL_SOURCE_PATH = CURRENT_PATH + TEST_PATH + '/' + SOURCE_PATH,
FULL_EXPECTED_OUTPUT_PATH = CURRENT_PATH + TEST_PATH + '/' + EXPECTED_OUTPUT_PATH,
FULL_OUTPUT_PATH = CURRENT_PATH + TEST_PATH + '/' + OUTPUT_PATH,
gulp = require('gulp'),
sass = require('gulp-sass')(require('sass')),
fs = require('fs'),
es = require('event-stream'),
fileExists = require('file-exists'),
process = require('process'),
through = require('through2'),
clc = require('cli-color');
diff = require('gulp-diff'),
del = require('del'),
sourcemaps = require('gulp-sourcemaps'),
gulpFlatmap = require('gulp-flatmap');
sass.compiler = require('node-sass');
var WATCH_FILE_NAMES = '*'; //'*.check*'; // default '*' = all
// ---------------------------------------------------------------------
gulp.task('log-start', function(cb){
noErrors = true;
cb();
});
gulp.task('clean', function(){
return del(FULL_OUTPUT_PATH);
});
gulp.task('check-expected-output-files', function() {
return gulp.src(FULL_SOURCE_PATH + '/**/'+WATCH_FILE_NAMES+'.s[ac]ss')
// check files if exists in EXPECTED_OUTPUT_PATH
.pipe(checkFiles(es));
});
gulp.task('sass', function() {
return gulp.src(FULL_SOURCE_PATH + '/**/'+WATCH_FILE_NAMES+'.s[ac]ss')
// gulpFlatmap is used to not fail pasing SASS if one of the files will fail
.pipe(gulpFlatmap(function (stream) { // the stream contains a single file
return stream
.pipe(sourcemaps.init())
.pipe(sass({outputStyle: 'expanded'})).on('error', throwSassError)
// write CSS files into SOURCE_PATH
.pipe(gulp.dest(FULL_OUTPUT_PATH))
}));
});
gulp.task('check-diff', function() {
return gulp.src(FULL_OUTPUT_PATH + '/**/'+WATCH_FILE_NAMES+'.css')
// compare generated files with expected output (mute )
.pipe(diff(FULL_EXPECTED_OUTPUT_PATH).on('error', function(e){
// mute this kind of error - it is outputed in task "check-expected-output-files" already
if (e.toString().indexOf('Failed to stat file')!==-1) {
this.emit('end');
}
}))
.pipe(reporter({ fail: false, quiet: false }));
});
gulp.task('status', function() {
return gulp.src(FULL_OUTPUT_PATH + '/**/'+WATCH_FILE_NAMES+'.css')
.pipe(finish());
});
gulp.task('test',
gulp.series(
'log-start',
'clean',
'sass',
'check-expected-output-files',
'check-diff',
'status'
)
);
gulp.task('watch', function() {
return gulp.watch([
FULL_SOURCE_PATH + '/**/'+WATCH_FILE_NAMES+'.s[ac]ss',
FULL_EXPECTED_OUTPUT_PATH + '/**/'+WATCH_FILE_NAMES+'.css',
'./src/**.s[ac]ss'
], gulp.series('test'));
});
gulp.task('test:watch', gulp.series('test', 'watch'));
// copy generated to expected (sync of directories)
gulp.task('test:sync', function(){
return gulp.src(FULL_OUTPUT_PATH + '/**/'+WATCH_FILE_NAMES+'.css')
.pipe(gulp.dest(FULL_EXPECTED_OUTPUT_PATH));
});
// ---------------------------------------------------------------------
function throwSassError(error) {
try {
var originalFile = error.message.split('\n')[0],
sourceFile = (error.message.match(new RegExp('from line \\d+ of ('+TEST_PATH + '/' + SOURCE_PATH + '[^,$\n]*)')) || error.message.match(/from line \d+ of (.*)/))[1],
targetFile = (sourceFile || originalFile).replace(SOURCE_PATH, OUTPUT_PATH).replace(/\.s[ac]ss$/, '.css'),
isFwkError = error.messageOriginal.indexOf(FWK_ERROR_PREFIX)===0 || error.messageOriginal.indexOf(FWK_ERROR_PREFIX_NEW)===0,
message = isFwkError ? error.messageOriginal.split(' - ')[0] : error.messageFormatted;
// uncomment when debugging for full errors
// if (targetFile.indexOf('only-for.mixins.')!==-1) {
// console.log(error);
// }
if (isFwkError) {
// write expected (SPOT CSS framework) error to target file
fs.mkdir(getPathWithoutFileName(targetFile), { recursive: true }, function(err, cb) {
fs.writeFileSync(targetFile, '/* Error '+message+' */');
});
}
else {
// non-expected (SASS) errors write to console
throwError(1, message);
}
}
catch(e) {
console.error(error);
}
this.emit('end');
}
var noErrors = true,
failedAlreadyPrinted = false;
function start() {
return through.obj(function (file, enc, cb) {
noErrors = true;
failedAlreadyPrinted = false;
return cb(null, file);
});
}
function finish(cb) {
if (noErrors) {
console.log('\x1b[32m%s\x1b[0m ', 'OK'); // green color
}
else if (!failedAlreadyPrinted) {
console.log('\x1b[31m%s\x1b[0m', 'Failed!'); // red color
}
return through.obj(function (file, enc, cb) {
return cb(null, file);
});
}
function throwError(code, message) {
console.log('\x1b[31m%s\x1b[0m', 'Failed!'); // red color
failedAlreadyPrinted = true;
if (message) {
console.log(message);
}
noErrors = false;
//process.exit(-1);
}
function checkFiles(es) {
return es.map(function(file, cb) {
var slash = recognizeSlash(file.path),
extension = getFileExtension(file.path),
relativePath = getRelativePathWithoutFileName(file.path, SOURCE_PATH),
fileNameWithoutExtension = getFileNameWithoutExtension(file.path),
expectedOutputFile = CURRENT_PATH + normalizePath( TEST_PATH + slash + EXPECTED_OUTPUT_PATH + slash + relativePath + fileNameWithoutExtension ) + '.css';
fileExists(expectedOutputFile, (err, exists) => {
if (err) cb(err, file);
var relative = relativePath.replace(/\\/g,'/');
if (!exists) {
throwError(1, 'Missing file in /'+TEST_PATH+'/'+EXPECTED_OUTPUT_PATH+'/'+relative+fileNameWithoutExtension+'.css\n'+
'as an expected output '+
'to input file /'+TEST_PATH+'/'+SOURCE_PATH+'/'+relative+fileNameWithoutExtension+'.'+extension);
var newContent = fs.readFileSync('./'+TEST_PATH+'/'+OUTPUT_PATH+'/'+relative+fileNameWithoutExtension+'.css');
if (err) cb(err, file);
console.log('\x1b[32m%s\x1b[0m ', newContent); // green color
}
cb(null, file);
});
});
};
/** Return \ or /. */
function recognizeSlash(path) {
return path.match(/[\/\\]/);
}
function getFileName(path) {
return path.split(recognizeSlash(path)).pop();
}
function getFileExtension(path) {
return path.split('.').pop();
}
function getFileNameWithoutExtension(path) {
var parts = getFileName(path).split('.');
parts.pop();
return parts.join('.');
}
/** Returns path without ending slash. */
function getPathWithoutFileName(path) {
var slash = recognizeSlash(path),
parts = path.split(slash);
parts.pop();
return parts.join(slash);
}
/** Returns path without starting slash. */
function getRelativePath(path, pivot) {
var slash = recognizeSlash(path),
parts = path.split(slash + pivot + slash);
return parts.pop();
}
/** Returns path without starting slash. */
function getRelativePathWithoutFileName(path, pivot) {
var relativePath = getPathWithoutFileName(getRelativePath(path, pivot));
return relativePath==='' ? '' : relativePath + recognizeSlash(path);
}
function normalizePath(path) {
return path.replace(/\\/g, '/');
}
// function reporter is from gulp-diff plugin, but changed
var reporter = function reporter(opts) {
opts = opts || {};
return through.obj(function(file, enc, cb) {
if (file.diff && Object.keys(file.diff).length) {
if (!opts.quiet) {
console.log('\n' + clc.underline(file.path), '\n');
console.log(file.diff.map(formatLine).join(''));
}
noErrors = false; // changed
if (opts.fail) {
this.emit('error', 'Files differ');
}
}
return cb(null, file);
});
};
function formatLine(part, i) {
// if original content, make it shorter (and more than 6 lines)
if (!part.added && !part.removed && part.count>6) {
var lines = part.value.split('\n'),
first3 = lines.slice(0,3),
last3 = lines.slice(-3);
// show just 3 first lines and 3 last lines
part.value = first3.join('\n')+'\n\n'+clc.cyanBright('...(cropped '+(lines.length-6)+' lines)...')+'\n\n'+last3.join('\n');
part.count = 3+3+3;
}
var indent = ' ';
return (!i ? indent : '') + part.value.split('\n').map(function(ln) {
return clc.black(clc[colorLine(part)](ln));
}).join('\n' + indent);
}
function colorLine(ln) {
if (ln.added) {
return 'bgGreen';
}
if (ln.removed) {
return 'bgRed';
}
return 'blackBright';
}