Home / File/ update-env-vars.ts — ui Source File

update-env-vars.ts — ui Source File

Architecture documentation for update-env-vars.ts, a typescript file in the ui codebase. 9 imports, 1 dependents.

File typescript FrameworkTooling CLICore 9 imports 1 dependents 1 functions

Entity Profile

Dependency Diagram

graph LR
  a5718d0a_a699_b571_7ac6_482ff9047b4c["update-env-vars.ts"]
  eac8f98f_e40a_7fe8_f505_372c83d20c7a["fs"]
  a5718d0a_a699_b571_7ac6_482ff9047b4c --> eac8f98f_e40a_7fe8_f505_372c83d20c7a
  d05ec4ea_7a68_3a36_bfa4_9ba7f8409ee5["path"]
  a5718d0a_a699_b571_7ac6_482ff9047b4c --> d05ec4ea_7a68_3a36_bfa4_9ba7f8409ee5
  a3b2545e_3d8c_699d_ef11_6ab18db14666["schema"]
  a5718d0a_a699_b571_7ac6_482ff9047b4c --> a3b2545e_3d8c_699d_ef11_6ab18db14666
  27496285_2f75_6442_1801_055f2601e84b["env-helpers"]
  a5718d0a_a699_b571_7ac6_482ff9047b4c --> 27496285_2f75_6442_1801_055f2601e84b
  b2895591_2a74_d518_deda_2f26be766dcb["get-config"]
  a5718d0a_a699_b571_7ac6_482ff9047b4c --> b2895591_2a74_d518_deda_2f26be766dcb
  15e8bad0_00cc_3d96_8e33_2f062120ea7f["highlighter"]
  a5718d0a_a699_b571_7ac6_482ff9047b4c --> 15e8bad0_00cc_3d96_8e33_2f062120ea7f
  1df8bbed_5110_29f0_12f0_996fc7a1eda1["logger"]
  a5718d0a_a699_b571_7ac6_482ff9047b4c --> 1df8bbed_5110_29f0_12f0_996fc7a1eda1
  a3e9bc4e_1faf_6261_a1db_396981c7761d["spinner"]
  a5718d0a_a699_b571_7ac6_482ff9047b4c --> a3e9bc4e_1faf_6261_a1db_396981c7761d
  6802ce19_522d_e5fb_e458_8826d9f6952e["zod"]
  a5718d0a_a699_b571_7ac6_482ff9047b4c --> 6802ce19_522d_e5fb_e458_8826d9f6952e
  2426dbbb_a6b0_c1dc_4af9_aad17c12ea4b["update-env-vars.test.ts"]
  2426dbbb_a6b0_c1dc_4af9_aad17c12ea4b --> a5718d0a_a699_b571_7ac6_482ff9047b4c
  style a5718d0a_a699_b571_7ac6_482ff9047b4c fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import { existsSync, promises as fs } from "fs"
import path from "path"
import { registryItemEnvVarsSchema } from "@/src/schema"
import {
  findExistingEnvFile,
  getNewEnvKeys,
  mergeEnvContent,
} from "@/src/utils/env-helpers"
import { Config } from "@/src/utils/get-config"
import { highlighter } from "@/src/utils/highlighter"
import { logger } from "@/src/utils/logger"
import { spinner } from "@/src/utils/spinner"
import { z } from "zod"

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}`)
      }
    }
  }

  if (!options.silent && envVarsAdded.length > 0) {
    logger.break()
  }

  return {
    envVarsAdded,
    envFileUpdated,
    envFileCreated,
  }
}

Subdomains

Functions

Dependencies

  • env-helpers
  • fs
  • get-config
  • highlighter
  • logger
  • path
  • schema
  • spinner
  • zod

Frequently Asked Questions

What does update-env-vars.ts do?
update-env-vars.ts is a source file in the ui codebase, written in typescript. It belongs to the FrameworkTooling domain, CLICore subdomain.
What functions are defined in update-env-vars.ts?
update-env-vars.ts defines 1 function(s): updateEnvVars.
What does update-env-vars.ts depend on?
update-env-vars.ts imports 9 module(s): env-helpers, fs, get-config, highlighter, logger, path, schema, spinner, and 1 more.
What files import update-env-vars.ts?
update-env-vars.ts is imported by 1 file(s): update-env-vars.test.ts.
Where is update-env-vars.ts in the architecture?
update-env-vars.ts is located at packages/shadcn/src/utils/updaters/update-env-vars.ts (domain: FrameworkTooling, subdomain: CLICore, directory: packages/shadcn/src/utils/updaters).

Analyze Your Own Codebase

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

Try Supermodel Free