export const environmentNames = ['development', 'test', 'production'] as const export type AppEnvironment = typeof environmentNames[number] export function resolveEnvironment(value: string | undefined) { if (!environmentNames.some((name) => name === value)) { throw new Error(`Invalid APP_ENV: ${value ?? '(missing)'}. Use development, test or production.`) } const name = value as AppEnvironment return { name, isProduction: name === 'production', dataSource: 'local' as const } } export function readPublicConfig(values: Record) { const label = values.TARO_APP_ENV_LABEL?.trim() if (!label) throw new Error('TARO_APP_ENV_LABEL is required. Check the selected .env. file.') const debug = values.TARO_APP_ENABLE_DEBUG if (debug !== 'true' && debug !== 'false') { throw new Error('TARO_APP_ENABLE_DEBUG must be true or false.') } const apiBaseUrl = values.TARO_APP_API_BASE_URL?.trim() ?? '' if (apiBaseUrl) { let url: URL try { url = new URL(apiBaseUrl) } catch { throw new Error('TARO_APP_API_BASE_URL must be an absolute HTTP(S) URL or empty.') } if (!['http:', 'https:'].includes(url.protocol)) throw new Error('TARO_APP_API_BASE_URL must use HTTP(S).') } return { label, apiBaseUrl, enableDebug: debug === 'true' } } export function outputDirectory(platform: string) { if (platform !== 'weapp' && platform !== 'h5') throw new Error(`Unsupported platform: ${platform}`) return platform === 'weapp' ? 'dist' : 'dist-h5' }