Home / File/ env-helpers.ts — ui Source File

env-helpers.ts — ui Source File

Architecture documentation for env-helpers.ts, a typescript file in the ui codebase. 2 imports, 1 dependents.

File typescript FrameworkTooling TemplateSync 2 imports 1 dependents 5 functions

Entity Profile

Dependency Diagram

graph LR
  6ea7c0f6_8e34_1742_bb2f_90e611bb6eda["env-helpers.ts"]
  eac8f98f_e40a_7fe8_f505_372c83d20c7a["fs"]
  6ea7c0f6_8e34_1742_bb2f_90e611bb6eda --> eac8f98f_e40a_7fe8_f505_372c83d20c7a
  d05ec4ea_7a68_3a36_bfa4_9ba7f8409ee5["path"]
  6ea7c0f6_8e34_1742_bb2f_90e611bb6eda --> d05ec4ea_7a68_3a36_bfa4_9ba7f8409ee5
  6ff0b4fb_dc28_0e2d_2a10_3e2e1d0a3d6b["env-helpers.test.ts"]
  6ff0b4fb_dc28_0e2d_2a10_3e2e1d0a3d6b --> 6ea7c0f6_8e34_1742_bb2f_90e611bb6eda
  style 6ea7c0f6_8e34_1742_bb2f_90e611bb6eda fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import { existsSync } from "fs"
import path from "path"

export function isEnvFile(filePath: string) {
  const fileName = path.basename(filePath)
  return /^\.env(\.|$)/.test(fileName)
}

/**
 * Finds a file variant in the project.
 * TODO: abstract this to a more generic function.
 */
export function findExistingEnvFile(targetDir: string) {
  const variants = [
    ".env.local",
    ".env",
    ".env.development.local",
    ".env.development",
  ]

  for (const variant of variants) {
    const filePath = path.join(targetDir, variant)
    if (existsSync(filePath)) {
      return filePath
    }
  }

  return null
}

/**
 * Parse .env content into key-value pairs.
 */
export function parseEnvContent(content: string) {
  const lines = content.split("\n")
  const env: Record<string, string> = {}

  for (const line of lines) {
    const trimmed = line.trim()

    if (!trimmed || trimmed.startsWith("#")) {
      continue
    }

    // Find the first = and split there
    const equalIndex = trimmed.indexOf("=")
    if (equalIndex === -1) {
      continue
    }

    const key = trimmed.substring(0, equalIndex).trim()
    const value = trimmed.substring(equalIndex + 1).trim()

    if (key) {
      env[key] = value.replace(/^["']|["']$/g, "")
    }
  }

  return env
}

/**
 * Get the list of new keys that would be added when merging env content.
 */
export function getNewEnvKeys(existingContent: string, newContent: string) {
  const existingEnv = parseEnvContent(existingContent)
  const newEnv = parseEnvContent(newContent)

  const newKeys = []
  for (const key of Object.keys(newEnv)) {
    if (!(key in existingEnv)) {
      newKeys.push(key)
    }
  }

  return newKeys
}

/**
 * Merge env content by appending ONLY new keys that don't exist in the existing content.
 * Existing keys are preserved with their original values.
 */
export function mergeEnvContent(existingContent: string, newContent: string) {
  const existingEnv = parseEnvContent(existingContent)
  const newEnv = parseEnvContent(newContent)

  let result = existingContent.trimEnd()
  if (result && !result.endsWith("\n")) {
    result += "\n"
  }

  const newKeys: string[] = []
  for (const [key, value] of Object.entries(newEnv)) {
    if (!(key in existingEnv)) {
      newKeys.push(`${key}=${value}`)
    }
  }

  if (newKeys.length > 0) {
    if (result) {
      result += "\n"
    }
    result += newKeys.join("\n")
    return result + "\n"
  }

  // Ensure existing content ends with newline.
  if (result && !result.endsWith("\n")) {
    return result + "\n"
  }

  return result
}

Subdomains

Dependencies

  • fs
  • path

Frequently Asked Questions

What does env-helpers.ts do?
env-helpers.ts is a source file in the ui codebase, written in typescript. It belongs to the FrameworkTooling domain, TemplateSync subdomain.
What functions are defined in env-helpers.ts?
env-helpers.ts defines 5 function(s): findExistingEnvFile, getNewEnvKeys, isEnvFile, mergeEnvContent, parseEnvContent.
What does env-helpers.ts depend on?
env-helpers.ts imports 2 module(s): fs, path.
What files import env-helpers.ts?
env-helpers.ts is imported by 1 file(s): env-helpers.test.ts.
Where is env-helpers.ts in the architecture?
env-helpers.ts is located at packages/shadcn/src/utils/env-helpers.ts (domain: FrameworkTooling, subdomain: TemplateSync, directory: packages/shadcn/src/utils).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free