forked from zhuanggenhua/BoardGame
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcapacitor.config.ts
More file actions
70 lines (61 loc) · 2.78 KB
/
Copy pathcapacitor.config.ts
File metadata and controls
70 lines (61 loc) · 2.78 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
import { existsSync } from 'node:fs';
import path from 'node:path';
import dotenv from 'dotenv';
import type { CapacitorConfig } from '@capacitor/cli';
const rootDir = process.cwd();
const debugAndroidAppIdSegments = new Set(['debug', 'dev', 'test', 'qa']);
for (const file of ['.env', '.env.android', '.env.android.local']) {
const fullPath = path.join(rootDir, file);
if (!existsSync(fullPath)) continue;
dotenv.config({ path: fullPath, override: true, quiet: true });
}
const parseBooleanEnv = (value: string | undefined) => /^(1|true|yes|on)$/i.test(value?.trim() || '');
const appId = process.env.CAPACITOR_APP_ID?.trim() || 'top.easyboardgame.app';
const appName = process.env.CAPACITOR_APP_NAME?.trim() || '易桌游';
const mode = (process.env.ANDROID_WEBVIEW_MODE?.trim().toLowerCase() || 'embedded');
const remoteUrl = process.env.ANDROID_REMOTE_WEB_URL?.trim() || '';
const isHttpRemoteUrl = /^http:\/\//i.test(remoteUrl);
const backendUrl = process.env.VITE_BACKEND_URL?.trim() || '';
const embeddedAndroidScheme = /^http:\/\//i.test(backendUrl) ? 'http' : 'https';
const isNonReleaseAndroidAppId = (value: string) => value
.split('.')
.some((segment) => debugAndroidAppIdSegments.has(segment.trim().toLowerCase()));
const otaEnabled = parseBooleanEnv(process.env.VITE_ANDROID_OTA_ENABLED)
&& (!isNonReleaseAndroidAppId(appId) || parseBooleanEnv(process.env.VITE_ANDROID_OTA_ALLOW_DEBUG_APP));
const otaAppReadyTimeout = Number.parseInt(process.env.VITE_ANDROID_OTA_APP_READY_TIMEOUT_MS?.trim() || '', 10);
if (mode !== 'embedded' && mode !== 'remote') {
throw new Error(`ANDROID_WEBVIEW_MODE 只支持 embedded 或 remote,当前值为: ${mode}`);
}
if (mode === 'remote' && !/^https?:\/\//i.test(remoteUrl)) {
throw new Error('ANDROID_REMOTE_WEB_URL 必须是绝对 HTTP/HTTPS 地址,且仅在 remote 模式下使用。');
}
const server: NonNullable<CapacitorConfig['server']> = {
androidScheme: mode === 'embedded' ? embeddedAndroidScheme : 'https',
};
if (mode === 'remote') {
server.url = remoteUrl;
server.cleartext = isHttpRemoteUrl;
}
const config: CapacitorConfig = {
appId,
appName,
webDir: 'dist',
server,
plugins: otaEnabled
? {
CapacitorUpdater: {
autoUpdate: false,
appReadyTimeout: Number.isFinite(otaAppReadyTimeout) && otaAppReadyTimeout >= 1000
? otaAppReadyTimeout
: 10000,
autoDeleteFailed: true,
autoDeletePrevious: true,
resetWhenUpdate: true,
keepUrlPathAfterReload: true,
allowManualBundleError: true,
defaultChannel: process.env.VITE_ANDROID_OTA_CHANNEL?.trim() || undefined,
},
}
: undefined,
};
export default config;