-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
54 lines (50 loc) · 1.46 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
var through = require('through2');
var gutil = require('gulp-util');
var PluginError = gutil.PluginError;
var File = gutil.File;
var sources = []; // store the source file paths
var options = {};
var crypto = require('crypto');
var createFont = require('./lib/createFont').createFont;
var _ = require('lodash');
var fs = require('fs');
// Consts
const PLUGIN_NAME = 'gulp-webfont';
// -------------------------------------------------------
var bufferContents = function(file, enc, cb) {
if (file.isNull()) {
// return empty file
return cb();
}
if (file.isStream()) {
cb(new gutil.PluginError(PLUGIN_NAME, 'Streaming not supported'));
return;
}
else if (file.isBuffer()) {
sources.push(file.path);
}
cb();
}
// -------------------------------------------------------
var endStream = function (cb) {
// all the input files are in, now convert them
var that = this;
createFont(options, sources, function(o) {
// all the fontforge magic is done.
// the generated files are in the temp folder now
_.map(o.types, function(type) {
var fn = o.full.file + '.' + type;
var f = new File({path: o.fontFilename + "." + type});
f.contents = fs.createReadStream(fn);
that.push(f)
})
cb();
});
}
function gulpWebfont(o) {
options = o || {};
// Creating a stream through which each file will pass
return through.obj(bufferContents, endStream);
}
// Exporting the plugin main function
module.exports = gulpWebfont;