environments.ts 1.5 KB

12345678910111213141516171819202122232425262728293031
  1. export const environmentNames = ['development', 'test', 'production'] as const
  2. export type AppEnvironment = typeof environmentNames[number]
  3. export function resolveEnvironment(value: string | undefined) {
  4. if (!environmentNames.some((name) => name === value)) {
  5. throw new Error(`Invalid APP_ENV: ${value ?? '(missing)'}. Use development, test or production.`)
  6. }
  7. const name = value as AppEnvironment
  8. return { name, isProduction: name === 'production', dataSource: 'local' as const }
  9. }
  10. export function readPublicConfig(values: Record<string, string | undefined>) {
  11. const label = values.TARO_APP_ENV_LABEL?.trim()
  12. if (!label) throw new Error('TARO_APP_ENV_LABEL is required. Check the selected .env.<mode> file.')
  13. const debug = values.TARO_APP_ENABLE_DEBUG
  14. if (debug !== 'true' && debug !== 'false') {
  15. throw new Error('TARO_APP_ENABLE_DEBUG must be true or false.')
  16. }
  17. const apiBaseUrl = values.TARO_APP_API_BASE_URL?.trim() ?? ''
  18. if (apiBaseUrl) {
  19. let url: URL
  20. try { url = new URL(apiBaseUrl) } catch { throw new Error('TARO_APP_API_BASE_URL must be an absolute HTTP(S) URL or empty.') }
  21. if (!['http:', 'https:'].includes(url.protocol)) throw new Error('TARO_APP_API_BASE_URL must use HTTP(S).')
  22. }
  23. return { label, apiBaseUrl, enableDebug: debug === 'true' }
  24. }
  25. export function outputDirectory(platform: string) {
  26. if (platform !== 'weapp' && platform !== 'h5') throw new Error(`Unsupported platform: ${platform}`)
  27. return platform === 'weapp' ? 'dist' : 'dist-h5'
  28. }