forked from itswadesh/svelte-commerce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-manifest.js
87 lines (79 loc) · 1.84 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
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
import fs from 'fs'
import path from 'path'
import sharp from 'sharp'
import {
backgroundColor,
icon,
siteShortTitle,
siteTitle,
themeColor,
shortcuts,
description,
screenshots
} from './src/lib/config'
const __dirname = path.resolve()
const iconsDirectory = path.join(__dirname, 'static/icons')
const manifestFile = path.join(__dirname, 'static/manifest.json')
const DEFAULT_SIZES = [72, 96, 120, 128, 144, 152, 180, 192, 256, 384, 512]
const iconImage = sharp(icon)
const resizeIcon = async ({ size, path }) => {
await iconImage
.resize(size, size, {
kernel: sharp.kernel.nearest,
fit: 'contain',
position: 'center',
background: { r: 255, g: 255, b: 255, alpha: 0.5 }
})
.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 = {
short_name: siteShortTitle,
name: siteTitle,
start_url: '/',
background_color: backgroundColor,
display: 'fullscreen',
scope: '/',
theme_color: themeColor,
description,
icons: sizes.map((size) => {
const path = `icons/icon-${size}x${size}.png`
resizeIcon({ size, path: `static/${path}` })
if (size === 180) {
return {
src: path,
type: 'image/png',
sizes: `${size}x${size}`,
purpose: 'maskable'
}
} else {
return {
src: path,
type: 'image/png',
sizes: `${size}x${size}`,
purpose: 'any'
}
}
}),
shortcuts,
screenshots
}
fs.writeFileSync(manifestFile, JSON.stringify(manifest, null, 2))
} catch (error) {
console.error(error)
}
}
fs.mkdir(iconsDirectory, { recursive: true }, (err) => {
if (err) {
// return console.error(err)
}
})
main()