Skip to content

Commit

Permalink
lint and add an interface for process.env
Browse files Browse the repository at this point in the history
  • Loading branch information
KKA11010 committed May 27, 2024
1 parent 6128379 commit 2cce400
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 24 deletions.
45 changes: 24 additions & 21 deletions src/consts/env.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

import type { IExpoConfig } from '@model'
import { IEnv } from '@model/env'
import { default as Consts, ExecutionEnvironment as ExecEnv } from 'expo-constants'
import { Platform } from 'react-native'

Expand All @@ -19,47 +20,49 @@ export { isExpo, isExpoDev, isExpoProd, isReactNativeDevMode }

type AppVariant = 'preview' | 'beta' | 'prod' | 'dev' | undefined

const typedEnv = process?.env as unknown as IEnv

function nodeEnvShort(): 'test' | AppVariant {
if (!process?.env?.NODE_ENV) {
process.env.NODE_ENV = 'development'
if (!typedEnv?.NODE_ENV) {
typedEnv.NODE_ENV = 'development'
return
}
if (process?.env?.NODE_ENV === 'production') { return 'prod' }
if (process?.env?.NODE_ENV === 'development') { return 'dev' }
if (process?.env?.NODE_ENV === 'test') { return 'test' }
if (process?.env?.NODE_ENV === 'preview') { return 'preview' }
if (process?.env?.NODE_ENV === 'beta') { return 'beta' }
if (typedEnv?.NODE_ENV === 'production') { return 'prod' }
if (typedEnv?.NODE_ENV === 'development') { return 'dev' }
if (typedEnv?.NODE_ENV === 'test') { return 'test' }
if (typedEnv?.NODE_ENV === 'preview') { return 'preview' }
if (typedEnv?.NODE_ENV === 'beta') { return 'beta' }
}

function appVariant(): AppVariant {
if (!process?.env?.APP_VARIANT) {
process.env.APP_VARIANT = 'dev'
if (!typedEnv?.APP_VARIANT) {
typedEnv.APP_VARIANT = 'dev'
return
}
if (process?.env?.APP_VARIANT === 'prod') { return 'prod' }
if (process?.env?.APP_VARIANT === 'dev') { return 'dev' }
if (process?.env?.APP_VARIANT === 'preview') { return 'preview' }
if (process?.env?.APP_VARIANT === 'beta') { return 'beta' }
if (typedEnv?.APP_VARIANT === 'prod') { return 'prod' }
if (typedEnv?.APP_VARIANT === 'dev') { return 'dev' }
if (typedEnv?.APP_VARIANT === 'preview') { return 'preview' }
if (typedEnv?.APP_VARIANT === 'beta') { return 'beta' }
}

const config: Readonly<IExpoConfig | undefined | null> = Consts?.expoConfig

export const env/* : Readonly<IExpoConfig['extra'] & { BUGSNAG_API_KEY?: string }> */ = {
DEBUG: process?.env?.DEBUG || config?.extra?.DEBUG,
DEBUG: typedEnv?.DEBUG || config?.extra?.DEBUG,

NODE_ENV: process?.env?.NODE_ENV || config?.extra?.NODE_ENV,
NODE_ENV: typedEnv?.NODE_ENV || config?.extra?.NODE_ENV,

NODE_ENV_SHORT: process?.env?.NODE_ENV_SHORT || config?.extra?.NODE_ENV_SHORT || nodeEnvShort() || appVariant(),
NODE_ENV_SHORT: typedEnv?.NODE_ENV_SHORT || config?.extra?.NODE_ENV_SHORT || nodeEnvShort() || appVariant(),

APP_VARIANT: process?.env?.APP_VARIANT || config?.extra?.APP_VARIANT || appVariant() || nodeEnvShort(),
APP_VARIANT: typedEnv?.APP_VARIANT || config?.extra?.APP_VARIANT || appVariant() || nodeEnvShort(),

SENTRY_DSN: process?.env?.SENTRY_DSN
|| process?.env?.SENTRY_DSN
SENTRY_DSN: typedEnv?.SENTRY_DSN
|| typedEnv?.SENTRY_DSN
|| config?.extra?.SENTRY_DSN,

isExpo,
isExpoDev,
isExpoBeta: process?.env?.APP_VARIANT === 'beta' || config?.extra?.APP_VARIANT === 'beta' || appVariant() === 'beta',
isExpoBeta: typedEnv?.APP_VARIANT === 'beta' || config?.extra?.APP_VARIANT === 'beta' || appVariant() === 'beta',
isExpoProd,
isReactNativeDevMode,
} as const
Expand All @@ -69,7 +72,7 @@ export const isTestMode = (typeof __TEST__ === 'boolean' && __TEST__)
|| (env?.NODE_ENV_SHORT === 'test' || env?.NODE_ENV === 'test')
|| (env?.APP_VARIANT === 'test' || env?.NODE_ENV === 'test')
|| (env?.NODE_ENV === 'test' && env?.NODE_ENV_SHORT === 'test')
|| process?.env?.NODE_ENV === 'test' || config?.extra?.NODE_ENV === 'test'
|| typedEnv?.NODE_ENV === 'test' || config?.extra?.NODE_ENV === 'test'

export const isIOS = Platform.OS === 'ios'
export const isNotIosStore = __DEV__ || env.isExpoBeta || !isIOS
Expand Down
7 changes: 7 additions & 0 deletions src/model/env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export interface IEnv {
NODE_ENV: string
APP_VARIANT: string
DEBUG: string
NODE_ENV_SHORT: string
SENTRY_DSN: string
}
2 changes: 1 addition & 1 deletion src/model/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { PayLnInvoiceResponse, Proof, Token } from '@cashu/cashu-ts'
import type { HighlightKey } from '@styles'
import type { ExpoConfig } from 'expo/config'
import type { SQLStmtCb, SQLStmtErrCb } from 'expo-sqlite'
import type { SQLStmtCb, SQLStmtErrCb } from 'expo-sqlite/legacy'

export interface IExpoConfig extends ExpoConfig {
extra?: {
Expand Down
5 changes: 3 additions & 2 deletions src/storage/db/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Proof, Token } from '@cashu/cashu-ts'
import { CashuMint, deriveKeysetId, getDecodedToken } from '@cashu/cashu-ts'
import { env } from '@consts'
import { l } from '@log'
import type { IContact, IInvoice, IMint, IMintWithBalance, IPreferences, IPreferencesResp, ITx } from '@model'
import { arrToChunks, isObj } from '@util'
Expand Down Expand Up @@ -30,7 +31,7 @@ const db = new Db(SQLite.openDatabase('cashu.db'))

// ################################ init DB ################################
export async function initDb() {
if (process.env.NODE_ENV === 'test') {
if (env.NODE_ENV === 'test') {
l('[initDb]', 'reset DB in test mode')
await db.reset(SQLite.openDatabase('cashu.db'))
}
Expand All @@ -47,7 +48,7 @@ export async function initDb() {
const cmds: ITx[] = queries.map(query => ({
sql: query,
args: [],
errorCb: (_, error) => {
errorCb: (_: any, error: unknown) => {
l('[initDb]', query, 'DB init error!', error)
return true
},
Expand Down

0 comments on commit 2cce400

Please sign in to comment.