-
Notifications
You must be signed in to change notification settings - Fork 20
/
index.js
84 lines (60 loc) · 2.13 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
'use strict';
var fontSpider = require('font-spider');
var colors = require('colors/safe');
var through = require('through2');
var gutil = require('gutil');
var fs = require('fs');
var path = require('path');
var htmlFiles = null;
function createStream(options) {
options = options || {};
if (!options.resourceBeforeLoad) {
options.resourceBeforeLoad = function(file) {
if (/https?/.test(file)) {
gutil.log('Load', colors.cyan(file));
}
};
}
function bufferContents(file, enc, callback) {
if (file.isNull()) {
callback(null);
return;
}
if (file.isBuffer && file.isBuffer()) {
if (!htmlFiles) {
htmlFiles = [];
}
htmlFiles.push(file);
callback(null, file);
} else {
callback(null, file);
}
}
function endStream(callback) {
if (!htmlFiles || htmlFiles.length === 0) {
callback();
return;
}
fontSpider(htmlFiles, options).then(function(webFonts) {
webFonts.forEach(function(webFont) {
gutil.log('Font family', colors.green(webFont.family));
gutil.log('Original size', colors.green(webFont.originalSize / 1000 + ' KB'));
gutil.log('Include chars', webFont.chars);
gutil.log('Font id', webFont.id);
gutil.log('CSS selectors', webFont.selectors.join(', '));
webFont.files.forEach(function(file) {
if (fs.existsSync(file.url)) {
gutil.log('File', colors.cyan(path.relative('./', file.url)) + ' created: ' +
colors.green(file.size / 1000 + ' KB'));
} else {
gutil.log(colors.red('File ' + path.relative('./', file.url) + ' not created'));
}
});
});
callback(null);
}).catch(callback);
htmlFiles = null;
}
return through.obj(bufferContents, endStream);
}
module.exports = createStream;