-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
109 lines (94 loc) · 2.95 KB
/
gulpfile.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
/**
* Created by Yamada246 on 2016/9/21.
*/
const gulp = require("gulp");
const rename = require("gulp-rename");
const argv = require("minimist")(process.argv.slice(1));
const fs = require("fs");
const iconfont = require("gulp-iconfont");
// var runTimestamp = Math.round(Date.now()/1000);
const consolidate = require("gulp-consolidate");
const { fontName, className, fontPath } = argv;
if(!(fontName && className && fontPath)) {
throw new Error("Missing argv fontName, className or fontPath");
}
gulp.task("Iconfont", async () => {
const iconStream = gulp.src([`svg/${fontName}/*.svg`]).pipe(
iconfont({
fontName,
formats: ["ttf", "eot", "woff"],
normalize: true,
fontHeight: 2500,
})
);
await Promise.all([
new Promise(resolve => {
iconStream.on("glyphs", function(glyphs, options) {
html(glyphs);
glyphmaps(glyphs);
rn();
gulp
.src("templates/template.css")
.pipe(
consolidate("lodash", {
glyphs,
fontName,
fontPath,
className,
})
)
.pipe(rename({
basename: fontName,
}))
.pipe(gulp.dest("dest/css/"))
.on("finish", resolve);
});
}),
new Promise(resolve => {
iconStream.pipe(gulp.dest("dest/fonts/")).on("finish", resolve);
}),
]);
});
gulp.task("default", ["Iconfont"]);
function html(glyphs) {
const dest = __dirname + "/dest/";
const htmlTemplate = `${__dirname}/templates/index.html`;
const destHTML = `${__dirname}/dest/${fontName}.html`;
let content = "";
let html = fs.readFileSync(htmlTemplate).toString();
glyphs.forEach(function(glyphs) {
content += `<div class="container"><i class="${className} ${className}-${glyphs.name}">${fontName}</i><br><i class="${className} ${className}-${glyphs.name}">${fontName}</i></div>`;
});
html = replace(html, {
...argv,
HTML: content,
});
fs.existsSync(dest) || fs.mkdirSync(dest);
fs.writeFileSync(destHTML, html);
}
function rn() {
const dest = `${__dirname}/react-native`;;
const jsTemplate = `${__dirname}/templates/template.js`;
const destJS = `${__dirname}/react-native/${fontName}.js`;
let js = fs.readFileSync(jsTemplate).toString();
js = replace(js, argv);
fs.existsSync(dest) || fs.mkdirSync(dest);
fs.writeFileSync(destJS, js);
}
function glyphmaps(glyphs) {
const dest = `${__dirname}/dest/glyphmaps`;
const destGlyphMap = `${dest}/${fontName}.json`;
const glyphmap = glyphs.reduce((accu, curr) => {
accu[curr.name] = curr.unicode[0].charCodeAt(0);
return accu;
}, Object.create(null));
fs.existsSync(dest) || fs.mkdirSync(dest);
fs.writeFileSync(destGlyphMap, JSON.stringify(glyphmap));
}
function replace(str, data) {
const temp = str.replace(/<%=\w+%>/g, item => {
item = item.match(/<%=(.*?)%>/)[1];
return data[item] ? data[item].toString() : '';
});
return temp;
}