From 2cce40005c0ca93fbb80ed99e26c87437a74705b Mon Sep 17 00:00:00 2001 From: First-Terraner Date: Mon, 27 May 2024 17:31:59 +0200 Subject: [PATCH] lint and add an interface for process.env --- src/consts/env.ts | 45 ++++++++++++++++++++++------------------- src/model/env.ts | 7 +++++++ src/model/index.ts | 2 +- src/storage/db/index.ts | 5 +++-- 4 files changed, 35 insertions(+), 24 deletions(-) create mode 100644 src/model/env.ts diff --git a/src/consts/env.ts b/src/consts/env.ts index 19c6e0b9..bf8ebf99 100644 --- a/src/consts/env.ts +++ b/src/consts/env.ts @@ -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' @@ -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 = Consts?.expoConfig export const env/* : Readonly */ = { - 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 @@ -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 diff --git a/src/model/env.ts b/src/model/env.ts new file mode 100644 index 00000000..b8e82b19 --- /dev/null +++ b/src/model/env.ts @@ -0,0 +1,7 @@ +export interface IEnv { + NODE_ENV: string + APP_VARIANT: string + DEBUG: string + NODE_ENV_SHORT: string + SENTRY_DSN: string +} \ No newline at end of file diff --git a/src/model/index.ts b/src/model/index.ts index a3bc27e9..d18087df 100644 --- a/src/model/index.ts +++ b/src/model/index.ts @@ -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?: { diff --git a/src/storage/db/index.ts b/src/storage/db/index.ts index 95b027b5..e4464c11 100644 --- a/src/storage/db/index.ts +++ b/src/storage/db/index.ts @@ -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' @@ -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')) } @@ -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 },