-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathindex.js
More file actions
103 lines (86 loc) · 2.86 KB
/
index.js
File metadata and controls
103 lines (86 loc) · 2.86 KB
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
'use strict';
const path = require('path');
const pathExists = require('path-exists');
const pify = require('pify');
const gm = require('gm');
const mkdir = require('mkdirp');
const pwaIcons = require('pwa-icon-list');
const androidIcons = require('android-icon-list');
const bb10Icons = require('bb10-icon-list');
const iosIcons = require('ios-icon-list');
const execa = require('execa');
const mask = require('./lib/mask');
const mkdirp = pify(mkdir);
const platformIcons = {
pwa: pwaIcons(),
android: androidIcons(),
ios: iosIcons(),
blackberry10: bb10Icons()
};
// See https://material.io/design/platform-guidance/android-icons.html#keyline-shapes
const platformRadius = new Map([
['android', 0.0909],
['pwa', 0.0909]
]);
const calculateDimension = (imgSize, iconSize, opts, resizeFn) => {
let width;
let height;
if (imgSize.width > imgSize.height) {
width = iconSize * opts.contentRatio;
height = imgSize.height / imgSize.width * width;
} else {
height = iconSize * opts.contentRatio;
width = imgSize.width / imgSize.height * height;
}
if (resizeFn === 'density') {
// Calculate the dpi (= 72 * targetSize / srcSize)
width = 72 * width / imgSize.width;
height = 72 * height / imgSize.height;
}
return {width, height};
};
module.exports = (file, opts) => {
if (typeof file !== 'string' || !pathExists.sync(file)) {
return Promise.reject(new TypeError('Expected a file.'));
}
opts = Object.assign({
platform: '',
dest: process.cwd(),
background: 'white',
roundedCorners: platformRadius.has(opts.platform),
borderRadius: platformRadius.get(opts.platform),
contentRatio: 1
}, opts);
if (opts.platform === '') {
return Promise.reject(new Error('Please provide a platform'));
}
if (Object.keys(platformIcons).indexOf(opts.platform.toLowerCase()) === -1) {
return Promise.reject(new Error(`Platform ${opts.platform} is not supported.`));
}
const icons = platformIcons[opts.platform.toLowerCase()];
const resizeFn = path.extname(file) === '.svg' ? 'density' : 'resize';
const img = gm(file);
return pify(img.identify.bind(img))()
.then(identity => {
const {size} = identity;
return Promise.all(icons.map(icon => {
const dest = path.join(opts.dest, icon.file);
const dimension = calculateDimension(size, icon.dimension, opts, resizeFn);
const image = gm(file)[resizeFn](dimension.width, dimension.height)
.gravity('Center')
.background(opts.background)
.extent(icon.dimension, icon.dimension);
return mkdirp(path.dirname(dest))
.then(() => pify(image.write.bind(image))(dest))
.then(() => {
if (opts.roundedCorners) {
return mask(icon.dimension, opts.borderRadius)
.then(maskLocation => {
// Apply the mask and overwrite the original image
return execa('gm', ['composite', '-compose', 'in', dest, maskLocation, dest]);
});
}
});
}));
});
};