@nativue/core 0.13.0 → 0.14.0
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 +31 -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 +515 -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,515 @@
|
|
|
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
|
+
const ANDROID_RESOURCE_TYPES = ['raw', 'font', 'drawable', 'xml', 'assets']
|
|
20
|
+
const APP_GROUP = /^group\.[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*$/
|
|
21
|
+
const EXTENSION_NAME = /^[A-Za-z][A-Za-z0-9_]*$/
|
|
22
|
+
const BUNDLE_IDENTIFIER_SUFFIX = /^(\.[a-zA-Z0-9-]+)+$/
|
|
23
|
+
const DEPLOYMENT_TARGET = /^\d+\.\d+(\.\d+)?$/
|
|
24
|
+
/** An extension target's type, and the NSExtensionPointIdentifier iOS knows it by. */
|
|
25
|
+
export const EXTENSION_POINTS = {
|
|
26
|
+
'notification-service': 'com.apple.usernotifications.service',
|
|
27
|
+
'notification-content': 'com.apple.usernotifications.content-extension',
|
|
28
|
+
widget: 'com.apple.widgetkit-extension',
|
|
29
|
+
share: 'com.apple.share-services',
|
|
30
|
+
}
|
|
31
|
+
const PNG_SIGNATURE = '89504e470d0a1a0a'
|
|
32
|
+
// IHDR color types with an alpha channel: grayscale and RGB with alpha.
|
|
33
|
+
const PNG_ALPHA_COLOR_TYPES = new Set([4, 6])
|
|
34
|
+
// The logo shown on the launch screen: 120 points wide on iOS, as @3x.
|
|
35
|
+
const SPLASH_IMAGE_SIZE = 360
|
|
36
|
+
const ICON_SIZE = 1024
|
|
37
|
+
|
|
38
|
+
/** The fields only generated projects use, which `prebuild: false` can't have. */
|
|
39
|
+
const GENERATED_PROJECT_FIELDS = [
|
|
40
|
+
'name',
|
|
41
|
+
'scheme',
|
|
42
|
+
'version',
|
|
43
|
+
'buildNumber',
|
|
44
|
+
'icon',
|
|
45
|
+
'splash',
|
|
46
|
+
'ios',
|
|
47
|
+
'android',
|
|
48
|
+
'resources',
|
|
49
|
+
]
|
|
50
|
+
|
|
51
|
+
const DEFAULTS = {
|
|
52
|
+
version: '1.0.0',
|
|
53
|
+
buildNumber: 1,
|
|
54
|
+
splashBackgroundColor: '#FFFFFF',
|
|
55
|
+
// Ink, from the template's own icon and splash.
|
|
56
|
+
darkSplashBackgroundColor: '#0B1220',
|
|
57
|
+
iconBackgroundColor: '#0B1220',
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function isObject(value) {
|
|
61
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value)
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function isPlistValue(value) {
|
|
65
|
+
if (['string', 'number', 'boolean'].includes(typeof value)) return true
|
|
66
|
+
if (Array.isArray(value)) return value.every(isPlistValue)
|
|
67
|
+
|
|
68
|
+
return isObject(value) && Object.values(value).every(isPlistValue)
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function plistProblem(field, value, example) {
|
|
72
|
+
return check(
|
|
73
|
+
value === undefined || (isObject(value) && isPlistValue(value)),
|
|
74
|
+
`${field}: strings, numbers, booleans, lists and objects, e.g. ${example}`,
|
|
75
|
+
)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** A PNG's size and whether it has an alpha channel, or null for any other file. */
|
|
79
|
+
function pngHeader(data) {
|
|
80
|
+
if (data.length < 26 || data.subarray(0, 8).toString('hex') !== PNG_SIGNATURE) return null
|
|
81
|
+
|
|
82
|
+
return {
|
|
83
|
+
width: data.readUInt32BE(16),
|
|
84
|
+
height: data.readUInt32BE(20),
|
|
85
|
+
hasAlpha: PNG_ALPHA_COLOR_TYPES.has(data[25]),
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function imageProblem(field, path, appDirectory, size, { opaque = false } = {}) {
|
|
90
|
+
if (path === undefined) return null
|
|
91
|
+
|
|
92
|
+
const expected = size ? `a ${size}×${size} PNG` : 'a square PNG'
|
|
93
|
+
const fullPath = resolve(appDirectory, String(path))
|
|
94
|
+
|
|
95
|
+
if (!existsSync(fullPath) || !statSync(fullPath).isFile()) {
|
|
96
|
+
return `${field}: ${path} isn't a file (${expected}, relative to the app)`
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const actual = pngHeader(readFileSync(fullPath))
|
|
100
|
+
const fits =
|
|
101
|
+
actual !== null &&
|
|
102
|
+
actual.width === actual.height &&
|
|
103
|
+
(size === undefined || actual.width === size)
|
|
104
|
+
|
|
105
|
+
// The App Store rejects an app icon with an alpha channel.
|
|
106
|
+
if (fits && opaque && actual.hasAlpha) {
|
|
107
|
+
return `${field}: ${path} must have no transparency: export it without an alpha channel`
|
|
108
|
+
}
|
|
109
|
+
if (fits) return null
|
|
110
|
+
|
|
111
|
+
const found = actual ? `${actual.width}×${actual.height}` : 'not a PNG'
|
|
112
|
+
|
|
113
|
+
return `${field}: ${path} must be ${expected}, it's ${found}`
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function check(isValid, problem) {
|
|
117
|
+
return isValid ? null : problem
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function colorProblem(field, value) {
|
|
121
|
+
return check(value === undefined || COLOR.test(value), `${field}: a color like '#0B1220'`)
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function schemeProblems(scheme) {
|
|
125
|
+
const schemes = [scheme].flat()
|
|
126
|
+
|
|
127
|
+
return check(
|
|
128
|
+
schemes.length > 0 && schemes.every((each) => URL_SCHEME.test(each ?? '')),
|
|
129
|
+
"scheme: e.g. scheme: 'myapp', for myapp:// links, or a list of them",
|
|
130
|
+
)
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** ios/ and android/ are generated: prebuild --force would delete a keystore kept there. */
|
|
134
|
+
function isInGeneratedFolder(path, appDirectory) {
|
|
135
|
+
const [folder] = relative(appDirectory, resolve(appDirectory, path)).split(sep)
|
|
136
|
+
|
|
137
|
+
// macOS and Windows don't tell Android/ from android/.
|
|
138
|
+
return ['ios', 'android'].includes(folder.toLowerCase())
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function signingProblems(signing, appDirectory) {
|
|
142
|
+
if (signing === undefined) return []
|
|
143
|
+
|
|
144
|
+
return [
|
|
145
|
+
check(
|
|
146
|
+
typeof signing.storeFile === 'string' &&
|
|
147
|
+
!isInGeneratedFolder(signing.storeFile, appDirectory),
|
|
148
|
+
"android.signing.storeFile: the keystore, outside ios/ and android/, e.g. './credentials/upload.keystore'",
|
|
149
|
+
),
|
|
150
|
+
check(typeof signing.keyAlias === 'string', "android.signing.keyAlias: e.g. 'upload'"),
|
|
151
|
+
...['storePasswordEnv', 'keyPasswordEnv'].map((field) =>
|
|
152
|
+
check(
|
|
153
|
+
SECRET_NAME.test(signing[field] ?? ''),
|
|
154
|
+
`android.signing.${field}: the name of the environment variable or Gradle property holding it, e.g. 'ANDROID_STORE_PASSWORD'`,
|
|
155
|
+
),
|
|
156
|
+
),
|
|
157
|
+
]
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
function isStringList(value) {
|
|
161
|
+
return Array.isArray(value) && value.every((item) => typeof item === 'string' && item !== '')
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function isStringRecord(value, keyPattern = /./) {
|
|
165
|
+
return (
|
|
166
|
+
isObject(value) &&
|
|
167
|
+
Object.entries(value).every(
|
|
168
|
+
([key, item]) => keyPattern.test(key) && typeof item === 'string' && !item.includes('\n'),
|
|
169
|
+
)
|
|
170
|
+
)
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
export function emptyGradle() {
|
|
174
|
+
return {
|
|
175
|
+
plugins: [],
|
|
176
|
+
dependencies: [],
|
|
177
|
+
manifestPlaceholders: {},
|
|
178
|
+
repositories: [],
|
|
179
|
+
properties: {},
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
function isText(value) {
|
|
184
|
+
return typeof value === 'string' && value !== '' && !value.includes('\n')
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/** android.gradle, or what the plugins' gradle mods made (field: where it comes from). */
|
|
188
|
+
export function gradleProblems(gradle, field = 'android.gradle') {
|
|
189
|
+
if (gradle === undefined) return []
|
|
190
|
+
|
|
191
|
+
const { plugins, dependencies, repositories, manifestPlaceholders, properties } = gradle
|
|
192
|
+
|
|
193
|
+
return [
|
|
194
|
+
check(
|
|
195
|
+
plugins === undefined ||
|
|
196
|
+
(Array.isArray(plugins) &&
|
|
197
|
+
plugins.every(
|
|
198
|
+
(plugin) =>
|
|
199
|
+
isText(plugin?.id) && (plugin.version === undefined || isText(plugin.version)),
|
|
200
|
+
)),
|
|
201
|
+
`${field}.plugins: e.g. [{ id: 'com.google.gms.google-services', version: '4.4.2' }]`,
|
|
202
|
+
),
|
|
203
|
+
check(
|
|
204
|
+
dependencies === undefined || (Array.isArray(dependencies) && dependencies.every(isText)),
|
|
205
|
+
`${field}.dependencies: e.g. ['com.squareup.okhttp3:okhttp:4.12.0']`,
|
|
206
|
+
),
|
|
207
|
+
check(
|
|
208
|
+
repositories === undefined ||
|
|
209
|
+
(Array.isArray(repositories) &&
|
|
210
|
+
repositories.every((url) => isText(url) && url.startsWith('https://'))),
|
|
211
|
+
`${field}.repositories: Maven URLs, e.g. ['https://jitpack.io']`,
|
|
212
|
+
),
|
|
213
|
+
check(
|
|
214
|
+
manifestPlaceholders === undefined || isStringRecord(manifestPlaceholders),
|
|
215
|
+
`${field}.manifestPlaceholders: e.g. { appAuthRedirectScheme: 'myapp' }`,
|
|
216
|
+
),
|
|
217
|
+
check(
|
|
218
|
+
properties === undefined || isStringRecord(properties, GRADLE_PROPERTY),
|
|
219
|
+
`${field}.properties: e.g. { 'android.enableJetifier': 'true' }`,
|
|
220
|
+
),
|
|
221
|
+
]
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
function isDirectory(path) {
|
|
225
|
+
return existsSync(path) && statSync(path).isDirectory()
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
function extensionTargetProblems(target, field, { appDirectory, appGroups }) {
|
|
229
|
+
const { sources } = target
|
|
230
|
+
|
|
231
|
+
return [
|
|
232
|
+
check(
|
|
233
|
+
typeof sources === 'string' && isDirectory(resolve(appDirectory, sources)),
|
|
234
|
+
`${field}.sources: the folder of the extension's Swift files`,
|
|
235
|
+
),
|
|
236
|
+
check(
|
|
237
|
+
Object.keys(EXTENSION_POINTS).includes(target.type),
|
|
238
|
+
`${field}.type: one of ${Object.keys(EXTENSION_POINTS).join(', ')}`,
|
|
239
|
+
),
|
|
240
|
+
check(
|
|
241
|
+
BUNDLE_IDENTIFIER_SUFFIX.test(target.bundleIdSuffix ?? ''),
|
|
242
|
+
`${field}.bundleIdSuffix: added to the app's bundle identifier, e.g. '.${target.name}'`,
|
|
243
|
+
),
|
|
244
|
+
check(
|
|
245
|
+
target.deploymentTarget === undefined || DEPLOYMENT_TARGET.test(target.deploymentTarget),
|
|
246
|
+
`${field}.deploymentTarget: e.g. '16.0'`,
|
|
247
|
+
),
|
|
248
|
+
plistProblem(`${field}.infoPlist`, target.infoPlist, '{ NSExtension: { ... } }'),
|
|
249
|
+
plistProblem(
|
|
250
|
+
`${field}.entitlements`,
|
|
251
|
+
target.entitlements,
|
|
252
|
+
"{ 'aps-environment': 'production' }",
|
|
253
|
+
),
|
|
254
|
+
check(
|
|
255
|
+
target.appGroups === undefined || typeof target.appGroups === 'boolean',
|
|
256
|
+
`${field}.appGroups: true to share ios.appGroups with the app`,
|
|
257
|
+
),
|
|
258
|
+
check(
|
|
259
|
+
target.appGroups !== true || appGroups.length > 0,
|
|
260
|
+
`${field}.appGroups: shares ios.appGroups, which nativue.config.ts doesn't set, e.g. ios: { appGroups: ['group.com.company.myapp'] }`,
|
|
261
|
+
),
|
|
262
|
+
check(
|
|
263
|
+
target.buildSettings === undefined || isStringRecord(target.buildSettings),
|
|
264
|
+
`${field}.buildSettings: e.g. { SWIFT_VERSION: '6.0' }`,
|
|
265
|
+
),
|
|
266
|
+
]
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/** Throws with every problem of the extension targets the plugins and native/extensions add. */
|
|
270
|
+
/** appGroups: the app's ios.appGroups, which targets with appGroups: true share. */
|
|
271
|
+
export function validateExtensionTargets(targets, { appDirectory, appGroups = [] }) {
|
|
272
|
+
const names = targets.map((target) => target?.name)
|
|
273
|
+
// macOS doesn't tell Extensions/Widget from Extensions/widget.
|
|
274
|
+
const folderNames = names.map((name) => String(name).toLowerCase())
|
|
275
|
+
const problems = targets.flatMap((target, index) => {
|
|
276
|
+
const name = names[index]
|
|
277
|
+
const field = `ios target ${typeof name === 'string' ? name : index}`
|
|
278
|
+
|
|
279
|
+
if (!isObject(target)) return [`${field}: e.g. { name: 'NotificationService', ... }`]
|
|
280
|
+
|
|
281
|
+
return [
|
|
282
|
+
check(
|
|
283
|
+
EXTENSION_NAME.test(name ?? '') &&
|
|
284
|
+
!['app', 'nativueplugins'].includes(folderNames[index]) &&
|
|
285
|
+
folderNames.indexOf(folderNames[index]) === index,
|
|
286
|
+
`${field}.name: letters, digits and underscores, other than App, NativuePlugins and the other targets' (whatever their case)`,
|
|
287
|
+
),
|
|
288
|
+
...extensionTargetProblems(target, field, { appDirectory, appGroups }),
|
|
289
|
+
]
|
|
290
|
+
})
|
|
291
|
+
const found = problems.filter(Boolean)
|
|
292
|
+
|
|
293
|
+
if (found.length === 0) return
|
|
294
|
+
|
|
295
|
+
throw new Error(
|
|
296
|
+
`The app's extension targets have invalid fields:\n${found.map((problem) => ` ${problem}`).join('\n')}`,
|
|
297
|
+
)
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
/** A resource with its defaults: where it lands on each platform, or false for none. */
|
|
301
|
+
function resourceWithDefaults(resource) {
|
|
302
|
+
const fileName = basename(String(resource.source))
|
|
303
|
+
const android = resource.android !== false && {
|
|
304
|
+
type: resource.font ? 'font' : 'raw',
|
|
305
|
+
...resource.android,
|
|
306
|
+
}
|
|
307
|
+
const defaultAndroidName =
|
|
308
|
+
android.type === 'assets' ? fileName : basename(fileName, extname(fileName))
|
|
309
|
+
|
|
310
|
+
return {
|
|
311
|
+
source: resource.source,
|
|
312
|
+
font: resource.font === true,
|
|
313
|
+
ios: resource.ios !== false && { destination: fileName, ...resource.ios },
|
|
314
|
+
android: android && { ...android, name: android.name ?? defaultAndroidName },
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
/** The plugins' resources, then the app's: a later one at the same place replaces an earlier one. */
|
|
319
|
+
export function appResources(config, plugins = []) {
|
|
320
|
+
return [...plugins.flatMap((plugin) => plugin.resources ?? []), ...(config.resources ?? [])].map(
|
|
321
|
+
resourceWithDefaults,
|
|
322
|
+
)
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/** The fonts' file names, once: a later resource at the same place replaces an earlier one. */
|
|
326
|
+
export function iosFonts(resources) {
|
|
327
|
+
const byDestination = new Map(
|
|
328
|
+
resources.filter(({ ios }) => ios).map((resource) => [resource.ios.destination, resource]),
|
|
329
|
+
)
|
|
330
|
+
|
|
331
|
+
return [...byDestination.values()].filter(({ font }) => font).map(({ ios }) => ios.destination)
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
/** Where a resource lands in android/app/src/main. */
|
|
335
|
+
export function resourceFile({ source, android }) {
|
|
336
|
+
return android.type === 'assets'
|
|
337
|
+
? `assets/${android.name}`
|
|
338
|
+
: `res/${android.type}/${android.name}${extname(source)}`
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
function resourceProblems(resource, field, appDirectory) {
|
|
342
|
+
if (!isObject(resource) || typeof resource.source !== 'string') {
|
|
343
|
+
return [`${field}: e.g. { source: './assets/chime.wav' }`]
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
const { source, ios, android } = resourceWithDefaults(resource)
|
|
347
|
+
const fullPath = resolve(appDirectory, source)
|
|
348
|
+
|
|
349
|
+
return [
|
|
350
|
+
check(
|
|
351
|
+
existsSync(fullPath) && statSync(fullPath).isFile(),
|
|
352
|
+
`${field}.source: ${source} isn't a file`,
|
|
353
|
+
),
|
|
354
|
+
check(
|
|
355
|
+
!ios || /^(?!\.\.?$)[^/\\]+$/.test(ios.destination),
|
|
356
|
+
`${field}.ios.destination: a file name, e.g. 'chime.wav'`,
|
|
357
|
+
),
|
|
358
|
+
check(
|
|
359
|
+
!android || ANDROID_RESOURCE_TYPES.includes(android.type),
|
|
360
|
+
`${field}.android.type: one of ${ANDROID_RESOURCE_TYPES.join(', ')}`,
|
|
361
|
+
),
|
|
362
|
+
check(
|
|
363
|
+
!android ||
|
|
364
|
+
(android.type === 'assets'
|
|
365
|
+
? android.name.split('/').every((part) => part !== '' && part !== '.' && part !== '..')
|
|
366
|
+
: ANDROID_RESOURCE_NAME.test(android.name)),
|
|
367
|
+
`${field}.android.name: lowercase letters, digits and underscores, e.g. '${basename(
|
|
368
|
+
source,
|
|
369
|
+
extname(source),
|
|
370
|
+
)
|
|
371
|
+
.toLowerCase()
|
|
372
|
+
.replace(/[^a-z0-9_]/g, '_')}'`,
|
|
373
|
+
),
|
|
374
|
+
]
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
/** The app's resources and each plugin's, named after where they come from. */
|
|
378
|
+
function allResourceProblems(config, plugins, appDirectory) {
|
|
379
|
+
const sources = [
|
|
380
|
+
...plugins.map((plugin) => [`${plugin.name}'s resources`, plugin.resources]),
|
|
381
|
+
['resources', config.resources],
|
|
382
|
+
]
|
|
383
|
+
|
|
384
|
+
return sources.flatMap(([field, resources]) => {
|
|
385
|
+
if (resources === undefined) return []
|
|
386
|
+
if (!Array.isArray(resources))
|
|
387
|
+
return [`${field}: a list, e.g. [{ source: './assets/chime.wav' }]`]
|
|
388
|
+
|
|
389
|
+
return resources.flatMap((resource, index) =>
|
|
390
|
+
resourceProblems(resource, `${field}[${index}]`, appDirectory),
|
|
391
|
+
)
|
|
392
|
+
})
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
function generatedProjectProblems(config, appDirectory, plugins) {
|
|
396
|
+
const { splash, buildNumber } = config
|
|
397
|
+
const ios = config.ios ?? {}
|
|
398
|
+
const android = config.android ?? {}
|
|
399
|
+
|
|
400
|
+
return [
|
|
401
|
+
check(
|
|
402
|
+
typeof config.name === 'string' && config.name.trim() !== '',
|
|
403
|
+
"name: the app's name, e.g. name: 'My App'",
|
|
404
|
+
),
|
|
405
|
+
schemeProblems(config.scheme),
|
|
406
|
+
check(
|
|
407
|
+
config.version === undefined || VERSION.test(config.version),
|
|
408
|
+
"version: three numbers, e.g. version: '1.2.0'",
|
|
409
|
+
),
|
|
410
|
+
check(
|
|
411
|
+
buildNumber === undefined ||
|
|
412
|
+
(Number.isInteger(buildNumber) && buildNumber > 0 && buildNumber <= MAXIMUM_BUILD_NUMBER),
|
|
413
|
+
`buildNumber: a whole number from 1 to ${MAXIMUM_BUILD_NUMBER}, raised for every release`,
|
|
414
|
+
),
|
|
415
|
+
imageProblem('icon', config.icon, appDirectory, ICON_SIZE, { opaque: true }),
|
|
416
|
+
imageProblem('splash.image', splash?.image, appDirectory, SPLASH_IMAGE_SIZE),
|
|
417
|
+
imageProblem('splash.darkImage', splash?.darkImage, appDirectory, SPLASH_IMAGE_SIZE),
|
|
418
|
+
colorProblem('splash.backgroundColor', splash?.backgroundColor),
|
|
419
|
+
colorProblem('splash.darkBackgroundColor', splash?.darkBackgroundColor),
|
|
420
|
+
check(
|
|
421
|
+
splash?.darkImage === undefined || splash.image !== undefined,
|
|
422
|
+
'splash.darkImage: needs splash.image, shown in light mode',
|
|
423
|
+
),
|
|
424
|
+
check(
|
|
425
|
+
IOS_BUNDLE_IDENTIFIER.test(ios.bundleIdentifier ?? ''),
|
|
426
|
+
"ios.bundleIdentifier: e.g. ios: { bundleIdentifier: 'com.company.myapp' }",
|
|
427
|
+
),
|
|
428
|
+
check(
|
|
429
|
+
ios.appleTeamId === undefined || APPLE_TEAM_ID.test(ios.appleTeamId),
|
|
430
|
+
'ios.appleTeamId: the 10 characters of your team id, from Signing & Capabilities in Xcode',
|
|
431
|
+
),
|
|
432
|
+
plistProblem('ios.infoPlist', ios.infoPlist, "{ NSCameraUsageDescription: 'Scans receipts' }"),
|
|
433
|
+
plistProblem('ios.entitlements', ios.entitlements, "{ 'aps-environment': 'development' }"),
|
|
434
|
+
check(
|
|
435
|
+
ios.appGroups === undefined ||
|
|
436
|
+
(Array.isArray(ios.appGroups) && ios.appGroups.every((group) => APP_GROUP.test(group))),
|
|
437
|
+
"ios.appGroups: e.g. ['group.com.company.myapp']",
|
|
438
|
+
),
|
|
439
|
+
check(
|
|
440
|
+
ANDROID_PACKAGE.test(android.package ?? ''),
|
|
441
|
+
"android.package: e.g. android: { package: 'com.company.myapp' }, without hyphens",
|
|
442
|
+
),
|
|
443
|
+
check(
|
|
444
|
+
android.permissions === undefined ||
|
|
445
|
+
(Array.isArray(android.permissions) &&
|
|
446
|
+
android.permissions.every((permission) => ANDROID_PERMISSION.test(permission))),
|
|
447
|
+
"android.permissions: full names, e.g. ['android.permission.CAMERA']",
|
|
448
|
+
),
|
|
449
|
+
check(
|
|
450
|
+
android.blockedPermissions === undefined ||
|
|
451
|
+
(Array.isArray(android.blockedPermissions) &&
|
|
452
|
+
android.blockedPermissions.every((permission) => ANDROID_PERMISSION.test(permission))),
|
|
453
|
+
"android.blockedPermissions: full names, e.g. ['android.permission.READ_MEDIA_IMAGES']",
|
|
454
|
+
),
|
|
455
|
+
check(
|
|
456
|
+
android.adaptiveIcon === undefined || android.adaptiveIcon.foregroundImage !== undefined,
|
|
457
|
+
"android.adaptiveIcon.foregroundImage: the icon's square PNG, e.g. './assets/adaptive-icon.png'",
|
|
458
|
+
),
|
|
459
|
+
imageProblem(
|
|
460
|
+
'android.adaptiveIcon.foregroundImage',
|
|
461
|
+
android.adaptiveIcon?.foregroundImage,
|
|
462
|
+
appDirectory,
|
|
463
|
+
),
|
|
464
|
+
colorProblem('android.adaptiveIcon.backgroundColor', android.adaptiveIcon?.backgroundColor),
|
|
465
|
+
...signingProblems(android.signing, appDirectory),
|
|
466
|
+
...gradleProblems(android.gradle),
|
|
467
|
+
...allResourceProblems(config, plugins, appDirectory),
|
|
468
|
+
]
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
function manualProjectProblems(config) {
|
|
472
|
+
return GENERATED_PROJECT_FIELDS.filter((field) => field in config).map(
|
|
473
|
+
(field) =>
|
|
474
|
+
`${field}: nativue uses it only to generate ios/ and android/, which prebuild: false keeps by hand. Set it in those folders instead`,
|
|
475
|
+
)
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
/** Throws with every problem of the config at once, before anything is generated. */
|
|
479
|
+
export function validateConfig(config, appDirectory, plugins = []) {
|
|
480
|
+
const problems = (
|
|
481
|
+
config.prebuild === false
|
|
482
|
+
? manualProjectProblems(config)
|
|
483
|
+
: generatedProjectProblems(config, appDirectory, plugins)
|
|
484
|
+
).filter(Boolean)
|
|
485
|
+
|
|
486
|
+
if (problems.length === 0) return
|
|
487
|
+
|
|
488
|
+
throw new Error(
|
|
489
|
+
`nativue.config.ts has invalid fields:\n${problems.map((problem) => ` ${problem}`).join('\n')}`,
|
|
490
|
+
)
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
/** The config with its defaults, as the project generators read it. */
|
|
494
|
+
export function appConfig(config) {
|
|
495
|
+
const splash = config.splash ?? {}
|
|
496
|
+
const splashBackgroundColor = splash.backgroundColor ?? DEFAULTS.splashBackgroundColor
|
|
497
|
+
|
|
498
|
+
return {
|
|
499
|
+
...config,
|
|
500
|
+
schemes: [config.scheme].flat(),
|
|
501
|
+
version: config.version ?? DEFAULTS.version,
|
|
502
|
+
buildNumber: config.buildNumber ?? DEFAULTS.buildNumber,
|
|
503
|
+
splash: {
|
|
504
|
+
image: splash.image,
|
|
505
|
+
darkImage: splash.darkImage ?? splash.image,
|
|
506
|
+
backgroundColor: splashBackgroundColor,
|
|
507
|
+
// The template's dark logo is made for Ink: keep it unless the app has its own logo.
|
|
508
|
+
darkBackgroundColor:
|
|
509
|
+
splash.darkBackgroundColor ??
|
|
510
|
+
(splash.image ? splashBackgroundColor : DEFAULTS.darkSplashBackgroundColor),
|
|
511
|
+
},
|
|
512
|
+
iconBackgroundColor:
|
|
513
|
+
config.android.adaptiveIcon?.backgroundColor ?? DEFAULTS.iconBackgroundColor,
|
|
514
|
+
}
|
|
515
|
+
}
|
package/cli/config.d.mts
CHANGED
|
@@ -1,19 +1,178 @@
|
|
|
1
1
|
import type { Plugin as VitePlugin } from 'vite'
|
|
2
2
|
|
|
3
|
+
/** A value of Info.plist or the entitlements. */
|
|
4
|
+
export type PlistValue = string | number | boolean | PlistValue[] | { [key: string]: PlistValue }
|
|
5
|
+
|
|
6
|
+
export type PlistDictionary = { [key: string]: PlistValue }
|
|
7
|
+
|
|
8
|
+
/** An XML element: attribute names keep their prefix, 'android:name' or 'tools:node'. */
|
|
9
|
+
export interface XmlElement {
|
|
10
|
+
tag: string
|
|
11
|
+
attributes: Record<string, string>
|
|
12
|
+
children: XmlElement[]
|
|
13
|
+
text?: string
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** A file copied into the app: a sound, a font, an XML file a manifest entry points at. */
|
|
17
|
+
export interface Resource {
|
|
18
|
+
/** Relative to the app, or absolute: a package passes `fileURLToPath(new URL('./chime.wav', import.meta.url))`. */
|
|
19
|
+
source: string
|
|
20
|
+
/** iOS: at the bundle's root, named destination (defaults to the file's name). false: not on iOS. */
|
|
21
|
+
ios?: false | { destination?: string }
|
|
22
|
+
/**
|
|
23
|
+
* Android: res/<type>/<name>.<extension>, or assets/<name>. type defaults to 'raw' ('font' for a
|
|
24
|
+
* font); name to the file's, without its extension: lowercase letters, digits and underscores.
|
|
25
|
+
*/
|
|
26
|
+
android?: false | { type?: 'raw' | 'font' | 'drawable' | 'xml' | 'assets'; name?: string }
|
|
27
|
+
/** A font: listed in UIAppFonts on iOS, in res/font on Android. */
|
|
28
|
+
font?: boolean
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** The app's Gradle setup: the plugins' gradle mods, then android.gradle. */
|
|
32
|
+
export interface AndroidGradle {
|
|
33
|
+
/** Applied to the app; a version is set in the root build file. */
|
|
34
|
+
plugins: { id: string; version?: string }[]
|
|
35
|
+
/** The app's implementation dependencies, e.g. 'com.squareup.okhttp3:okhttp:4.12.0'. */
|
|
36
|
+
dependencies: string[]
|
|
37
|
+
manifestPlaceholders: Record<string, string>
|
|
38
|
+
/** Maven repositories, e.g. 'https://jitpack.io'. */
|
|
39
|
+
repositories: string[]
|
|
40
|
+
/** gradle.properties */
|
|
41
|
+
properties: Record<string, string>
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* An iOS app extension the app embeds, built from a folder of Swift files. It doesn't link nativue:
|
|
46
|
+
* it's plain Swift.
|
|
47
|
+
*/
|
|
48
|
+
export interface IosTarget {
|
|
49
|
+
/** The target's name, and its Swift module: letters, digits and underscores. */
|
|
50
|
+
name: string
|
|
51
|
+
type: 'notification-service' | 'notification-content' | 'widget' | 'share'
|
|
52
|
+
/** The folder of its Swift files: a package passes an absolute path. */
|
|
53
|
+
sources: string
|
|
54
|
+
/** Added to the app's bundle identifier, e.g. '.NotificationService'. */
|
|
55
|
+
bundleIdSuffix: string
|
|
56
|
+
/** Defaults to the app's, 15.1. */
|
|
57
|
+
deploymentTarget?: string
|
|
58
|
+
/** Merged over { NSExtension: { NSExtensionPointIdentifier } } for its type. */
|
|
59
|
+
infoPlist?: PlistDictionary
|
|
60
|
+
entitlements?: PlistDictionary
|
|
61
|
+
/** true: shares the app's ios.appGroups. */
|
|
62
|
+
appGroups?: boolean
|
|
63
|
+
buildSettings?: Record<string, string>
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export interface PrebuildContext {
|
|
67
|
+
/** nativue.config.ts, with its defaults. */
|
|
68
|
+
config: NativueConfig
|
|
69
|
+
/** The app's folder. */
|
|
70
|
+
projectRoot: string
|
|
71
|
+
platform: 'ios' | 'android'
|
|
72
|
+
}
|
|
73
|
+
|
|
3
74
|
/**
|
|
4
|
-
* A plugin of nativue.config.ts, e.g. @nativue/tailwind: build-time work through Vite plugins,
|
|
5
|
-
* module the app imports before it runs.
|
|
75
|
+
* A plugin of nativue.config.ts, e.g. @nativue/tailwind: build-time work through Vite plugins, a
|
|
76
|
+
* module the app imports before it runs, and mods of the native files prebuild generates. A package
|
|
77
|
+
* declares its own in package.json, `"nativue": { "plugin": "./plugin.mjs" }`, whose default export
|
|
78
|
+
* is `(options?) => NativuePlugin`: it's applied without options unless nativue.config.ts lists it.
|
|
6
79
|
*/
|
|
7
80
|
export interface NativuePlugin {
|
|
81
|
+
/** A package's own plugin: the package's name. */
|
|
8
82
|
name: string
|
|
9
83
|
/** Added to the bundler's (Vite's) plugins. */
|
|
10
84
|
vite?: VitePlugin | VitePlugin[]
|
|
11
85
|
/** Imported before the app runs, resolved from the app: a package path or a virtual module. */
|
|
12
86
|
runtime?: string
|
|
87
|
+
/** Copied into the app, like the config's resources, before them. */
|
|
88
|
+
resources?: Resource[]
|
|
89
|
+
/** Change the documents in place; the app's own config fields are applied after them. */
|
|
90
|
+
ios?: {
|
|
91
|
+
infoPlist?: (infoPlist: PlistDictionary, context: PrebuildContext) => void
|
|
92
|
+
entitlements?: (entitlements: PlistDictionary, context: PrebuildContext) => void
|
|
93
|
+
/** Extension targets the app embeds: push yours. */
|
|
94
|
+
targets?: (targets: IosTarget[], context: PrebuildContext) => void
|
|
95
|
+
}
|
|
96
|
+
android?: {
|
|
97
|
+
/** The app's AndroidManifest.xml: a package's own entries belong in its library manifest. */
|
|
98
|
+
manifest?: (manifest: XmlElement, context: PrebuildContext) => void
|
|
99
|
+
/** res/values/strings.xml, by name. */
|
|
100
|
+
strings?: (strings: Record<string, string>, context: PrebuildContext) => void
|
|
101
|
+
/** The app's Gradle files. A package's own dependencies belong in its build.gradle.kts. */
|
|
102
|
+
gradle?: (gradle: AndroidGradle, context: PrebuildContext) => void
|
|
103
|
+
}
|
|
13
104
|
}
|
|
14
105
|
|
|
15
|
-
|
|
106
|
+
/** nativue.config.ts: the app, which `nativue prebuild` generates ios/ and android/ from. */
|
|
107
|
+
export interface GeneratedProjectsConfig {
|
|
108
|
+
prebuild?: true
|
|
109
|
+
/** Shown under the app's icon. */
|
|
110
|
+
name: string
|
|
111
|
+
/** Deep links open the app: `myapp://...`. Several, e.g. to keep an old one working. */
|
|
112
|
+
scheme: string | string[]
|
|
113
|
+
/** Shown to people, three numbers: '1.2.0'. Defaults to '1.0.0'. */
|
|
114
|
+
version?: string
|
|
115
|
+
/** Raised for every release the stores get: CFBundleVersion and versionCode. Defaults to 1. */
|
|
116
|
+
buildNumber?: number
|
|
117
|
+
/** A 1024×1024 PNG, relative to the app. */
|
|
118
|
+
icon?: string
|
|
119
|
+
/** The launch screen: a logo on a color. */
|
|
120
|
+
splash?: {
|
|
121
|
+
/** A 360×360 PNG, shown 120 points wide. */
|
|
122
|
+
image?: string
|
|
123
|
+
/** In dark mode; defaults to image. */
|
|
124
|
+
darkImage?: string
|
|
125
|
+
/** e.g. '#F1F5F9'; defaults to white. */
|
|
126
|
+
backgroundColor?: string
|
|
127
|
+
/** Defaults to backgroundColor. */
|
|
128
|
+
darkBackgroundColor?: string
|
|
129
|
+
}
|
|
130
|
+
/** Files copied into the app, e.g. [{ source: './assets/chime.wav' }]. */
|
|
131
|
+
resources?: Resource[]
|
|
132
|
+
ios: {
|
|
133
|
+
/** e.g. com.company.myapp */
|
|
134
|
+
bundleIdentifier: string
|
|
135
|
+
/** Signs the app for phones: the team id under Signing & Capabilities in Xcode. */
|
|
136
|
+
appleTeamId?: string
|
|
137
|
+
/** Keys added to Info.plist, e.g. { NSCameraUsageDescription: 'Scans receipts' }. */
|
|
138
|
+
infoPlist?: Record<string, PlistValue>
|
|
139
|
+
/** e.g. { 'aps-environment': 'development' } */
|
|
140
|
+
entitlements?: Record<string, PlistValue>
|
|
141
|
+
/** The app's App Groups, shared with extension targets that ask for them, e.g. ['group.com.company.myapp']. */
|
|
142
|
+
appGroups?: string[]
|
|
143
|
+
}
|
|
144
|
+
android: {
|
|
145
|
+
/** The applicationId and the Kotlin package, e.g. com.company.myapp: no hyphens. */
|
|
146
|
+
package: string
|
|
147
|
+
/** e.g. ['android.permission.CAMERA'] */
|
|
148
|
+
permissions?: string[]
|
|
149
|
+
/** Removed even when a package's manifest asks for them, e.g. ['android.permission.READ_MEDIA_IMAGES'] */
|
|
150
|
+
blockedPermissions?: string[]
|
|
151
|
+
/** e.g. { dependencies: ['com.squareup.okhttp3:okhttp:4.12.0'] } */
|
|
152
|
+
gradle?: Partial<AndroidGradle>
|
|
153
|
+
/** Android 8+'s icon: a square PNG whose logo fits in the middle 61%, on a color. */
|
|
154
|
+
adaptiveIcon?: { foregroundImage: string; backgroundColor?: string }
|
|
155
|
+
/**
|
|
156
|
+
* Signs release builds. The passwords are read when Gradle builds, from a Gradle property
|
|
157
|
+
* (~/.gradle/gradle.properties) or an environment variable of these names.
|
|
158
|
+
*/
|
|
159
|
+
signing?: {
|
|
160
|
+
/** Outside android/, e.g. './credentials/upload.keystore' */
|
|
161
|
+
storeFile: string
|
|
162
|
+
keyAlias: string
|
|
163
|
+
storePasswordEnv: string
|
|
164
|
+
keyPasswordEnv: string
|
|
165
|
+
}
|
|
166
|
+
}
|
|
16
167
|
plugins?: NativuePlugin[]
|
|
17
168
|
}
|
|
18
169
|
|
|
170
|
+
/** An app that keeps ios/ and android/ by hand: nativue only links its packages' native code. */
|
|
171
|
+
export interface ManualProjectsConfig {
|
|
172
|
+
prebuild: false
|
|
173
|
+
plugins?: NativuePlugin[]
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
export type NativueConfig = GeneratedProjectsConfig | ManualProjectsConfig
|
|
177
|
+
|
|
19
178
|
export function defineConfig(config: NativueConfig): NativueConfig
|