@nativue/core 0.13.0 → 0.14.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +39 -7
- package/README.md +1 -0
- package/android/build.gradle.kts +4 -0
- package/android/nativue.settings.gradle.kts +2 -0
- package/android/src/main/java/dev/nativue/Bridge.kt +66 -7
- package/android/src/main/java/dev/nativue/NativueActivity.kt +12 -5
- package/android/src/main/java/dev/nativue/Permissions.kt +107 -0
- package/android/src/main/java/dev/nativue/Plugins.kt +98 -15
- package/cli/android-project.mjs +377 -0
- package/cli/app-config.mjs +526 -0
- package/cli/config.d.mts +162 -3
- package/cli/ios-project.mjs +652 -0
- package/cli/link.mjs +66 -71
- package/cli/nativue.mjs +39 -5
- package/cli/plugins.mjs +236 -0
- package/cli/prebuild.mjs +458 -0
- package/cli/run.mjs +2 -2
- package/cli/serialize.mjs +168 -0
- package/ios/AppLifecycle.swift +165 -0
- package/ios/Bridge.swift +53 -4
- package/ios/NativueView.swift +5 -1
- package/ios/Permissions.swift +19 -0
- package/ios/Plugins.swift +24 -0
- package/package.json +3 -2
- package/src/globals.ts +7 -1
- package/src/native-events.ts +99 -0
- package/src/native-functions.ts +16 -2
- package/src/permissions.ts +57 -0
- package/src/renderer.ts +5 -1
- package/src/router.ts +2 -1
- package/src/types.ts +3 -0
- package/template/android/app/src/debug/AndroidManifest.xml +4 -0
- package/template/android/app/src/main/java/MainActivity.kt +5 -0
- package/template/android/app/src/main/res/drawable-night-xxxhdpi/splash_logo.png +0 -0
- package/template/android/app/src/main/res/drawable-xxxhdpi/splash_logo.png +0 -0
- package/template/android/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml +6 -0
- package/template/android/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png +0 -0
- package/template/android/app/src/main/res/mipmap-xxxhdpi/ic_launcher_foreground.png +0 -0
- package/template/android/app/src/main/res/values/colors.xml +4 -0
- package/template/android/app/src/main/res/values/styles.xml +15 -0
- package/template/android/app/src/main/res/values-night/styles.xml +5 -0
- package/template/android/gradle/wrapper/gradle-wrapper.jar +0 -0
- package/template/android/gradle/wrapper/gradle-wrapper.properties +9 -0
- package/template/android/gradlew +248 -0
- package/template/android/gradlew.bat +112 -0
- package/template/ios/App/App.swift +18 -0
- package/template/ios/App/Assets.xcassets/AppIcon.appiconset/Contents.json +1 -0
- package/template/ios/App/Assets.xcassets/AppIcon.appiconset/icon.png +0 -0
- package/template/ios/App/Assets.xcassets/Contents.json +1 -0
- package/template/ios/App/Assets.xcassets/LaunchLogo.imageset/Contents.json +11 -0
- package/template/ios/App/Assets.xcassets/LaunchLogo.imageset/logo-dark@1x.png +0 -0
- package/template/ios/App/Assets.xcassets/LaunchLogo.imageset/logo-dark@2x.png +0 -0
- package/template/ios/App/Assets.xcassets/LaunchLogo.imageset/logo-dark@3x.png +0 -0
- package/template/ios/App/Assets.xcassets/LaunchLogo.imageset/logo@1x.png +0 -0
- package/template/ios/App/Assets.xcassets/LaunchLogo.imageset/logo@2x.png +0 -0
- package/template/ios/App/Assets.xcassets/LaunchLogo.imageset/logo@3x.png +0 -0
- package/template/ios/App.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved +15 -0
|
@@ -0,0 +1,526 @@
|
|
|
1
|
+
// nativue.config.ts as prebuild reads it: every problem at once, then the config with its defaults.
|
|
2
|
+
import { existsSync, readFileSync, statSync } from 'node:fs'
|
|
3
|
+
import { basename, extname, relative, resolve, sep } from 'node:path'
|
|
4
|
+
|
|
5
|
+
const IOS_BUNDLE_IDENTIFIER = /^[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)+$/
|
|
6
|
+
const APPLE_TEAM_ID = /^[A-Z0-9]{10}$/
|
|
7
|
+
const ANDROID_PACKAGE = /^[a-zA-Z][a-zA-Z0-9_]*(\.[a-zA-Z][a-zA-Z0-9_]*)+$/
|
|
8
|
+
const ANDROID_PERMISSION = /^[a-zA-Z][a-zA-Z0-9_]*(\.[a-zA-Z0-9_]+)+$/
|
|
9
|
+
const URL_SCHEME = /^[a-zA-Z][a-zA-Z0-9+.-]*$/
|
|
10
|
+
// CFBundleShortVersionString: "three period-separated integers".
|
|
11
|
+
const VERSION = /^\d+\.\d+\.\d+$/
|
|
12
|
+
// Google Play's greatest versionCode.
|
|
13
|
+
const MAXIMUM_BUILD_NUMBER = 2100000000
|
|
14
|
+
const COLOR = /^#[0-9a-fA-F]{6}$/
|
|
15
|
+
const SECRET_NAME = /^[A-Za-z_][A-Za-z0-9_.]*$/
|
|
16
|
+
const GRADLE_PROPERTY = /^[A-Za-z_][A-Za-z0-9_.-]*$/
|
|
17
|
+
// aapt2's rule for the files of res/: lowercase letters, digits and underscores.
|
|
18
|
+
const ANDROID_RESOURCE_NAME = /^[a-z][a-z0-9_]*$/
|
|
19
|
+
// aapt2 turns resource names into Java fields (R.raw.chime): it refuses Java's keywords.
|
|
20
|
+
const JAVA_KEYWORDS = new Set(
|
|
21
|
+
`abstract assert boolean break byte case catch char class const continue default do double else enum
|
|
22
|
+
extends false final finally float for goto if implements import instanceof int interface long native
|
|
23
|
+
new null package private protected public return short static strictfp super switch synchronized this
|
|
24
|
+
throw throws transient true try void volatile while`.split(/\s+/),
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
function isAndroidResourceName(name) {
|
|
28
|
+
return ANDROID_RESOURCE_NAME.test(name) && !JAVA_KEYWORDS.has(name)
|
|
29
|
+
}
|
|
30
|
+
const ANDROID_RESOURCE_TYPES = ['raw', 'font', 'drawable', 'xml', 'assets']
|
|
31
|
+
const APP_GROUP = /^group\.[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*$/
|
|
32
|
+
const EXTENSION_NAME = /^[A-Za-z][A-Za-z0-9_]*$/
|
|
33
|
+
const BUNDLE_IDENTIFIER_SUFFIX = /^(\.[a-zA-Z0-9-]+)+$/
|
|
34
|
+
const DEPLOYMENT_TARGET = /^\d+\.\d+(\.\d+)?$/
|
|
35
|
+
/** An extension target's type, and the NSExtensionPointIdentifier iOS knows it by. */
|
|
36
|
+
export const EXTENSION_POINTS = {
|
|
37
|
+
'notification-service': 'com.apple.usernotifications.service',
|
|
38
|
+
'notification-content': 'com.apple.usernotifications.content-extension',
|
|
39
|
+
widget: 'com.apple.widgetkit-extension',
|
|
40
|
+
share: 'com.apple.share-services',
|
|
41
|
+
}
|
|
42
|
+
const PNG_SIGNATURE = '89504e470d0a1a0a'
|
|
43
|
+
// IHDR color types with an alpha channel: grayscale and RGB with alpha.
|
|
44
|
+
const PNG_ALPHA_COLOR_TYPES = new Set([4, 6])
|
|
45
|
+
// The logo shown on the launch screen: 120 points wide on iOS, as @3x.
|
|
46
|
+
const SPLASH_IMAGE_SIZE = 360
|
|
47
|
+
const ICON_SIZE = 1024
|
|
48
|
+
|
|
49
|
+
/** The fields only generated projects use, which `prebuild: false` can't have. */
|
|
50
|
+
const GENERATED_PROJECT_FIELDS = [
|
|
51
|
+
'name',
|
|
52
|
+
'scheme',
|
|
53
|
+
'version',
|
|
54
|
+
'buildNumber',
|
|
55
|
+
'icon',
|
|
56
|
+
'splash',
|
|
57
|
+
'ios',
|
|
58
|
+
'android',
|
|
59
|
+
'resources',
|
|
60
|
+
]
|
|
61
|
+
|
|
62
|
+
const DEFAULTS = {
|
|
63
|
+
version: '1.0.0',
|
|
64
|
+
buildNumber: 1,
|
|
65
|
+
splashBackgroundColor: '#FFFFFF',
|
|
66
|
+
// Ink, from the template's own icon and splash.
|
|
67
|
+
darkSplashBackgroundColor: '#0B1220',
|
|
68
|
+
iconBackgroundColor: '#0B1220',
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function isObject(value) {
|
|
72
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value)
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function isPlistValue(value) {
|
|
76
|
+
if (['string', 'number', 'boolean'].includes(typeof value)) return true
|
|
77
|
+
if (Array.isArray(value)) return value.every(isPlistValue)
|
|
78
|
+
|
|
79
|
+
return isObject(value) && Object.values(value).every(isPlistValue)
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function plistProblem(field, value, example) {
|
|
83
|
+
return check(
|
|
84
|
+
value === undefined || (isObject(value) && isPlistValue(value)),
|
|
85
|
+
`${field}: strings, numbers, booleans, lists and objects, e.g. ${example}`,
|
|
86
|
+
)
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** A PNG's size and whether it has an alpha channel, or null for any other file. */
|
|
90
|
+
function pngHeader(data) {
|
|
91
|
+
if (data.length < 26 || data.subarray(0, 8).toString('hex') !== PNG_SIGNATURE) return null
|
|
92
|
+
|
|
93
|
+
return {
|
|
94
|
+
width: data.readUInt32BE(16),
|
|
95
|
+
height: data.readUInt32BE(20),
|
|
96
|
+
hasAlpha: PNG_ALPHA_COLOR_TYPES.has(data[25]),
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function imageProblem(field, path, appDirectory, size, { opaque = false } = {}) {
|
|
101
|
+
if (path === undefined) return null
|
|
102
|
+
|
|
103
|
+
const expected = size ? `a ${size}×${size} PNG` : 'a square PNG'
|
|
104
|
+
const fullPath = resolve(appDirectory, String(path))
|
|
105
|
+
|
|
106
|
+
if (!existsSync(fullPath) || !statSync(fullPath).isFile()) {
|
|
107
|
+
return `${field}: ${path} isn't a file (${expected}, relative to the app)`
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const actual = pngHeader(readFileSync(fullPath))
|
|
111
|
+
const fits =
|
|
112
|
+
actual !== null &&
|
|
113
|
+
actual.width === actual.height &&
|
|
114
|
+
(size === undefined || actual.width === size)
|
|
115
|
+
|
|
116
|
+
// The App Store rejects an app icon with an alpha channel.
|
|
117
|
+
if (fits && opaque && actual.hasAlpha) {
|
|
118
|
+
return `${field}: ${path} must have no transparency: export it without an alpha channel`
|
|
119
|
+
}
|
|
120
|
+
if (fits) return null
|
|
121
|
+
|
|
122
|
+
const found = actual ? `${actual.width}×${actual.height}` : 'not a PNG'
|
|
123
|
+
|
|
124
|
+
return `${field}: ${path} must be ${expected}, it's ${found}`
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function check(isValid, problem) {
|
|
128
|
+
return isValid ? null : problem
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function colorProblem(field, value) {
|
|
132
|
+
return check(value === undefined || COLOR.test(value), `${field}: a color like '#0B1220'`)
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function schemeProblems(scheme) {
|
|
136
|
+
const schemes = [scheme].flat()
|
|
137
|
+
|
|
138
|
+
return check(
|
|
139
|
+
schemes.length > 0 && schemes.every((each) => URL_SCHEME.test(each ?? '')),
|
|
140
|
+
"scheme: e.g. scheme: 'myapp', for myapp:// links, or a list of them",
|
|
141
|
+
)
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/** ios/ and android/ are generated: prebuild --force would delete a keystore kept there. */
|
|
145
|
+
function isInGeneratedFolder(path, appDirectory) {
|
|
146
|
+
const [folder] = relative(appDirectory, resolve(appDirectory, path)).split(sep)
|
|
147
|
+
|
|
148
|
+
// macOS and Windows don't tell Android/ from android/.
|
|
149
|
+
return ['ios', 'android'].includes(folder.toLowerCase())
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function signingProblems(signing, appDirectory) {
|
|
153
|
+
if (signing === undefined) return []
|
|
154
|
+
|
|
155
|
+
return [
|
|
156
|
+
check(
|
|
157
|
+
typeof signing.storeFile === 'string' &&
|
|
158
|
+
!isInGeneratedFolder(signing.storeFile, appDirectory),
|
|
159
|
+
"android.signing.storeFile: the keystore, outside ios/ and android/, e.g. './credentials/upload.keystore'",
|
|
160
|
+
),
|
|
161
|
+
check(typeof signing.keyAlias === 'string', "android.signing.keyAlias: e.g. 'upload'"),
|
|
162
|
+
...['storePasswordEnv', 'keyPasswordEnv'].map((field) =>
|
|
163
|
+
check(
|
|
164
|
+
SECRET_NAME.test(signing[field] ?? ''),
|
|
165
|
+
`android.signing.${field}: the name of the environment variable or Gradle property holding it, e.g. 'ANDROID_STORE_PASSWORD'`,
|
|
166
|
+
),
|
|
167
|
+
),
|
|
168
|
+
]
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function isStringList(value) {
|
|
172
|
+
return Array.isArray(value) && value.every((item) => typeof item === 'string' && item !== '')
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
function isStringRecord(value, keyPattern = /./) {
|
|
176
|
+
return (
|
|
177
|
+
isObject(value) &&
|
|
178
|
+
Object.entries(value).every(
|
|
179
|
+
([key, item]) => keyPattern.test(key) && typeof item === 'string' && !item.includes('\n'),
|
|
180
|
+
)
|
|
181
|
+
)
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
export function emptyGradle() {
|
|
185
|
+
return {
|
|
186
|
+
plugins: [],
|
|
187
|
+
dependencies: [],
|
|
188
|
+
manifestPlaceholders: {},
|
|
189
|
+
repositories: [],
|
|
190
|
+
properties: {},
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
function isText(value) {
|
|
195
|
+
return typeof value === 'string' && value !== '' && !value.includes('\n')
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/** android.gradle, or what the plugins' gradle mods made (field: where it comes from). */
|
|
199
|
+
export function gradleProblems(gradle, field = 'android.gradle') {
|
|
200
|
+
if (gradle === undefined) return []
|
|
201
|
+
|
|
202
|
+
const { plugins, dependencies, repositories, manifestPlaceholders, properties } = gradle
|
|
203
|
+
|
|
204
|
+
return [
|
|
205
|
+
check(
|
|
206
|
+
plugins === undefined ||
|
|
207
|
+
(Array.isArray(plugins) &&
|
|
208
|
+
plugins.every(
|
|
209
|
+
(plugin) =>
|
|
210
|
+
isText(plugin?.id) && (plugin.version === undefined || isText(plugin.version)),
|
|
211
|
+
)),
|
|
212
|
+
`${field}.plugins: e.g. [{ id: 'com.google.gms.google-services', version: '4.4.2' }]`,
|
|
213
|
+
),
|
|
214
|
+
check(
|
|
215
|
+
dependencies === undefined || (Array.isArray(dependencies) && dependencies.every(isText)),
|
|
216
|
+
`${field}.dependencies: e.g. ['com.squareup.okhttp3:okhttp:4.12.0']`,
|
|
217
|
+
),
|
|
218
|
+
check(
|
|
219
|
+
repositories === undefined ||
|
|
220
|
+
(Array.isArray(repositories) &&
|
|
221
|
+
repositories.every((url) => isText(url) && url.startsWith('https://'))),
|
|
222
|
+
`${field}.repositories: Maven URLs, e.g. ['https://jitpack.io']`,
|
|
223
|
+
),
|
|
224
|
+
check(
|
|
225
|
+
manifestPlaceholders === undefined || isStringRecord(manifestPlaceholders),
|
|
226
|
+
`${field}.manifestPlaceholders: e.g. { appAuthRedirectScheme: 'myapp' }`,
|
|
227
|
+
),
|
|
228
|
+
check(
|
|
229
|
+
properties === undefined || isStringRecord(properties, GRADLE_PROPERTY),
|
|
230
|
+
`${field}.properties: e.g. { 'android.enableJetifier': 'true' }`,
|
|
231
|
+
),
|
|
232
|
+
]
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
function isDirectory(path) {
|
|
236
|
+
return existsSync(path) && statSync(path).isDirectory()
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
function extensionTargetProblems(target, field, { appDirectory, appGroups }) {
|
|
240
|
+
const { sources } = target
|
|
241
|
+
|
|
242
|
+
return [
|
|
243
|
+
check(
|
|
244
|
+
typeof sources === 'string' && isDirectory(resolve(appDirectory, sources)),
|
|
245
|
+
`${field}.sources: the folder of the extension's Swift files`,
|
|
246
|
+
),
|
|
247
|
+
check(
|
|
248
|
+
Object.keys(EXTENSION_POINTS).includes(target.type),
|
|
249
|
+
`${field}.type: one of ${Object.keys(EXTENSION_POINTS).join(', ')}`,
|
|
250
|
+
),
|
|
251
|
+
check(
|
|
252
|
+
BUNDLE_IDENTIFIER_SUFFIX.test(target.bundleIdSuffix ?? ''),
|
|
253
|
+
`${field}.bundleIdSuffix: added to the app's bundle identifier, e.g. '.${target.name}'`,
|
|
254
|
+
),
|
|
255
|
+
check(
|
|
256
|
+
target.deploymentTarget === undefined || DEPLOYMENT_TARGET.test(target.deploymentTarget),
|
|
257
|
+
`${field}.deploymentTarget: e.g. '16.0'`,
|
|
258
|
+
),
|
|
259
|
+
plistProblem(`${field}.infoPlist`, target.infoPlist, '{ NSExtension: { ... } }'),
|
|
260
|
+
plistProblem(
|
|
261
|
+
`${field}.entitlements`,
|
|
262
|
+
target.entitlements,
|
|
263
|
+
"{ 'aps-environment': 'production' }",
|
|
264
|
+
),
|
|
265
|
+
check(
|
|
266
|
+
target.appGroups === undefined || typeof target.appGroups === 'boolean',
|
|
267
|
+
`${field}.appGroups: true to share ios.appGroups with the app`,
|
|
268
|
+
),
|
|
269
|
+
check(
|
|
270
|
+
target.appGroups !== true || appGroups.length > 0,
|
|
271
|
+
`${field}.appGroups: shares ios.appGroups, which nativue.config.ts doesn't set, e.g. ios: { appGroups: ['group.com.company.myapp'] }`,
|
|
272
|
+
),
|
|
273
|
+
check(
|
|
274
|
+
target.buildSettings === undefined || isStringRecord(target.buildSettings),
|
|
275
|
+
`${field}.buildSettings: e.g. { SWIFT_VERSION: '6.0' }`,
|
|
276
|
+
),
|
|
277
|
+
]
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/** Throws with every problem of the extension targets the plugins and native/extensions add. */
|
|
281
|
+
/** appGroups: the app's ios.appGroups, which targets with appGroups: true share. */
|
|
282
|
+
export function validateExtensionTargets(targets, { appDirectory, appGroups = [] }) {
|
|
283
|
+
const names = targets.map((target) => target?.name)
|
|
284
|
+
// macOS doesn't tell Extensions/Widget from Extensions/widget.
|
|
285
|
+
const folderNames = names.map((name) => String(name).toLowerCase())
|
|
286
|
+
const problems = targets.flatMap((target, index) => {
|
|
287
|
+
const name = names[index]
|
|
288
|
+
const field = `ios target ${typeof name === 'string' ? name : index}`
|
|
289
|
+
|
|
290
|
+
if (!isObject(target)) return [`${field}: e.g. { name: 'NotificationService', ... }`]
|
|
291
|
+
|
|
292
|
+
return [
|
|
293
|
+
check(
|
|
294
|
+
EXTENSION_NAME.test(name ?? '') &&
|
|
295
|
+
!['app', 'nativueplugins'].includes(folderNames[index]) &&
|
|
296
|
+
folderNames.indexOf(folderNames[index]) === index,
|
|
297
|
+
`${field}.name: letters, digits and underscores, other than App, NativuePlugins and the other targets' (whatever their case)`,
|
|
298
|
+
),
|
|
299
|
+
...extensionTargetProblems(target, field, { appDirectory, appGroups }),
|
|
300
|
+
]
|
|
301
|
+
})
|
|
302
|
+
const found = problems.filter(Boolean)
|
|
303
|
+
|
|
304
|
+
if (found.length === 0) return
|
|
305
|
+
|
|
306
|
+
throw new Error(
|
|
307
|
+
`The app's extension targets have invalid fields:\n${found.map((problem) => ` ${problem}`).join('\n')}`,
|
|
308
|
+
)
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/** A resource with its defaults: where it lands on each platform, or false for none. */
|
|
312
|
+
function resourceWithDefaults(resource) {
|
|
313
|
+
const fileName = basename(String(resource.source))
|
|
314
|
+
const android = resource.android !== false && {
|
|
315
|
+
type: resource.font ? 'font' : 'raw',
|
|
316
|
+
...resource.android,
|
|
317
|
+
}
|
|
318
|
+
const defaultAndroidName =
|
|
319
|
+
android.type === 'assets' ? fileName : basename(fileName, extname(fileName))
|
|
320
|
+
|
|
321
|
+
return {
|
|
322
|
+
source: resource.source,
|
|
323
|
+
font: resource.font === true,
|
|
324
|
+
ios: resource.ios !== false && { destination: fileName, ...resource.ios },
|
|
325
|
+
android: android && { ...android, name: android.name ?? defaultAndroidName },
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
/** The plugins' resources, then the app's: a later one at the same place replaces an earlier one. */
|
|
330
|
+
export function appResources(config, plugins = []) {
|
|
331
|
+
return [...plugins.flatMap((plugin) => plugin.resources ?? []), ...(config.resources ?? [])].map(
|
|
332
|
+
resourceWithDefaults,
|
|
333
|
+
)
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/** The fonts' file names, once: a later resource at the same place replaces an earlier one. */
|
|
337
|
+
export function iosFonts(resources) {
|
|
338
|
+
const byDestination = new Map(
|
|
339
|
+
resources.filter(({ ios }) => ios).map((resource) => [resource.ios.destination, resource]),
|
|
340
|
+
)
|
|
341
|
+
|
|
342
|
+
return [...byDestination.values()].filter(({ font }) => font).map(({ ios }) => ios.destination)
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
/** Where a resource lands in android/app/src/main. */
|
|
346
|
+
export function resourceFile({ source, android }) {
|
|
347
|
+
return android.type === 'assets'
|
|
348
|
+
? `assets/${android.name}`
|
|
349
|
+
: `res/${android.type}/${android.name}${extname(source)}`
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
function resourceProblems(resource, field, appDirectory) {
|
|
353
|
+
if (!isObject(resource) || typeof resource.source !== 'string') {
|
|
354
|
+
return [`${field}: e.g. { source: './assets/chime.wav' }`]
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
const { source, ios, android } = resourceWithDefaults(resource)
|
|
358
|
+
const fullPath = resolve(appDirectory, source)
|
|
359
|
+
|
|
360
|
+
return [
|
|
361
|
+
check(
|
|
362
|
+
existsSync(fullPath) && statSync(fullPath).isFile(),
|
|
363
|
+
`${field}.source: ${source} isn't a file`,
|
|
364
|
+
),
|
|
365
|
+
check(
|
|
366
|
+
!ios || /^(?!\.\.?$)[^/\\]+$/.test(ios.destination),
|
|
367
|
+
`${field}.ios.destination: a file name, e.g. 'chime.wav'`,
|
|
368
|
+
),
|
|
369
|
+
check(
|
|
370
|
+
!android || ANDROID_RESOURCE_TYPES.includes(android.type),
|
|
371
|
+
`${field}.android.type: one of ${ANDROID_RESOURCE_TYPES.join(', ')}`,
|
|
372
|
+
),
|
|
373
|
+
check(
|
|
374
|
+
!android ||
|
|
375
|
+
(android.type === 'assets'
|
|
376
|
+
? android.name.split('/').every((part) => part !== '' && part !== '.' && part !== '..')
|
|
377
|
+
: isAndroidResourceName(android.name)),
|
|
378
|
+
`${field}.android.name: lowercase letters, digits and underscores, not a Java keyword, e.g. '${basename(
|
|
379
|
+
source,
|
|
380
|
+
extname(source),
|
|
381
|
+
)
|
|
382
|
+
.toLowerCase()
|
|
383
|
+
.replace(/[^a-z0-9_]/g, '_')}'`,
|
|
384
|
+
),
|
|
385
|
+
]
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
/** The app's resources and each plugin's, named after where they come from. */
|
|
389
|
+
function allResourceProblems(config, plugins, appDirectory) {
|
|
390
|
+
const sources = [
|
|
391
|
+
...plugins.map((plugin) => [`${plugin.name}'s resources`, plugin.resources]),
|
|
392
|
+
['resources', config.resources],
|
|
393
|
+
]
|
|
394
|
+
|
|
395
|
+
return sources.flatMap(([field, resources]) => {
|
|
396
|
+
if (resources === undefined) return []
|
|
397
|
+
if (!Array.isArray(resources))
|
|
398
|
+
return [`${field}: a list, e.g. [{ source: './assets/chime.wav' }]`]
|
|
399
|
+
|
|
400
|
+
return resources.flatMap((resource, index) =>
|
|
401
|
+
resourceProblems(resource, `${field}[${index}]`, appDirectory),
|
|
402
|
+
)
|
|
403
|
+
})
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
function generatedProjectProblems(config, appDirectory, plugins) {
|
|
407
|
+
const { splash, buildNumber } = config
|
|
408
|
+
const ios = config.ios ?? {}
|
|
409
|
+
const android = config.android ?? {}
|
|
410
|
+
|
|
411
|
+
return [
|
|
412
|
+
check(
|
|
413
|
+
typeof config.name === 'string' && config.name.trim() !== '',
|
|
414
|
+
"name: the app's name, e.g. name: 'My App'",
|
|
415
|
+
),
|
|
416
|
+
schemeProblems(config.scheme),
|
|
417
|
+
check(
|
|
418
|
+
config.version === undefined || VERSION.test(config.version),
|
|
419
|
+
"version: three numbers, e.g. version: '1.2.0'",
|
|
420
|
+
),
|
|
421
|
+
check(
|
|
422
|
+
buildNumber === undefined ||
|
|
423
|
+
(Number.isInteger(buildNumber) && buildNumber > 0 && buildNumber <= MAXIMUM_BUILD_NUMBER),
|
|
424
|
+
`buildNumber: a whole number from 1 to ${MAXIMUM_BUILD_NUMBER}, raised for every release`,
|
|
425
|
+
),
|
|
426
|
+
imageProblem('icon', config.icon, appDirectory, ICON_SIZE, { opaque: true }),
|
|
427
|
+
imageProblem('splash.image', splash?.image, appDirectory, SPLASH_IMAGE_SIZE),
|
|
428
|
+
imageProblem('splash.darkImage', splash?.darkImage, appDirectory, SPLASH_IMAGE_SIZE),
|
|
429
|
+
colorProblem('splash.backgroundColor', splash?.backgroundColor),
|
|
430
|
+
colorProblem('splash.darkBackgroundColor', splash?.darkBackgroundColor),
|
|
431
|
+
check(
|
|
432
|
+
splash?.darkImage === undefined || splash.image !== undefined,
|
|
433
|
+
'splash.darkImage: needs splash.image, shown in light mode',
|
|
434
|
+
),
|
|
435
|
+
check(
|
|
436
|
+
IOS_BUNDLE_IDENTIFIER.test(ios.bundleIdentifier ?? ''),
|
|
437
|
+
"ios.bundleIdentifier: e.g. ios: { bundleIdentifier: 'com.company.myapp' }",
|
|
438
|
+
),
|
|
439
|
+
check(
|
|
440
|
+
ios.appleTeamId === undefined || APPLE_TEAM_ID.test(ios.appleTeamId),
|
|
441
|
+
'ios.appleTeamId: the 10 characters of your team id, from Signing & Capabilities in Xcode',
|
|
442
|
+
),
|
|
443
|
+
plistProblem('ios.infoPlist', ios.infoPlist, "{ NSCameraUsageDescription: 'Scans receipts' }"),
|
|
444
|
+
plistProblem('ios.entitlements', ios.entitlements, "{ 'aps-environment': 'development' }"),
|
|
445
|
+
check(
|
|
446
|
+
ios.appGroups === undefined ||
|
|
447
|
+
(Array.isArray(ios.appGroups) && ios.appGroups.every((group) => APP_GROUP.test(group))),
|
|
448
|
+
"ios.appGroups: e.g. ['group.com.company.myapp']",
|
|
449
|
+
),
|
|
450
|
+
check(
|
|
451
|
+
ANDROID_PACKAGE.test(android.package ?? ''),
|
|
452
|
+
"android.package: e.g. android: { package: 'com.company.myapp' }, without hyphens",
|
|
453
|
+
),
|
|
454
|
+
check(
|
|
455
|
+
android.permissions === undefined ||
|
|
456
|
+
(Array.isArray(android.permissions) &&
|
|
457
|
+
android.permissions.every((permission) => ANDROID_PERMISSION.test(permission))),
|
|
458
|
+
"android.permissions: full names, e.g. ['android.permission.CAMERA']",
|
|
459
|
+
),
|
|
460
|
+
check(
|
|
461
|
+
android.blockedPermissions === undefined ||
|
|
462
|
+
(Array.isArray(android.blockedPermissions) &&
|
|
463
|
+
android.blockedPermissions.every((permission) => ANDROID_PERMISSION.test(permission))),
|
|
464
|
+
"android.blockedPermissions: full names, e.g. ['android.permission.READ_MEDIA_IMAGES']",
|
|
465
|
+
),
|
|
466
|
+
check(
|
|
467
|
+
android.adaptiveIcon === undefined || android.adaptiveIcon.foregroundImage !== undefined,
|
|
468
|
+
"android.adaptiveIcon.foregroundImage: the icon's square PNG, e.g. './assets/adaptive-icon.png'",
|
|
469
|
+
),
|
|
470
|
+
imageProblem(
|
|
471
|
+
'android.adaptiveIcon.foregroundImage',
|
|
472
|
+
android.adaptiveIcon?.foregroundImage,
|
|
473
|
+
appDirectory,
|
|
474
|
+
),
|
|
475
|
+
colorProblem('android.adaptiveIcon.backgroundColor', android.adaptiveIcon?.backgroundColor),
|
|
476
|
+
...signingProblems(android.signing, appDirectory),
|
|
477
|
+
...gradleProblems(android.gradle),
|
|
478
|
+
...allResourceProblems(config, plugins, appDirectory),
|
|
479
|
+
]
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
function manualProjectProblems(config) {
|
|
483
|
+
return GENERATED_PROJECT_FIELDS.filter((field) => field in config).map(
|
|
484
|
+
(field) =>
|
|
485
|
+
`${field}: nativue uses it only to generate ios/ and android/, which prebuild: false keeps by hand. Set it in those folders instead`,
|
|
486
|
+
)
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
/** Throws with every problem of the config at once, before anything is generated. */
|
|
490
|
+
export function validateConfig(config, appDirectory, plugins = []) {
|
|
491
|
+
const problems = (
|
|
492
|
+
config.prebuild === false
|
|
493
|
+
? manualProjectProblems(config)
|
|
494
|
+
: generatedProjectProblems(config, appDirectory, plugins)
|
|
495
|
+
).filter(Boolean)
|
|
496
|
+
|
|
497
|
+
if (problems.length === 0) return
|
|
498
|
+
|
|
499
|
+
throw new Error(
|
|
500
|
+
`nativue.config.ts has invalid fields:\n${problems.map((problem) => ` ${problem}`).join('\n')}`,
|
|
501
|
+
)
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
/** The config with its defaults, as the project generators read it. */
|
|
505
|
+
export function appConfig(config) {
|
|
506
|
+
const splash = config.splash ?? {}
|
|
507
|
+
const splashBackgroundColor = splash.backgroundColor ?? DEFAULTS.splashBackgroundColor
|
|
508
|
+
|
|
509
|
+
return {
|
|
510
|
+
...config,
|
|
511
|
+
schemes: [config.scheme].flat(),
|
|
512
|
+
version: config.version ?? DEFAULTS.version,
|
|
513
|
+
buildNumber: config.buildNumber ?? DEFAULTS.buildNumber,
|
|
514
|
+
splash: {
|
|
515
|
+
image: splash.image,
|
|
516
|
+
darkImage: splash.darkImage ?? splash.image,
|
|
517
|
+
backgroundColor: splashBackgroundColor,
|
|
518
|
+
// The template's dark logo is made for Ink: keep it unless the app has its own logo.
|
|
519
|
+
darkBackgroundColor:
|
|
520
|
+
splash.darkBackgroundColor ??
|
|
521
|
+
(splash.image ? splashBackgroundColor : DEFAULTS.darkSplashBackgroundColor),
|
|
522
|
+
},
|
|
523
|
+
iconBackgroundColor:
|
|
524
|
+
config.android.adaptiveIcon?.backgroundColor ?? DEFAULTS.iconBackgroundColor,
|
|
525
|
+
}
|
|
526
|
+
}
|