Home / Function/ init() — ui Function Reference

init() — ui Function Reference

Architecture documentation for the init() function in init.ts from the ui codebase.

Entity Profile

Dependency Diagram

graph TD
  f8868a17_e9a2_805a_984b_553ed4170019["init()"]
  f50d8e38_92d7_c930_5ca6_b71673ceb658["init.ts"]
  f8868a17_e9a2_805a_984b_553ed4170019 -->|defined in| f50d8e38_92d7_c930_5ca6_b71673ceb658
  56c01a63_5907_6b2d_a507_d935d5ec6f68["runInit()"]
  f8868a17_e9a2_805a_984b_553ed4170019 -->|calls| 56c01a63_5907_6b2d_a507_d935d5ec6f68
  style f8868a17_e9a2_805a_984b_553ed4170019 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/shadcn/src/commands/init.ts lines 147–269

  .action(async (components, opts) => {
    try {
      // Apply defaults when --defaults flag is set.
      if (opts.defaults) {
        opts.template = opts.template || "next"
        opts.baseColor = opts.baseColor || "neutral"
      }

      const options = initOptionsSchema.parse({
        cwd: path.resolve(opts.cwd),
        isNewProject: false,
        components,
        ...opts,
      })

      await loadEnvFiles(options.cwd)

      // We need to check if we're initializing with a new style.
      // This will allow us to determine if we need to install the base style.
      // And if we should prompt the user for a base color.
      if (components.length > 0) {
        // We don't know the full config at this point.
        // So we'll use a shadow config to fetch the first item.
        let shadowConfig = configWithDefaults(
          createConfig({
            resolvedPaths: {
              cwd: options.cwd,
            },
          })
        )

        // Check if there's a components.json file.
        // If so, we'll merge with our shadow config.
        const componentsJsonPath = path.resolve(options.cwd, "components.json")
        if (fsExtra.existsSync(componentsJsonPath)) {
          const existingConfig = await fsExtra.readJson(componentsJsonPath)
          const config = rawConfigSchema.partial().parse(existingConfig)
          const baseConfig = createConfig({
            resolvedPaths: {
              cwd: options.cwd,
            },
          })
          shadowConfig = configWithDefaults({
            ...config,
            resolvedPaths: {
              ...baseConfig.resolvedPaths,
              cwd: options.cwd,
            },
          })

          // Since components.json might not be valid at this point.
          // Temporarily rename components.json to allow preflight to run.
          // We'll rename it back after preflight.
          createFileBackup(componentsJsonPath)
        }

        // Ensure all registries used in components are configured.
        const { config: updatedConfig } = await ensureRegistriesInConfig(
          components,
          shadowConfig,
          {
            silent: true,
            writeFile: false,
          }
        )
        shadowConfig = updatedConfig

        // This forces a shadowConfig validation early in the process.
        buildUrlAndHeadersForRegistryItem(components[0], shadowConfig)

        const [item] = await getRegistryItems([components[0]], {
          config: shadowConfig,
        })

        // Set options from registry:base.
        if (item?.type === "registry:base") {
          if (item.config) {
            // Merge config values into shadowConfig.
            shadowConfig = configWithDefaults(
              deepmerge(shadowConfig, item.config)
            )

Subdomains

Calls

Frequently Asked Questions

What does init() do?
init() is a function in the ui codebase, defined in packages/shadcn/src/commands/init.ts.
Where is init() defined?
init() is defined in packages/shadcn/src/commands/init.ts at line 147.
What does init() call?
init() calls 1 function(s): runInit.

Analyze Your Own Codebase

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

Try Supermodel Free