Skip to content

Commit

Permalink
Remove the environment indirections (#7089)
Browse files Browse the repository at this point in the history
* Use raw underlying globals for environment

* Set dev EXPO_PUBLIC_ENV by exclusion
  • Loading branch information
gaearon authored Dec 13, 2024
1 parent e2a7965 commit 356dad1
Show file tree
Hide file tree
Showing 18 changed files with 32 additions and 50 deletions.
1 change: 0 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

BITDRIFT_API_KEY=
SENTRY_AUTH_TOKEN=
EXPO_PUBLIC_ENV=development
EXPO_PUBLIC_LOG_LEVEL=debug
EXPO_PUBLIC_LOG_DEBUG=
EXPO_PUBLIC_BUNDLE_IDENTIFIER=
Expand Down
2 changes: 1 addition & 1 deletion app.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ module.exports = function (config) {
*/
const PLATFORM = process.env.EAS_BUILD_PLATFORM

const IS_DEV = process.env.EXPO_PUBLIC_ENV === 'development'
const IS_TESTFLIGHT = process.env.EXPO_PUBLIC_ENV === 'testflight'
const IS_PRODUCTION = process.env.EXPO_PUBLIC_ENV === 'production'
const IS_DEV = !IS_TESTFLIGHT || !IS_PRODUCTION

const ASSOCIATED_DOMAINS = [
'applinks:bsky.app',
Expand Down
3 changes: 1 addition & 2 deletions src/components/Typography.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
renderChildrenWithEmoji,
TextProps,
} from '#/alf/typography'
import {IS_DEV} from '#/env'
export type {TextProps}

/**
Expand All @@ -31,7 +30,7 @@ export function Text({
flags,
})

if (IS_DEV) {
if (__DEV__) {
if (!emoji && childHasEmoji(children)) {
logger.warn(
`Text: emoji detected but emoji not enabled: "${children}"\n\nPlease add <Text emoji />'`,
Expand Down
5 changes: 2 additions & 3 deletions src/components/dialogs/nuxs/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {useOnboardingState} from '#/state/shell'
* NUXs
*/
import {isSnoozed, snooze, unsnooze} from '#/components/dialogs/nuxs/snoozing'
import {IS_DEV} from '#/env'

type Context = {
activeNux: Nux | undefined
Expand Down Expand Up @@ -93,10 +92,10 @@ function Inner({
setActiveNux(undefined)
}, [activeNux, setActiveNux])

if (IS_DEV && typeof window !== 'undefined') {
if (__DEV__ && typeof window !== 'undefined') {
// @ts-ignore
window.clearNuxDialog = (id: Nux) => {
if (!IS_DEV || !id) return
if (!__DEV__ || !id) return
resetNuxs([id])
unsnooze()
}
Expand Down
2 changes: 0 additions & 2 deletions src/env.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
export const IS_DEV = __DEV__
export const IS_PROD = !IS_DEV
export const LOG_DEBUG = process.env.EXPO_PUBLIC_LOG_DEBUG || ''
export const LOG_LEVEL = (process.env.EXPO_PUBLIC_LOG_LEVEL || 'info') as
| 'debug'
Expand Down
8 changes: 3 additions & 5 deletions src/lib/app-info.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import {nativeApplicationVersion, nativeBuildVersion} from 'expo-application'

export const BUILD_ENV = process.env.EXPO_PUBLIC_ENV
export const IS_DEV = process.env.EXPO_PUBLIC_ENV === 'development'
export const IS_TESTFLIGHT = process.env.EXPO_PUBLIC_ENV === 'testflight'
export const IS_INTERNAL = IS_DEV || IS_TESTFLIGHT
export const IS_INTERNAL = __DEV__ || IS_TESTFLIGHT

// This is the commit hash that the current bundle was made from. The user can see the commit hash in the app's settings
// along with the other version info. Useful for debugging/reporting.
Expand All @@ -12,9 +10,9 @@ export const BUNDLE_IDENTIFIER = process.env.EXPO_PUBLIC_BUNDLE_IDENTIFIER ?? ''
// This will always be in the format of YYMMDD, so that it always increases for each build. This should only be used
// for Statsig reporting and shouldn't be used to identify a specific bundle.
export const BUNDLE_DATE =
IS_TESTFLIGHT || IS_DEV ? 0 : Number(process.env.EXPO_PUBLIC_BUNDLE_DATE)
IS_TESTFLIGHT || __DEV__ ? 0 : Number(process.env.EXPO_PUBLIC_BUNDLE_DATE)

export const appVersion = `${nativeApplicationVersion}.${nativeBuildVersion}`
export const bundleInfo = `${BUNDLE_IDENTIFIER} (${
IS_DEV ? 'dev' : IS_TESTFLIGHT ? 'tf' : 'prod'
__DEV__ ? 'dev' : IS_TESTFLIGHT ? 'tf' : 'prod'
})`
8 changes: 3 additions & 5 deletions src/lib/app-info.web.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import {version} from '../../package.json'

export const BUILD_ENV = process.env.EXPO_PUBLIC_ENV
export const IS_DEV = process.env.EXPO_PUBLIC_ENV === 'development'
export const IS_TESTFLIGHT = false
export const IS_INTERNAL = IS_DEV
export const IS_INTERNAL = __DEV__

// This is the commit hash that the current bundle was made from. The user can see the commit hash in the app's settings
// along with the other version info. Useful for debugging/reporting.
Expand All @@ -12,9 +10,9 @@ export const BUNDLE_IDENTIFIER =

// This will always be in the format of YYMMDD, so that it always increases for each build. This should only be used
// for Statsig reporting and shouldn't be used to identify a specific bundle.
export const BUNDLE_DATE = IS_DEV
export const BUNDLE_DATE = __DEV__
? 0
: Number(process.env.EXPO_PUBLIC_BUNDLE_DATE)

export const appVersion = version
export const bundleInfo = `${BUNDLE_IDENTIFIER} (${IS_DEV ? 'dev' : 'prod'})`
export const bundleInfo = `${BUNDLE_IDENTIFIER} (${__DEV__ ? 'dev' : 'prod'})`
4 changes: 1 addition & 3 deletions src/lib/hooks/useEnableKeyboardController.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import {
} from 'react-native-keyboard-controller'
import {useFocusEffect} from '@react-navigation/native'

import {IS_DEV} from '#/env'

const KeyboardControllerRefCountContext = createContext<{
incrementRefCount: () => void
decrementRefCount: () => void
Expand Down Expand Up @@ -57,7 +55,7 @@ function KeyboardControllerProviderInner({
refCount.current--
setEnabled(refCount.current > 0)

if (IS_DEV && refCount.current < 0) {
if (__DEV__ && refCount.current < 0) {
console.error('KeyboardController ref count < 0')
}
},
Expand Down
6 changes: 3 additions & 3 deletions src/lib/sentry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {Platform} from 'react-native'
import {nativeApplicationVersion, nativeBuildVersion} from 'expo-application'
import {init} from '@sentry/react-native'

import {BUILD_ENV, IS_DEV, IS_TESTFLIGHT} from '#/lib/app-info'
import {IS_TESTFLIGHT} from '#/lib/app-info'

/**
* Examples:
Expand All @@ -27,14 +27,14 @@ const release = nativeApplicationVersion ?? 'dev'
*/
const dist = `${Platform.OS}.${nativeBuildVersion}.${
IS_TESTFLIGHT ? 'tf' : ''
}${IS_DEV ? 'dev' : ''}`
}${__DEV__ ? 'dev' : ''}`

init({
enabled: !__DEV__,
autoSessionTracking: false,
dsn: 'https://05bc3789bf994b81bd7ce20c86ccd3ae@o4505071687041024.ingest.sentry.io/4505071690514432',
debug: false, // If `true`, Sentry will try to print out useful debugging information if something goes wrong with sending the event. Set it to `false` in production
environment: BUILD_ENV ?? 'development',
environment: process.env.NODE_ENV,
dist,
release,
})
4 changes: 2 additions & 2 deletions src/logger/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ logger.error(error[, metadata])

#### Modes

The "modes" referred to here are inferred from the values exported from `#/env`.
Basically, the booleans `IS_DEV` and `IS_PROD`.
The "modes" referred to here are inferred from `process.env.NODE_ENV`,
which matches how React Native sets the `__DEV__` global.

#### Log Levels

Expand Down
2 changes: 0 additions & 2 deletions src/logger/__tests__/logger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import {nanoid} from 'nanoid/non-secure'
import {Logger, LogLevel, sentryTransport} from '#/logger'

jest.mock('#/env', () => ({
IS_DEV: false,
IS_PROD: false,
/*
* Forces debug mode for tests using the default logger. Most tests create
* their own logger instance.
Expand Down
19 changes: 10 additions & 9 deletions src/logger/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,13 +270,14 @@ if (process.env.NODE_ENV !== 'test') {
logger.addTransport(createBitdriftTransport())
}

if (env.IS_DEV && process.env.NODE_ENV !== 'test') {
logger.addTransport(consoleTransport)

/*
* Comment this out to disable Sentry transport in dev
*/
// logger.addTransport(sentryTransport)
} else if (env.IS_PROD) {
logger.addTransport(sentryTransport)
if (process.env.NODE_ENV !== 'test') {
if (__DEV__) {
logger.addTransport(consoleTransport)
/*
* Comment this out to enable Sentry transport in dev
*/
// logger.addTransport(sentryTransport)
} else {
logger.addTransport(sentryTransport)
}
}
3 changes: 1 addition & 2 deletions src/screens/Onboarding/Layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import {Button, ButtonIcon, ButtonText} from '#/components/Button'
import {ChevronLeft_Stroke2_Corner0_Rounded as ChevronLeft} from '#/components/icons/Chevron'
import {createPortalGroup} from '#/components/Portal'
import {P, Text} from '#/components/Typography'
import {IS_DEV} from '#/env'

const COL_WIDTH = 420

Expand Down Expand Up @@ -64,7 +63,7 @@ export function Layout({children}: React.PropsWithChildren<{}>) {
a.flex_1,
t.atoms.bg,
]}>
{IS_DEV && (
{__DEV__ && (
<View style={[a.absolute, a.p_xl, a.z_10, {right: 0, top: insets.top}]}>
<Button
variant="ghost"
Expand Down
3 changes: 1 addition & 2 deletions src/state/geolocation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import EventEmitter from 'eventemitter3'

import {networkRetry} from '#/lib/async/retry'
import {logger} from '#/logger'
import {IS_DEV} from '#/env'
import {Device, device} from '#/storage'

const events = new EventEmitter()
Expand Down Expand Up @@ -65,7 +64,7 @@ export function beginResolveGeolocation() {
* In dev, IP server is unavailable, so we just set the default geolocation
* and fail closed.
*/
if (IS_DEV) {
if (__DEV__) {
geolocationResolution = new Promise(y => y())
device.set(['geolocation'], DEFAULT_GEOLOCATION)
return
Expand Down
3 changes: 1 addition & 2 deletions src/state/session/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {isWeb} from '#/platform/detection'
import * as persisted from '#/state/persisted'
import {useCloseAllActiveElements} from '#/state/util'
import {useGlobalDialogsControlContext} from '#/components/dialogs/Context'
import {IS_DEV} from '#/env'
import {emitSessionDropped} from '../events'
import {
agentToSessionAccount,
Expand Down Expand Up @@ -260,7 +259,7 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
)

// @ts-ignore
if (IS_DEV && isWeb) window.agent = state.currentAgentState.agent
if (__DEV__ && isWeb) window.agent = state.currentAgentState.agent

const agent = state.currentAgentState.agent as BskyAppAgent
const currentAgentRef = React.useRef(agent)
Expand Down
3 changes: 1 addition & 2 deletions src/state/shell/light-status-bar.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {createContext, useContext, useEffect, useState} from 'react'

import {isWeb} from '#/platform/detection'
import {IS_DEV} from '#/env'

const LightStatusBarRefCountContext = createContext<boolean>(false)
const SetLightStatusBarRefCountContext = createContext<React.Dispatch<
Expand All @@ -19,7 +18,7 @@ export function useSetLightStatusBar(enabled: boolean) {
if (isWeb) return

if (!setRefCount) {
if (IS_DEV)
if (__DEV__)
console.error(
'useLightStatusBar was used without a SetLightStatusBarRefCountContext provider',
)
Expand Down
3 changes: 1 addition & 2 deletions src/storage/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {MMKV} from 'react-native-mmkv'

import {IS_DEV} from '#/env'
import {Device} from '#/storage/schema'

export * from '#/storage/schema'
Expand Down Expand Up @@ -74,7 +73,7 @@ export class Storage<Scopes extends unknown[], Schema> {
*/
export const device = new Storage<[], Device>({id: 'bsky_device'})

if (IS_DEV && typeof window !== 'undefined') {
if (__DEV__ && typeof window !== 'undefined') {
// @ts-ignore
window.bsky_storage = {
device,
Expand Down
3 changes: 1 addition & 2 deletions src/view/com/util/text/Text.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
renderChildrenWithEmoji,
StringChild,
} from '#/alf/typography'
import {IS_DEV} from '#/env'

export type CustomTextProps = Omit<TextProps, 'children'> & {
type?: TypographyVariant
Expand Down Expand Up @@ -49,7 +48,7 @@ function Text_DEPRECATED({
const theme = useTheme()
const {fonts} = useAlf()

if (IS_DEV) {
if (__DEV__) {
if (!emoji && childHasEmoji(children)) {
logger.warn(
`Text: emoji detected but emoji not enabled: "${children}"\n\nPlease add <Text emoji />'`,
Expand Down

0 comments on commit 356dad1

Please sign in to comment.