-
Notifications
You must be signed in to change notification settings - Fork 164
/
index.js
164 lines (139 loc) · 4.41 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
const through2 = require("through2");
const clone = require("clone");
const mergeDefaults = require("lodash.defaultsdeep");
const configDefaults = require("require-directory")(module, "config");
const helpers = require("./helpers.js");
const path = require("path");
const File = require("vinyl");
const toIco = require("to-ico");
function favicons(source, options = {}, next) {
if (next) {
return favicons(source, options)
.then(response => next(null, response))
.catch(next);
}
options = mergeDefaults(options, configDefaults.defaults);
const config = clone(configDefaults);
const µ = helpers(options);
function createFavicon(sourceset, properties, name, platformOptions) {
if (path.extname(name) === ".ico") {
return Promise.all(
properties.sizes.map(({ width, height }) =>
createFavicon(
sourceset,
Object.assign({}, properties, { width, height }),
`${width}x${height}.png`,
platformOptions
)
)
).then(results =>
toIco(results.map(({ contents }) => contents)).then(buffer => ({
name,
contents: buffer
}))
);
}
const maximum = Math.max(properties.width, properties.height);
const offset = Math.round((maximum / 100) * platformOptions.offset) || 0;
const mergedProperties = Object.assign({}, properties, platformOptions);
mergedProperties.transparent =
!mergedProperties.background ||
mergedProperties.background === "transparent";
return Promise.all([
µ.Images.create(mergedProperties),
µ.Images.render(sourceset, mergedProperties, offset)
])
.then(([canvas, buffer]) =>
µ.Images.composite(canvas, buffer, mergedProperties, offset, maximum)
)
.then(contents => ({ name, contents }));
}
function createHTML(platform) {
return Promise.all((config.html[platform] || []).map(µ.HTML.render));
}
function createFiles(platform) {
return Promise.all(
Object.keys(config.files[platform] || {}).map(name =>
µ.Files.create(config.files[platform][name], name)
)
);
}
function createFavicons(sourceset, platform) {
const platformOptions = µ.General.preparePlatformOptions(platform);
return Promise.all(
Object.keys(config.icons[platform] || {}).map(name =>
createFavicon(
sourceset,
config.icons[platform][name],
name,
platformOptions
)
)
);
}
function createPlatform(sourceset, platform) {
return Promise.all([
createFavicons(sourceset, platform),
createFiles(platform),
createHTML(platform)
]);
}
async function create(sourceset) {
const responses = [];
const platforms = Object.keys(options.icons)
.filter(platform => options.icons[platform])
.sort((a, b) => {
if (a === "favicons") return -1;
if (b === "favicons") return 1;
return a.localeCompare(b);
});
for (const platform of platforms) {
responses.push(await createPlatform(sourceset, platform));
}
return {
images: [].concat(...responses.map(r => r[0])),
files: [].concat(...responses.map(r => r[1])),
html: [].concat(...responses.map(r => r[2]))
};
}
const result = µ.General.source(source).then(create);
return options.pipeHTML
? result.then(response =>
µ.Files.create(response.html, options.html, true).then(file =>
Object.assign(response, { files: [...response.files, file] })
)
)
: result;
}
function stream(params, handleHtml) {
/* eslint no-invalid-this: 0 */
return through2.obj(function(file, encoding, callback) {
if (file.isNull()) {
return callback(null, file);
}
if (file.isStream()) {
return callback(new Error("Streaming not supported"));
}
favicons(file.contents, params)
.then(({ images, files, html }) => {
for (const asset of [...images, ...files]) {
this.push(
new File({
path: asset.name,
contents: Buffer.isBuffer(asset.contents)
? asset.contents
: new Buffer(asset.contents)
})
);
}
if (handleHtml) {
handleHtml(html);
}
callback(null);
})
.catch(callback);
});
}
module.exports = favicons;
module.exports.config = configDefaults;
module.exports.stream = stream;