-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
76 lines (63 loc) · 2.54 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
'use strict';
var fs = require('fs');
var path = require('path');
var keyContains = require('./lib/keyContains');
var humanize = require('underscore.string/humanize');
function noop () {}
module.exports = function (options) {
var tests = options.tests;
var minifiers = options.minifiers;
var onSuite = options.onSuite || noop;
var onEnd = options.onEnd || noop;
var suites = [];
var testNames = [];
tests.forEach(function (test) {
suites.push(new Promise(function (resolve) {
var opts = fs.readdirSync(test).reduce(function (config, item) {
if (~item.indexOf('.css')) {
var file = fs.readFileSync(path.join(test, item), 'utf-8');
config[path.basename(item, '.css')] = file;
}
return config;
}, {});
var output = {
fixture: opts.fixture,
optimal: keyContains(opts, 'optimal'),
broken: keyContains(opts, 'broken'),
outstanding: keyContains(opts, 'outstanding'),
title: humanize(path.basename(test))
};
testNames.push(output.title);
var promises = [];
var results = {};
Object.keys(minifiers).forEach(function (minifier) {
promises.push(minifiers[minifier](opts.fixture).then(function (css) {
var grade = function (key) {
return typeof key !== 'undefined' && ~key.indexOf(css);
};
results[minifier] = {result: 'sub-optimal', output: css};
if (grade(output.outstanding)) {
results[minifier].result = 'outstanding';
} else if (grade(output.optimal)) {
results[minifier].result = 'optimal';
} else if (grade(output.broken)) {
results[minifier].result = 'broken';
}
}, function (err) {
results[minifier] = {result: 'crashed', err: err};
}).then(function () {
return results;
}));
});
var getResults = function () {
return resolve(results);
};
Promise.all(promises).then(function () {
onSuite(results, output);
}).then(getResults).catch(getResults);
}));
});
Promise.all(suites).then(function (results) {
onEnd(results, testNames);
});
};