-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.js
148 lines (141 loc) · 4.02 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
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
'use strict';
const Base = require('run-plugin-webpack');
const svgiconsToSvgfont = require('svgicons2svgfont');
const Font = require('fonteditor-core').Font;
const fs = require('fs');
const path = require('path');
const svgToTtf = require('svg2ttf');
function shouldReplace(svg) {
try {
fs.accessSync(svg.path, fs.constants ? fs.constants.R_OK : fs.R_OK);
} catch(e) {
return true;
}
const oldFile = fs.readFileSync(svg.path).toString();
const newFile = svg.contents.toString();
return oldFile !== newFile;
}
const Plugin = Base.extends(function(options) {
this.options = this.getOptions(options);
});
Plugin.prototype.getOptions = function(options) {
const opts = Object(options);
if(!opts.src || 'string' !== typeof opts.src) {
throw new TypeError('`src` is invalid!');
}
if(!opts.dest || 'string' !== typeof opts.dest.font || 'string' !== typeof opts.dest.css) {
throw new TypeError('`dest` is invalid!');
}
const src = path.resolve(opts.src);
const dest = {
font: path.resolve(opts.dest.font),
css: path.resolve(opts.dest.css)
};
const cssTemplate = ('function' === typeof opts.cssTemplate
? opts.cssTemplate
: require('./template')
);
return {
src: src,
dest: {
font: dest.font,
css: dest.css,
},
cssTemplate: cssTemplate,
family: ('string' === typeof opts.family && opts.family) || 'iconfont'
};
};
Plugin.prototype.main = function() {
const src = this.options.src;
const readdir = fs.readdirSync(src);
const useMultipleGroups = readdir.some(function(file) {
return fs.lstatSync(path.join(src, file)).isDirectory();
});
const context = this;
const promises = (useMultipleGroups
? readdir.map(function(dir) {
const dirPath = path.join(src, dir);
const files = fs.readdirSync(dirPath).map(function(file) {
return path.resolve(dirPath, file);
});
return context.generateFonts(dir, files);
})
: [ this.generateFonts(this.options.family, readdir.map(function(file) {
return path.resolve(src, file);
})) ]
);
return Promise.all(promises);
};
Plugin.prototype.generateFonts = function(family, files) {
const context = this;
return new Promise(function(resolve, reject) {
const buffer = [];
const unicodes = [];
const fileStream = svgiconsToSvgfont({
fontName: family,
prependUnicode: true,
log: Function.prototype
}).on('data', function(data) {
return buffer.push(data);
}).on('end', function() {
return resolve({
contents: Buffer.concat(buffer).toString(),
unicodes: unicodes
});
}).on('error', function(err) {
return reject(err);
});
let startUnicode = 0xEA01;
files.forEach(function(file) {
const glyph = fs.createReadStream(file);
const unicode = String.fromCharCode(startUnicode++);
const name = path.parse(file).name;
unicodes.push({ name: name, unicode: unicode });
glyph.metadata = {
name: name,
unicode: [ unicode ]
};
fileStream.write(glyph);
});
fileStream.end();
}).then(function(args) {
const ttf = new Buffer(svgToTtf(args.contents.toString()).buffer);
const fontCreator = Font.create(ttf, {
type: 'ttf',
hinting: true,
compound2simple: true,
inflate: null,
combinePath: true
});
const files = ['svg', 'ttf', 'woff', 'eot'].map(function(type) {
const buffer = fontCreator.write({
type: type,
hinting: true,
deflate: null
});
const filePath = context.options.dest.font
.replace(/\[family\]/g, family)
.replace(/\[type\]/g, type);
return { path:filePath, contents: buffer };
}, []);
return { files: files, unicodes: args.unicodes };
}).then(function(args) {
const files = args.files;
const unicodes = args.unicodes;
if(!shouldReplace(files[0])) {
return;
}
files.forEach(function(file) {
return context.addFile(file.path, file.contents);
});
return Promise.resolve(context.options.cssTemplate({
unicodes: unicodes,
family: family
}))
.then(function(cssContent) {
const cssPath = context.options.dest.css.replace(/\[family\]/g, family);
context.addFile(cssPath, cssContent);
});
}).catch(console.dir);
}
module.exports = Plugin;