-
Notifications
You must be signed in to change notification settings - Fork 36
/
Gulpfile.js
171 lines (152 loc) · 4.28 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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
var gulp = require("gulp");
var buffer = require('vinyl-buffer');
var imageminPngquant = require('imagemin-pngquant');
var imageResize = require("gulp-image-resize");
var zip = require("gulp-zip");
var jade = require("gulp-jade");
var spritesmith = require("gulp.spritesmith");
var request = require("request-promise-native");
var fs = require("fs-extra");
const BUILD_DIR = "./build/";
const VERSION = "2.0.1";
var static = [
"./data/emoji/clock9.png",
"./data/emoji/smiley.png",
"./data/emoji/cherry_blossom.png",
"./data/emoji/bell.png",
"./data/emoji/oncoming_automobile.png",
"./data/emoji/symbols.png",
"./data/emoji/hatched_chick.png",
"./data/emoji/owl.png",
"./data/emoji/heart.png"
];
gulp.task("emoji", function() {
return gulp.src(static).pipe(gulp.dest(BUILD_DIR + "./img/emoji/"));
});
gulp.task("sources", function() {
gulp.src([
"./src/**/*",
"!./src/popup.jade",
"./data/emoji.json",
"./data/sprite.png",
"./manifest.json",
"./icons/icon32.png",
"./icons/icon48.png",
"./icons/icon128.png",
]).pipe(gulp.dest(BUILD_DIR));
});
gulp.task("popup", function() {
let sprite = require("./data/emoji.json");
gulp.src("./src/popup.jade")
.pipe(jade({
locals: { version: VERSION, emojis: sprite }
}))
.pipe(gulp.dest(BUILD_DIR));
});
gulp.task("fetch-images", function() {
return request({
url: "https://api.github.com/emojis",
headers: { "User-Agent": ":unicorn:" },
json: true,
}).then(function(emojis) {
let downloads = [];
for (let id of Object.keys(emojis)) {
downloads.push([
request({
url: emojis[id],
headers: { "User-Agent": ":unicorn:" },
encoding: null,
}).then(function(data) {
return fs.writeFile(`data/emoji/${id}.png`, data);
})
]);
}
return Promise.all(downloads);
});
});
gulp.task("sprite", function () {
return request({
url: "https://raw.githubusercontent.com/github/gemoji/master/db/emoji.json",
headers: { "User-Agent": ":unicorn:" },
json: true,
}).then(function(emojis) {
let categories = {
"People": [],
"Nature": [],
"Foods": [],
"Activity": [],
"Places": [],
"Objects": [],
"Symbols": [],
"Flags": [],
};
for (let emoji of emojis) {
if (emoji.category) {
categories[emoji.category].push({
name: emoji.aliases[0],
unicode: emoji.emoji,
tags: emoji.tags,
});
}
}
return {
"recent": [],
"people": categories.People,
"nature": categories.Nature.concat(categories.Foods),
"objects": categories.Activity.concat(categories.Objects),
"places": categories.Places.concat(categories.Flags),
"symbols": categories.Symbols,
};
}).then(function(emojis) {
var spriteData = gulp.src("./data/emoji/*")
.pipe(imageResize({
width: 46,
height: 46
})).pipe(spritesmith({
imgName: "sprite.png",
cssFormat: "json",
cssTemplate: function (params) {
var coll = params.items.reduce(function (c, item) {
c[item.name] = {
x: item.x,
y: item.y
};
return c;
}, {});
for (let category of Object.keys(emojis)) {
for (let emoji of emojis[category]) {
emoji.x = coll[emoji.name].x;
emoji.y = coll[emoji.name].y;
}
}
return JSON.stringify(emojis);
},
algorithm: "binary-tree",
cssName: "emoji.json"
}));
spriteData.css.pipe(gulp.dest("./data"));
return spriteData.img
.pipe(buffer())
.pipe(imageminPngquant({quality: '0-100', speed: 1})())
.pipe(gulp.dest("./data/"));
});
});
// Rerun the task when a file changes
gulp.task("watch", function() {
gulp.watch("./src/*", ["build"]);
});
gulp.task("release", ["build"], function() {
return gulp.src([BUILD_DIR + "**/*"])
.pipe(zip("emoji-helper.zip"))
.pipe(gulp.dest("./release/latest/"))
.pipe(gulp.dest(`./release/${VERSION}/`));
});
gulp.task("clean", function() {
return Promise.all([
fs.remove(BUILD_DIR),
fs.remove("./release/latest/"),
fs.remove(`./release/${VERSION}/`),
]);
});
gulp.task("build", ["sources", "popup", "emoji"]);
gulp.task("default", ["build", "watch"]);