-
Notifications
You must be signed in to change notification settings - Fork 0
/
adaptive-icon.ts
126 lines (110 loc) · 4.48 KB
/
adaptive-icon.ts
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
import { existsSync, mkdirSync, writeFileSync, readFileSync } from 'node:fs'
import { dirname, join, extname } from 'path'
import svg2vectordrawable from 'svg2vectordrawable'
import { Log, Options } from './types'
const androidXMLFiles = () => [
{
path: 'android/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml',
contents: `<?xml version="1.0" encoding="utf-8"?>
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
<background android:drawable="@drawable/ic_launcher_background" />
<foreground android:drawable="@drawable/ic_launcher_foreground" />
</adaptive-icon>`,
},
{
path: 'android/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml',
contents: `<?xml version="1.0" encoding="utf-8"?>
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
<background android:drawable="@drawable/ic_launcher_background" />
<foreground android:drawable="@drawable/ic_launcher_foreground" />
</adaptive-icon>`,
},
]
const solidBackgroundVector = (color: string) => `<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="108dp"
android:height="108dp"
android:viewportWidth="108"
android:viewportHeight="108">
<path
android:fillColor="#FF${color}"
android:pathData="M0 0h108v108H0z"/>
</vector>`
// Options see https://www.npmjs.com/package/svg2vectordrawable
const convertSVG = (svgContents: string) => svg2vectordrawable(svgContents, { xmlTag: true })
export const getFileType = (fileName?: string) => extname(fileName ?? '').replace('.', '')
const writeResFile = (nativePath: string, path: string, contents: string) => {
const destinationFile = join(nativePath, 'android/app/src/main/res', path)
const directory = dirname(destinationFile)
if (!existsSync(directory)) {
mkdirSync(directory, { recursive: true })
}
writeFileSync(destinationFile, contents)
}
export const generateAndroidAdaptiveIcons = async (
nativePath: string,
projectPath: string,
options: Options,
log: Log
) => {
const xmlFiles = androidXMLFiles()
if (
!options.androidForeground ||
(!options.androidBackground && !options.androidBackgroundColor)
) {
log('Not creating adaptive icons for Android')
return
}
const foregroundType = getFileType(options.androidForeground)
const backgroundType = !options.androidBackgroundColor && getFileType(options.androidBackground)
if (foregroundType !== 'svg' && foregroundType !== 'xml') {
log('"androidForeground" invalid, .svg or .xml file required')
}
if (!options.androidBackgroundColor && backgroundType !== 'svg' && backgroundType !== 'xml') {
log('"androidBackground" invalid, .svg or .xml file required')
}
// Create importing files.
xmlFiles.forEach((file) => {
const destinationFile = join(nativePath, file.path)
const directory = dirname(destinationFile)
if (!existsSync(directory)) {
mkdirSync(directory, { recursive: true })
}
writeFileSync(destinationFile, file.contents)
})
if (foregroundType === 'svg') {
const foregroundSVGContents = readFileSync(
join(projectPath, options.androidForeground),
'utf-8'
)
const foregroundVector = await convertSVG(foregroundSVGContents)
writeResFile(nativePath, 'drawable-v24/ic_launcher_foreground.xml', foregroundVector)
}
if (foregroundType === 'xml') {
const foregroundXMLContents = readFileSync(
join(projectPath, options.androidForeground),
'utf-8'
)
writeResFile(nativePath, 'drawable-v24/ic_launcher_foreground.xml', foregroundXMLContents)
}
if (!options.androidBackgroundColor && backgroundType === 'svg') {
const backgroundSVGContents = readFileSync(
join(projectPath, options.androidBackground as string),
'utf-8'
)
const backgroundVector = await convertSVG(backgroundSVGContents)
writeResFile(nativePath, 'drawable/ic_launcher_background.xml', backgroundVector)
}
if (!options.androidBackgroundColor && backgroundType === 'xml') {
const backgroundXMLContents = readFileSync(
join(projectPath, options.androidBackground as string),
'utf-8'
)
writeResFile(nativePath, 'drawable/ic_launcher_background.xml', backgroundXMLContents)
}
if (options.androidBackgroundColor) {
// Currently only hex colors allowed.
const color = options.androidBackgroundColor.replace('#', '')
writeResFile(nativePath, 'drawable/ic_launcher_background.xml', solidBackgroundVector(color))
}
}