From 3435f8168bdd21feac60df6324deb672136bd44b Mon Sep 17 00:00:00 2001 From: bouzuya Date: Thu, 29 Jun 2023 20:56:26 +0900 Subject: [PATCH] tsukota: Add config mod --- packages/tsukota/app/index.tsx | 5 +- packages/tsukota/lib/config.ts | 81 +++++++++++++++++++++++++++++++ packages/tsukota/lib/firebase.ts | 54 +-------------------- packages/tsukota/lib/time-span.ts | 5 +- 4 files changed, 87 insertions(+), 58 deletions(-) create mode 100644 packages/tsukota/lib/config.ts diff --git a/packages/tsukota/app/index.tsx b/packages/tsukota/app/index.tsx index d9dd0c7..7fe9453 100644 --- a/packages/tsukota/app/index.tsx +++ b/packages/tsukota/app/index.tsx @@ -1,4 +1,3 @@ -import Constants from "expo-constants"; import { err } from "neverthrow"; import React, { useCallback, useEffect, useState } from "react"; import { Linking, StyleSheet } from "react-native"; @@ -14,6 +13,7 @@ import { useAccounts } from "../components/AccountContext"; import { useCurrentUserId } from "../hooks/use-credential"; import { deleteAccount } from "../lib/account"; import { getMinAppVersion, loadAccountIds } from "../lib/api"; +import { getConfig } from "../lib/config"; import { useTranslation } from "../lib/i18n"; import { useFocusEffect, useTypedNavigation } from "../lib/navigation"; import { showErrorMessage } from "../lib/show-error-message"; @@ -54,9 +54,8 @@ export function AccountIndex(): JSX.Element { if (currentUserId === null || minAppVersion === null) return <>; // FIXME: Move to root - const version = Constants.expoConfig?.version ?? "1.0.0"; + const { packageName, version } = getConfig(); if (semver.lt(version, minAppVersion)) { - const packageName = Constants.expoConfig?.android?.package ?? ""; const url = `https://play.google.com/store/apps/details?id=${packageName}`; return ( diff --git a/packages/tsukota/lib/config.ts b/packages/tsukota/lib/config.ts new file mode 100644 index 0000000..ca66636 --- /dev/null +++ b/packages/tsukota/lib/config.ts @@ -0,0 +1,81 @@ +import Constants from "expo-constants"; + +type Config = { + apiKey: string; + authEmulatorHost: string; + enableDebugLogging: boolean; + firestoreEmulatorHost: string; + functionsEmulatorHost: string; + packageName: string; + projectId: string; + version: string; +}; + +let cachedConfig: Config | null = null; + +export function getConfig(): Config { + if (cachedConfig !== null) return cachedConfig; + + const expoConfig = Constants.expoConfig; + if (expoConfig === null) throw new Error("assert expoConfig !== null"); + + const android = expoConfig.android; + if (android === undefined) throw new Error("assert android !== undefined"); + const packageName = android.package; + if (packageName === undefined) + throw new Error("assert android.package !== undefined"); + + const version = expoConfig.version; + if (version === undefined) throw new Error("assert version !== undefined"); + + const { extra } = expoConfig; + if (extra === undefined) throw new Error("assert extra !== undefined"); + + const { apiKey } = extra; + if (apiKey === undefined) throw new Error("assert apiKey !== undefined"); + if (typeof apiKey !== "string") + throw new Error("assert typeof apiKey === string"); + + const { authEmulatorHost } = extra; + if (authEmulatorHost === undefined) + throw new Error("assert authEmulatorHost !== undefined"); + if (typeof authEmulatorHost !== "string") + throw new Error("assert typeof authEmulatorHost === string"); + + const { enableDebugLogging } = extra; + if (enableDebugLogging === undefined) + throw new Error("assert enableDebugLogging !== undefined"); + if (typeof enableDebugLogging !== "string") + throw new Error("assert typeof enableDebugLogging === string"); + + const { firestoreEmulatorHost } = extra; + if (firestoreEmulatorHost === undefined) + throw new Error("assert firestoreEmulatorHost !== undefined"); + if (typeof firestoreEmulatorHost !== "string") + throw new Error("assert typeof firestoreEmulatorHost === string"); + + const { functionsEmulatorHost } = extra; + if (functionsEmulatorHost === undefined) + throw new Error("assert functionsEmulatorHost !== undefined"); + if (typeof functionsEmulatorHost !== "string") + throw new Error("assert typeof functionsEmulatorHost === string"); + + const { projectId } = extra; + if (projectId === undefined) + throw new Error("assert projectId !== undefined"); + if (typeof projectId !== "string") + throw new Error("assert typeof projectId === string"); + + cachedConfig = { + apiKey, + authEmulatorHost, + enableDebugLogging: enableDebugLogging === "true", + firestoreEmulatorHost, + functionsEmulatorHost, + packageName, + projectId, + version, + }; + + return cachedConfig; +} diff --git a/packages/tsukota/lib/firebase.ts b/packages/tsukota/lib/firebase.ts index baac665..ae9753b 100644 --- a/packages/tsukota/lib/firebase.ts +++ b/packages/tsukota/lib/firebase.ts @@ -1,5 +1,4 @@ import AsyncStorage from "@react-native-async-storage/async-storage"; -import Constants from "expo-constants"; import { FirebaseOptions, initializeApp } from "firebase/app"; import { Firestore, @@ -18,63 +17,14 @@ import { initializeAuth, } from "firebase/auth/react-native"; import { Auth } from "firebase/auth"; - -function getExpoConfigExtra(): { - apiKey: string; - authEmulatorHost: string; - firestoreEmulatorHost: string; - functionsEmulatorHost: string; - projectId: string; -} { - const expoConfig = Constants.expoConfig; - if (expoConfig === null) throw new Error("assert expoConfig !== null"); - const { extra } = expoConfig; - if (extra === undefined) throw new Error("assert extra !== undefined"); - - const { apiKey } = extra; - if (apiKey === undefined) throw new Error("assert apiKey !== undefined"); - if (typeof apiKey !== "string") - throw new Error("assert typeof apiKey === string"); - - const { authEmulatorHost } = extra; - if (authEmulatorHost === undefined) - throw new Error("assert authEmulatorHost !== undefined"); - if (typeof authEmulatorHost !== "string") - throw new Error("assert typeof authEmulatorHost === string"); - - const { firestoreEmulatorHost } = extra; - if (firestoreEmulatorHost === undefined) - throw new Error("assert firestoreEmulatorHost !== undefined"); - if (typeof firestoreEmulatorHost !== "string") - throw new Error("assert typeof firestoreEmulatorHost === string"); - - const { functionsEmulatorHost } = extra; - if (functionsEmulatorHost === undefined) - throw new Error("assert functionsEmulatorHost !== undefined"); - if (typeof functionsEmulatorHost !== "string") - throw new Error("assert typeof functionsEmulatorHost === string"); - - const { projectId } = extra; - if (projectId === undefined) - throw new Error("assert projectId !== undefined"); - if (typeof projectId !== "string") - throw new Error("assert typeof projectId === string"); - - return { - apiKey, - authEmulatorHost, - firestoreEmulatorHost, - functionsEmulatorHost, - projectId, - }; -} +import { getConfig } from "./config"; function initializeFirebaseInstances(): { auth: Auth; db: Firestore; functions: Functions; } { - const extra = getExpoConfigExtra(); + const extra = getConfig(); const { apiKey, projectId } = extra; const firebaseOptions: FirebaseOptions = { apiKey, diff --git a/packages/tsukota/lib/time-span.ts b/packages/tsukota/lib/time-span.ts index a04a94f..bbbd579 100644 --- a/packages/tsukota/lib/time-span.ts +++ b/packages/tsukota/lib/time-span.ts @@ -1,11 +1,10 @@ -import Constants from "expo-constants"; +import { getConfig } from "./config"; export async function timeSpan( label: string, callback: () => Promise ): Promise { - if (Constants.expoConfig?.extra?.enableDebugLogging !== "true") - return await callback(); + if (!getConfig().enableDebugLogging) return await callback(); const start = new Date(); console.log(`${label} start`, start.toISOString()); const result = await callback();