forked from itgalaxy/favicons
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
246 lines (208 loc) · 9.1 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
const _ = require('underscore'),
async = require('async'),
through2 = require('through2'),
clone = require('clone'),
mergeDefaults = require('merge-defaults'),
configDefaults = require('require-directory')(module, 'config'),
helpers = require('./helpers-es5.js'),
path = require('path'),
toIco = require('to-ico');
(() => {
'use strict';
_.mergeDefaults = mergeDefaults;
function favicons (source, parameters, next) {
const config = clone(configDefaults),
options = _.mergeDefaults(parameters || config.defaults),
µ = helpers(options),
background = µ.General.background(options.background);
function createFavicon (sourceset, properties, name, platformOptions, callback) {
if (path.extname(name) === '.ico') {
async.map(
properties.sizes,
(sizeProperties, cb) => {
const newProperties = clone(properties);
newProperties.width = sizeProperties.width;
newProperties.height = sizeProperties.height;
const tempName = `favicon-temp-${newProperties.width}x${newProperties.height}.png`;
createFavicon(sourceset, newProperties, tempName, platformOptions, cb);
},
(error, results) => {
if (error) {
return callback(error);
}
const files = results.map((icoImage) => icoImage.contents);
toIco(files)
.then((buffer) => callback(null, { name, contents: buffer }))
.catch(callback);
}
);
} else {
const maximum = Math.max(properties.width, properties.height),
offset = Math.round(maximum / 100 * platformOptions.offset) || 0;
async.waterfall([
(cb) =>
µ.Images.nearest(sourceset, properties, offset, cb),
(nearest, cb) =>
µ.Images.read(nearest.file, cb),
(buffer, cb) =>
µ.Images.resize(buffer, properties, offset, cb),
(resizedBuffer, cb) =>
µ.Images.create(properties, background, (error, canvas) =>
cb(error, resizedBuffer, canvas)),
(resizedBuffer, canvas, cb) =>
µ.Images.composite(canvas, resizedBuffer, properties, offset, maximum, cb),
(composite, cb) => {
µ.Images.getBuffer(composite, cb);
}
], (error, buffer) =>
callback(error, { name, contents: buffer }));
}
}
function createHTML (platform, callback) {
const html = [];
async.forEachOf(config.html[platform], (tag, selector, cb) =>
µ.HTML.parse(tag, (error, metadata) =>
cb(html.push(metadata) && error)),
(error) =>
callback(error, html));
}
function createFiles (platform, callback) {
const files = [];
async.forEachOf(config.files[platform], (properties, name, cb) =>
µ.Files.create(properties, name, (error, file) =>
cb(files.push(file) && error)),
(error) =>
callback(error, files));
}
function createFavicons (sourceset, platform, platformOptions, callback) {
const images = [];
async.forEachOf(config.icons[platform], (properties, name, cb) =>
createFavicon(sourceset, properties, name, platformOptions, (error, image) =>
cb(images.push(image) && error)),
(error) =>
callback(error, images));
}
function createPlatform (sourceset, platform, platformOptions, callback) {
async.parallel([
(cb) =>
createFavicons(sourceset, platform, platformOptions, cb),
(cb) =>
createFiles(platform, cb),
(cb) =>
createHTML(platform, cb)
], (error, results) =>
callback(error, results[0], results[1], results[2]));
}
function createOffline (sourceset, callback) {
const response = { images: [], files: [], html: [] };
async.forEachOf(options.icons, (enabled, platform, cb) => {
const platformOptions = µ.General.preparePlatformOptions(platform, enabled);
if (enabled) {
createPlatform(sourceset, platform, platformOptions, (error, images, files, html) => {
response.images = response.images.concat(images);
response.files = response.files.concat(files);
response.html = response.html.concat(html);
cb(error);
});
} else {
return cb(null);
}
}, (error) =>
callback(error, response));
}
function unpack (pack, callback) {
const response = { images: [], files: [], html: pack.html.split('\n') };
async.each(pack.files, (url, cb) =>
µ.RFG.fetch(url, (error, box) =>
cb(response.images.push(box.image) && response.files.push(box.file) && error)),
(error) =>
callback(error, response));
}
function createOnline (sourceset, callback) {
async.waterfall([
(cb) =>
µ.RFG.configure(sourceset, config.rfg, cb),
(request, cb) =>
µ.RFG.request(request, cb),
(pack, cb) =>
unpack(pack, cb)
], (error, results) => {
if (error && options.preferOnline) {
createOffline(sourceset, callback)
} else {
callback(error, results);
}
});
}
function create (sourceset, callback) {
options.online || options.preferOnline ? createOnline(sourceset, callback) : createOffline(sourceset, callback);
}
async.waterfall([
(callback) =>
µ.General.source(source, callback),
(sourceset, callback) =>
create(sourceset, callback),
(response, callback) => {
if (options.pipeHTML) {
µ.Files.create(response.html, options.html, (error, file) => {
response.files = response.files.concat([file]);
return callback(error, response);
});
} else {
return callback(null, response);
}
}
], (error, response) =>
error ? next(error) : next(null, {
images: _.compact(response.images),
files: _.compact(response.files),
html: _.compact(response.html)
}));
}
function stream (params, handleHtml) {
const config = clone(configDefaults),
µ = helpers(params);
function processDocuments (documents, html, callback) {
async.each(documents, (document, cb) =>
µ.HTML.update(document, html, config.html, cb),
(error) =>
callback(error));
}
/* eslint func-names: 0, no-invalid-this: 0 */
return through2.obj(function (file, encoding, callback) {
const that = this;
if (file.isNull()) {
return callback(null, file);
}
if (file.isStream()) {
return callback(new Error('[gulp-favicons] Streaming not supported'));
}
async.waterfall([
(cb) =>
favicons(file.contents, params, cb),
(response, cb) =>
async.each(response.images.concat(response.files), (image, c) => {
that.push(µ.General.vinyl(image));
c();
}, (error) =>
cb(error, response)),
(response, cb) => {
if (handleHtml) {
handleHtml(response.html);
return cb(null);
}
if (params.html && !params.pipeHTML) {
const documents = typeof params.html === 'object' ? params.html : [params.html];
processDocuments(documents, response.html, cb);
} else {
return cb(null);
}
}
], (error) =>
callback(error));
});
}
module.exports = favicons;
module.exports.config = configDefaults;
module.exports.stream = stream;
})();