-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate-manifest.js
62 lines (52 loc) · 1.6 KB
/
generate-manifest.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
import fs from 'fs';
import path from 'path';
import sharp from 'sharp';
import website from './src/lib/config/website.js';
const __dirname = path.resolve();
const iconsDirectory = path.join(__dirname, 'static/icons');
const manifestFile = path.join(__dirname, 'static/manifest.json');
const { backgroundColor, icon, siteShortTitle, siteTitle, themeColor } = website;
const DEFAULT_SIZES = [128, 144, 152, 192, 256, 512];
const iconImage = sharp(icon);
const resizeIcon = async ({ size, path }) => {
await iconImage.resize(size).toFile(path, (err) => {
if (err) {
console.error(err);
}
});
};
const main = async () => {
try {
const { height, width } = await iconImage.metadata();
const maxSize = Math.min(width, height);
const sizes = DEFAULT_SIZES.filter((element) => element <= maxSize);
const manifest = {
name: siteTitle,
short_name: siteShortTitle,
start_url: '/index.html',
background_color: backgroundColor,
theme_color: themeColor,
display: 'standalone',
icons: sizes.map((size) => {
const path = `icons/icon-${size}x${size}.png`;
resizeIcon({ size, path: `static/${path}` });
return {
src: path,
sizes: `${size}x${size}`,
type: 'image/png',
purpose: 'any maskable',
};
}),
};
fs.writeFileSync(manifestFile, JSON.stringify(manifest, null, 2));
} catch (error) {
console.error(error);
}
};
console.log('Generating manifest.json');
fs.mkdir(iconsDirectory, { recursive: true }, (err) => {
if (err) {
return console.error(err);
}
});
main();