forked from ben-eb/css-minifiers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
engine.js
48 lines (39 loc) · 1.08 KB
/
engine.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
'use strict';
const fs = require('fs');
const path = require('path');
const pretty = require('pretty-hrtime');
function Engine (name, fn) {
if (!(this instanceof Engine)) {
return new Engine();
}
this.name = name;
this.func = fn;
const exports = this.func;
exports.version = this._getKey('version');
exports.url = this._getKey('homepage');
exports.toString = this.toString.bind(this);
exports.bench = function (css, precise) {
let start = process.hrtime();
return new Promise(function (resolve, reject) {
Promise.resolve(fn(css))
.then(
function () {
resolve(pretty(process.hrtime(start), { precise: precise }));
},
function (err) {
reject(err);
}
);
});
};
return exports;
}
Engine.prototype._getKey = function (key) {
const manifestPath = path.join('node_modules', this.name, 'package.json');
const manifest = JSON.parse(fs.readFileSync(manifestPath));
return manifest[key];
};
Engine.prototype.toString = function () {
return this.name;
};
module.exports = Engine;