All files / utils getEnvironment.ts

100% Statements 3/3
50% Branches 1/2
100% Functions 1/1
100% Lines 3/3

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                                                            2x 2x                       2x    
import { inAndroid } from './inAndroid'
import { inBrowser } from './inBrowser'
import { inDeno } from './inDeno'
import { inIOS } from './inIOS'
import { inMiniProgram } from './inMiniProgram'
import { inNodeJS } from './inNodeJS'
import { inTaro } from './inTaro'
import { inWechatWebView } from './inWechatWebView'
 
export interface GetEnvironmentResult {
  /** 浏览器 */
  readonly browser: boolean
  /** 微信 WebView */
  readonly wechatWebView: boolean
  /** 小程序 */
  readonly miniProgram: boolean
  /** 微信小程序 */
  readonly wechatMiniProgram: boolean
  /** iOS */
  readonly ios: boolean
  /** 安卓 */
  readonly android: boolean
  /** Node.js */
  readonly nodejs: boolean
  /** Deno */
  readonly deno: boolean
  /** Taro 3 */
  readonly taro: boolean
}
 
let env: GetEnvironmentResult | undefined
 
/**
 * 获取运行环境信息。
 *
 * @returns 返回运行环境信息
 */
export function getEnvironment(): GetEnvironmentResult {
  if (env == null) {
    env = {
      browser: inBrowser(),
      wechatWebView: inWechatWebView(),
      miniProgram: !!inMiniProgram(),
      wechatMiniProgram: !!inMiniProgram('微信'),
      ios: inIOS(),
      android: inAndroid(),
      nodejs: inNodeJS(),
      deno: inDeno(),
      taro: inTaro(),
    }
  }
  return env
}