-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgenerate-app-icon.cjs
More file actions
247 lines (227 loc) · 5.7 KB
/
generate-app-icon.cjs
File metadata and controls
247 lines (227 loc) · 5.7 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
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
247
/**
* Generate iOS AppIcon.appiconset PNGs + Android mipmap launcher icons from one source.
* Source: largest PNG under assets/bootsplash/ (logo@4x → … → logo.png), else
* assets/app-icon.png, else assets/bootsplash-logo.svg (rasterized to temp).
* Uses sharp (fit: cover, square). Flattens onto #111827 so marketing icon has no transparency.
*/
const fs = require('node:fs')
const os = require('node:os')
const path = require('node:path')
const sharp = require('sharp')
const root = path.join(__dirname, '..')
const SVG_LOGO = path.join(root, 'assets', 'bootsplash-logo.svg')
const APP_ICON = path.join(root, 'assets', 'app-icon.png')
const BOOTSPLASH_DIR = path.join(root, 'assets', 'bootsplash')
/** Prefer largest PNG first (matches typical react-native-bootsplash output names). */
const BOOTSPLASH_SOURCES = [
'logo@1,5x.png',
'logo.png',
]
const BG = { r: 17, g: 24, b: 39 }
const SVG_RASTER_PX = 1024
/** @type {{ px: number; filename: string; idiom: string; size: string; scale: string }[]} */
const IOS_SLOTS = [
{
px: 40,
filename: '[email protected]',
idiom: 'iphone',
size: '20x20',
scale: '2x',
},
{
px: 60,
filename: '[email protected]',
idiom: 'iphone',
size: '20x20',
scale: '3x',
},
{
px: 58,
filename: '[email protected]',
idiom: 'iphone',
size: '29x29',
scale: '2x',
},
{
px: 87,
filename: '[email protected]',
idiom: 'iphone',
size: '29x29',
scale: '3x',
},
{
px: 80,
filename: '[email protected]',
idiom: 'iphone',
size: '40x40',
scale: '2x',
},
{
px: 120,
filename: '[email protected]',
idiom: 'iphone',
size: '40x40',
scale: '3x',
},
{
px: 120,
filename: '[email protected]',
idiom: 'iphone',
size: '60x60',
scale: '2x',
},
{
px: 180,
filename: '[email protected]',
idiom: 'iphone',
size: '60x60',
scale: '3x',
},
{
px: 1024,
filename: '[email protected]',
idiom: 'ios-marketing',
size: '1024x1024',
scale: '1x',
},
]
/** Density folder → launcher side length (px) */
const ANDROID_MAP = [
{ folder: 'mipmap-mdpi', px: 48 },
{ folder: 'mipmap-hdpi', px: 72 },
{ folder: 'mipmap-xhdpi', px: 96 },
{ folder: 'mipmap-xxhdpi', px: 144 },
{ folder: 'mipmap-xxxhdpi', px: 192 },
]
function resolveBootsplashSource() {
for (const name of BOOTSPLASH_SOURCES) {
const p = path.join(BOOTSPLASH_DIR, name)
if (fs.existsSync(p)) return p
}
return null
}
/**
* @returns {{ path: string; cleanup: boolean; label: string } | null}
*/
function resolveSourceSync() {
const fromBootsplash = resolveBootsplashSource()
if (fromBootsplash) {
return {
path: fromBootsplash,
cleanup: false,
label: path.relative(root, fromBootsplash),
}
}
if (fs.existsSync(APP_ICON)) {
return { path: APP_ICON, cleanup: false, label: 'assets/app-icon.png' }
}
if (fs.existsSync(SVG_LOGO)) {
return {
path: SVG_LOGO,
cleanup: true,
label: 'assets/bootsplash-logo.svg',
}
}
return null
}
/**
* @param {string} destPng
*/
async function rasterizeSvgToPng(destPng) {
await sharp(SVG_LOGO)
.resize(SVG_RASTER_PX, SVG_RASTER_PX, {
fit: 'cover',
position: 'centre',
})
.flatten({ background: BG })
.png({ compressionLevel: 9 })
.toFile(destPng)
}
/**
* @param {string} inputPath
* @param {number} px
*/
async function renderIcon(inputPath, px) {
return sharp(inputPath)
.resize(px, px, { fit: 'cover', position: 'centre' })
.flatten({ background: BG })
.png({ compressionLevel: 9 })
.toBuffer()
}
async function main() {
const resolved = resolveSourceSync()
if (!resolved) {
console.error(
'app-icon: missing source. Add PNGs under assets/bootsplash/ ([email protected] … logo.png), assets/app-icon.png, or assets/bootsplash-logo.svg.',
)
process.exit(1)
}
let inputPath = resolved.path
if (resolved.cleanup) {
inputPath = path.join(
os.tmpdir(),
`app-icon-svg-${process.pid}-${Date.now()}.png`,
)
}
try {
if (resolved.cleanup) {
await rasterizeSvgToPng(inputPath)
}
console.log(`app-icon: using source ${resolved.label}`)
const iosDir = path.join(
root,
'ios',
'ReactNativeStarter',
'Images.xcassets',
'AppIcon.appiconset',
)
fs.mkdirSync(iosDir, { recursive: true })
for (const slot of IOS_SLOTS) {
const buf = await renderIcon(inputPath, slot.px)
fs.writeFileSync(path.join(iosDir, slot.filename), buf)
console.log(` iOS ${slot.filename} (${slot.px}px)`)
}
const contents = {
images: IOS_SLOTS.map(s => ({
idiom: s.idiom,
scale: s.scale,
size: s.size,
filename: s.filename,
})),
info: {
author: 'xcode',
version: 1,
},
}
fs.writeFileSync(
path.join(iosDir, 'Contents.json'),
`${JSON.stringify(contents, null, 2)}\n`,
)
const androidRes = path.join(root, 'android', 'app', 'src', 'main', 'res')
for (const { folder, px } of ANDROID_MAP) {
const dir = path.join(androidRes, folder)
fs.mkdirSync(dir, { recursive: true })
const buf = await renderIcon(inputPath, px)
for (const name of ['ic_launcher.png', 'ic_launcher_round.png']) {
fs.writeFileSync(path.join(dir, name), buf)
}
console.log(` Android ${folder} (${px}px)`)
}
console.log('app-icon: done')
} finally {
if (resolved.cleanup) {
try {
fs.unlinkSync(inputPath)
} catch {
// ignore
}
}
}
}
main().catch(err => {
console.error(err)
process.exit(1)
})