Skip to content

Commit

Permalink
tsukota: Add config mod
Browse files Browse the repository at this point in the history
  • Loading branch information
bouzuya committed Jun 29, 2023
1 parent f9ed6c6 commit 3435f81
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 58 deletions.
5 changes: 2 additions & 3 deletions 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";
Expand All @@ -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";
Expand Down Expand Up @@ -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 (
<Screen options={{ title: "tsukota" }}>
Expand Down
81 changes: 81 additions & 0 deletions 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;
}
54 changes: 2 additions & 52 deletions 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,
Expand All @@ -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,
Expand Down
5 changes: 2 additions & 3 deletions packages/tsukota/lib/time-span.ts
@@ -1,11 +1,10 @@
import Constants from "expo-constants";
import { getConfig } from "./config";

export async function timeSpan<T>(
label: string,
callback: () => Promise<T>
): Promise<T> {
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();
Expand Down

0 comments on commit 3435f81

Please sign in to comment.