Home / Function/ search() — ui Function Reference

search() — ui Function Reference

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

Entity Profile

Dependency Diagram

graph TD
  5053530a_7b20_2409_67c1_a60205523534["search()"]
  4e5f7e35_b0ab_caaf_1279_d9bb981befcc["search.ts"]
  5053530a_7b20_2409_67c1_a60205523534 -->|defined in| 4e5f7e35_b0ab_caaf_1279_d9bb981befcc
  style 5053530a_7b20_2409_67c1_a60205523534 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/shadcn/src/commands/search.ts lines 45–119

  .action(async (registries: string[], opts) => {
    try {
      const options = searchOptionsSchema.parse({
        cwd: path.resolve(opts.cwd),
        query: opts.query,
        limit: opts.limit ? parseInt(opts.limit, 10) : undefined,
        offset: opts.offset ? parseInt(opts.offset, 10) : undefined,
      })

      await loadEnvFiles(options.cwd)

      // Start with a shadow config to support partial components.json.
      // Use createConfig to get proper default paths
      const defaultConfig = createConfig({
        style: "new-york",
        resolvedPaths: {
          cwd: options.cwd,
        },
      })
      let shadowConfig = configWithDefaults(defaultConfig)

      // Check if there's a components.json file (partial or complete).
      const componentsJsonPath = path.resolve(options.cwd, "components.json")
      if (fsExtra.existsSync(componentsJsonPath)) {
        const existingConfig = await fsExtra.readJson(componentsJsonPath)
        const partialConfig = rawConfigSchema.partial().parse(existingConfig)
        shadowConfig = configWithDefaults({
          ...defaultConfig,
          ...partialConfig,
        })
      }

      // Try to get the full config, but fall back to shadow config if it fails.
      let config = shadowConfig
      try {
        const fullConfig = await getConfig(options.cwd)
        if (fullConfig) {
          config = configWithDefaults(fullConfig)
        }
      } catch {
        // Use shadow config if getConfig fails (partial components.json).
      }

      const { config: updatedConfig, newRegistries } =
        await ensureRegistriesInConfig(
          registries.map((registry) => `${registry}/registry`),
          config,
          {
            silent: true,
            writeFile: false,
          }
        )
      if (newRegistries.length > 0) {
        config.registries = updatedConfig.registries
      }

      // Validate registries early for better error messages.
      validateRegistryConfigForItems(registries, config)

      // Use searchRegistries for both search and non-search cases
      const results = await searchRegistries(registries as `@${string}`[], {
        query: options.query,
        limit: options.limit,
        offset: options.offset,
        config,
      })

      console.log(JSON.stringify(results, null, 2))
      process.exit(0)
    } catch (error) {
      handleError(error)
    } finally {
      clearRegistryContext()
    }
  })

Subdomains

Frequently Asked Questions

What does search() do?
search() is a function in the ui codebase, defined in packages/shadcn/src/commands/search.ts.
Where is search() defined?
search() is defined in packages/shadcn/src/commands/search.ts at line 45.

Analyze Your Own Codebase

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

Try Supermodel Free