Skip to content

Commit f988ada

Browse files
committed
Consolidate environment constants (IS_DEV, IS_TEST, IS_PROD, IS_CI) in common/env
1 parent e9cffd5 commit f988ada

File tree

8 files changed

+28
-40
lines changed

8 files changed

+28
-40
lines changed

cli/src/utils/analytics.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { env } from '@codebuff/common/env'
1+
import { env, IS_PROD } from '@codebuff/common/env'
22
import { PostHog } from 'posthog-node'
33

44
import type { AnalyticsEvent } from '@codebuff/common/constants/analytics-events'
@@ -59,7 +59,7 @@ export function initAnalytics() {
5959
try {
6060
client = new PostHog(env.NEXT_PUBLIC_POSTHOG_API_KEY, {
6161
host: env.NEXT_PUBLIC_POSTHOG_HOST_URL,
62-
enableExceptionAutocapture: env.NEXT_PUBLIC_CB_ENVIRONMENT === 'prod',
62+
enableExceptionAutocapture: IS_PROD,
6363
})
6464
} catch (error) {
6565
logAnalyticsError(error, { stage: AnalyticsErrorStage.Init })
@@ -89,7 +89,7 @@ export function trackEvent(
8989
return
9090
}
9191
if (!client) {
92-
if (env.NEXT_PUBLIC_CB_ENVIRONMENT === 'prod') {
92+
if (IS_PROD) {
9393
const error = new Error('Analytics client not initialized')
9494
logAnalyticsError(error, {
9595
stage: AnalyticsErrorStage.Track,
@@ -101,7 +101,7 @@ export function trackEvent(
101101
return
102102
}
103103

104-
if (env.NEXT_PUBLIC_CB_ENVIRONMENT !== 'prod') {
104+
if (!IS_PROD) {
105105
if (DEBUG_DEV_EVENTS) {
106106
console.log('Analytics event sent', {
107107
event,
@@ -139,7 +139,7 @@ export function identifyUser(userId: string, properties?: Record<string, any>) {
139139
throw error
140140
}
141141

142-
if (env.NEXT_PUBLIC_CB_ENVIRONMENT !== 'prod') {
142+
if (!IS_PROD) {
143143
if (DEBUG_DEV_EVENTS) {
144144
console.log('Identify event sent', {
145145
userId,

cli/src/utils/logger.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { appendFileSync, existsSync, mkdirSync, unlinkSync } from 'fs'
22
import path, { dirname } from 'path'
33
import { format as stringFormat } from 'util'
44

5-
import { env } from '@codebuff/common/env'
5+
import { env, IS_DEV, IS_TEST, IS_CI } from '@codebuff/common/env'
66
import { createAnalyticsDispatcher } from '@codebuff/common/util/analytics-dispatcher'
77
import { pino } from 'pino'
88

@@ -101,16 +101,11 @@ function sendAnalyticsAndLog(
101101
msg?: string,
102102
...args: any[]
103103
): void {
104-
const envName = env.NEXT_PUBLIC_CB_ENVIRONMENT
105-
const isDevEnv = envName === 'dev'
106-
const isTestEnv = envName === 'test'
107-
const isCi = process.env.CODEBUFF_GITHUB_ACTIONS === 'true'
108-
109-
if (!isCi && !isTestEnv) {
104+
if (!IS_CI && !IS_TEST) {
110105
const projectRoot = getProjectRoot()
111106

112107
const logTarget =
113-
isDevEnv
108+
IS_DEV
114109
? path.join(projectRoot, 'debug', 'cli.jsonl')
115110
: path.join(getCurrentChatDir(), 'log.jsonl')
116111

@@ -131,7 +126,7 @@ function sendAnalyticsAndLog(
131126

132127
logAsErrorIfNeeded(toTrack)
133128

134-
if (!isDevEnv && includeData && typeof normalizedData === 'object') {
129+
if (!IS_DEV && includeData && typeof normalizedData === 'object') {
135130
const analyticsPayloads = analyticsDispatcher.process({
136131
data: normalizedData,
137132
level,
@@ -146,7 +141,7 @@ function sendAnalyticsAndLog(
146141

147142
// In dev mode, use appendFileSync for real-time logging (Bun has issues with pino sync)
148143
// In prod mode, use pino for better performance
149-
if (isDevEnv && logPath) {
144+
if (IS_DEV && logPath) {
150145
const logEntry = JSON.stringify({
151146
level: level.toUpperCase(),
152147
timestamp: new Date().toISOString(),

common/src/analytics.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { PostHog } from 'posthog-node'
22

3-
import { env } from '@codebuff/common/env'
3+
import { env, IS_PROD } from '@codebuff/common/env'
44

55
import type { AnalyticsEvent } from './constants/analytics-events'
66
import type { Logger } from '@codebuff/common/types/contracts/logger'
@@ -46,7 +46,7 @@ export function trackEvent({
4646
properties?: Record<string, any>
4747
logger: Logger
4848
}) {
49-
if (env.NEXT_PUBLIC_CB_ENVIRONMENT !== 'prod') {
49+
if (!IS_PROD) {
5050
// Note (James): This log was too noisy. Reenable it as you need to test something.
5151
// logger.info({ payload: { event, properties } }, event)
5252
return

common/src/env.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,9 @@ if (process.env.NEXT_PUBLIC_CB_ENVIRONMENT !== 'prod') {
66
}
77

88
export const env = clientEnvSchema.parse(clientProcessEnv)
9+
10+
// Derived environment constants for convenience
11+
export const IS_DEV = env.NEXT_PUBLIC_CB_ENVIRONMENT === 'dev'
12+
export const IS_TEST = env.NEXT_PUBLIC_CB_ENVIRONMENT === 'test'
13+
export const IS_PROD = env.NEXT_PUBLIC_CB_ENVIRONMENT === 'prod'
14+
export const IS_CI = process.env.CODEBUFF_GITHUB_ACTIONS === 'true'

evals/logger.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { mkdirSync } from 'fs'
22
import path, { dirname } from 'path'
33

4-
import { env } from '@codebuff/common/env'
4+
import { IS_CI, IS_TEST } from '@codebuff/common/env'
55
import { pino } from 'pino'
66

77
let logPath: string | undefined = undefined
@@ -38,10 +38,7 @@ function initPinoLoggerWithPath(path: string): void {
3838
}
3939

4040
function log(level: LogLevel, data: any, msg?: string, ...args: any[]): void {
41-
if (
42-
process.env.CODEBUFF_GITHUB_ACTIONS !== 'true' &&
43-
env.NEXT_PUBLIC_CB_ENVIRONMENT !== 'test'
44-
) {
41+
if (!IS_CI && !IS_TEST) {
4542
const projectRoot = path.join(__dirname, '..')
4643
const logTarget = path.join(projectRoot, 'debug', 'evals.log')
4744

packages/bigquery/src/client.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { env } from '@codebuff/common/env'
1+
import { IS_PROD } from '@codebuff/common/env'
22
import { getErrorObject } from '@codebuff/common/util/error'
33
import { BigQuery } from '@google-cloud/bigquery'
44

@@ -8,10 +8,7 @@ import type { BaseTrace, GetRelevantFilesTrace, Relabel, Trace } from './schema'
88
import type { MessageRow } from '@codebuff/common/types/contracts/bigquery'
99
import type { Logger } from '@codebuff/common/types/contracts/logger'
1010

11-
const DATASET =
12-
env.NEXT_PUBLIC_CB_ENVIRONMENT === 'prod'
13-
? 'codebuff_data'
14-
: 'codebuff_data_dev'
11+
const DATASET = IS_PROD ? 'codebuff_data' : 'codebuff_data_dev'
1512

1613
const TRACES_TABLE = 'traces'
1714
const RELABELS_TABLE = 'relabels'

sdk/src/constants.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
import { env } from '@codebuff/common/env'
1+
import { env, IS_DEV, IS_TEST, IS_PROD } from '@codebuff/common/env'
22

3-
export const CODEBUFF_BINARY = 'codebuff'
3+
export { IS_DEV, IS_TEST, IS_PROD }
44

5-
export const IS_DEV = env.NEXT_PUBLIC_CB_ENVIRONMENT === 'dev'
6-
export const IS_TEST = env.NEXT_PUBLIC_CB_ENVIRONMENT === 'test'
7-
export const IS_PROD = !IS_DEV && !IS_TEST
5+
export const CODEBUFF_BINARY = 'codebuff'
86

97
export const WEBSITE_URL = env.NEXT_PUBLIC_CODEBUFF_APP_URL
108

web/src/util/logger.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import path from 'path'
33
import { format } from 'util'
44

55
import { trackEvent } from '@codebuff/common/analytics'
6-
import { env } from '@codebuff/common/env'
6+
import { env, IS_DEV, IS_CI } from '@codebuff/common/env'
77
import { createAnalyticsDispatcher } from '@codebuff/common/util/analytics-dispatcher'
88
import { splitData } from '@codebuff/common/util/split-data'
99
import pino from 'pino'
@@ -36,10 +36,7 @@ function getDebugDir(): string | null {
3636
}
3737

3838
// Initialize debug directory in dev environment
39-
if (
40-
env.NEXT_PUBLIC_CB_ENVIRONMENT === 'dev' &&
41-
process.env.CODEBUFF_GITHUB_ACTIONS !== 'true'
42-
) {
39+
if (IS_DEV && !IS_CI) {
4340
const dir = getDebugDir()
4441
if (dir) {
4542
try {
@@ -107,9 +104,7 @@ function splitAndLog(
107104
// Also output to console via pinoLogger so logs remain visible in the terminal
108105
function logWithSync(level: LogLevel, data: any, msg?: string, ...args: any[]): void {
109106
const formattedMsg = format(msg ?? '', ...args)
110-
const isDevEnv = env.NEXT_PUBLIC_CB_ENVIRONMENT === 'dev'
111-
112-
if (isDevEnv) {
107+
if (IS_DEV) {
113108
// Write to file for real-time logging
114109
if (debugDir) {
115110
const logEntry = JSON.stringify({

0 commit comments

Comments
 (0)