Home / Function/ updateEnvVars() — ui Function Reference

updateEnvVars() — ui Function Reference

Architecture documentation for the updateEnvVars() function in update-env-vars.ts from the ui codebase.

Entity Profile

Dependency Diagram

graph TD
  2bd4115e_3585_6d12_8eb1_e8800a6d2a9b["updateEnvVars()"]
  a5718d0a_a699_b571_7ac6_482ff9047b4c["update-env-vars.ts"]
  2bd4115e_3585_6d12_8eb1_e8800a6d2a9b -->|defined in| a5718d0a_a699_b571_7ac6_482ff9047b4c
  style 2bd4115e_3585_6d12_8eb1_e8800a6d2a9b fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/shadcn/src/utils/updaters/update-env-vars.ts lines 15–108

export async function updateEnvVars(
  envVars: z.infer<typeof registryItemEnvVarsSchema> | undefined,
  config: Config,
  options: {
    silent?: boolean
  }
) {
  if (!envVars || Object.keys(envVars).length === 0) {
    return {
      envVarsAdded: [],
      envFileUpdated: null,
      envFileCreated: null,
    }
  }

  options = {
    silent: false,
    ...options,
  }

  const envSpinner = spinner(`Adding environment variables.`, {
    silent: options.silent,
  })?.start()

  const projectRoot = config.resolvedPaths.cwd

  // Find existing env file or use .env.local as default.
  let envFilePath = path.join(projectRoot, ".env.local")
  const existingEnvFile = findExistingEnvFile(projectRoot)

  if (existingEnvFile) {
    envFilePath = existingEnvFile
  }

  const envFileExists = existsSync(envFilePath)
  const envFileName = path.basename(envFilePath)

  // Convert envVars object to env file format
  const newEnvContent = Object.entries(envVars)
    .map(([key, value]) => `${key}=${value}`)
    .join("\n")

  let envVarsAdded: string[] = []
  let envFileUpdated: string | null = null
  let envFileCreated: string | null = null

  if (envFileExists) {
    const existingContent = await fs.readFile(envFilePath, "utf-8")
    const mergedContent = mergeEnvContent(existingContent, newEnvContent)
    envVarsAdded = getNewEnvKeys(existingContent, newEnvContent)

    if (envVarsAdded.length > 0) {
      await fs.writeFile(envFilePath, mergedContent, "utf-8")
      envFileUpdated = path.relative(projectRoot, envFilePath)

      envSpinner?.succeed(
        `Added the following variables to ${highlighter.info(envFileName)}:`
      )

      if (!options.silent) {
        for (const key of envVarsAdded) {
          logger.log(`  ${highlighter.success("+")} ${key}`)
        }
      }
    } else {
      envSpinner?.stop()
    }
  } else {
    // Create new env file
    await fs.writeFile(envFilePath, newEnvContent + "\n", "utf-8")
    envFileCreated = path.relative(projectRoot, envFilePath)
    envVarsAdded = Object.keys(envVars)

    envSpinner?.succeed(
      `Added the following variables to ${highlighter.info(envFileName)}:`
    )

    if (!options.silent) {
      for (const key of envVarsAdded) {
        logger.log(`  ${highlighter.success("+")} ${key}`)
      }

Subdomains

Frequently Asked Questions

What does updateEnvVars() do?
updateEnvVars() is a function in the ui codebase, defined in packages/shadcn/src/utils/updaters/update-env-vars.ts.
Where is updateEnvVars() defined?
updateEnvVars() is defined in packages/shadcn/src/utils/updaters/update-env-vars.ts at line 15.

Analyze Your Own Codebase

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

Try Supermodel Free