Home / Function/ findCommonRoot() — ui Function Reference

findCommonRoot() — ui Function Reference

Architecture documentation for the findCommonRoot() function in update-files.ts from the ui codebase.

Entity Profile

Dependency Diagram

graph TD
  4b6decbf_93a4_8403_9ccd_daf4d79c041f["findCommonRoot()"]
  3efb2c23_7931_d244_b4e1_85acab69298c["update-files.ts"]
  4b6decbf_93a4_8403_9ccd_daf4d79c041f -->|defined in| 3efb2c23_7931_d244_b4e1_85acab69298c
  bc52ca78_0228_54e7_56b2_602b341e2ec4["updateFiles()"]
  bc52ca78_0228_54e7_56b2_602b341e2ec4 -->|calls| 4b6decbf_93a4_8403_9ccd_daf4d79c041f
  style 4b6decbf_93a4_8403_9ccd_daf4d79c041f fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/shadcn/src/utils/updaters/update-files.ts lines 417–447

export function findCommonRoot(paths: string[], needle: string): string {
  // Remove leading slashes for consistent handling
  const normalizedPaths = paths.map((p) => p.replace(/^\//, ""))
  const normalizedNeedle = needle.replace(/^\//, "")

  // Get the directory path of the needle by removing the file name
  const needleDir = normalizedNeedle.split("/").slice(0, -1).join("/")

  // If needle is at root level, return empty string
  if (!needleDir) {
    return ""
  }

  // Split the needle directory into segments
  const needleSegments = needleDir.split("/")

  // Start from the full path and work backwards
  for (let i = needleSegments.length; i > 0; i--) {
    const testPath = needleSegments.slice(0, i).join("/")
    // Check if this is a common root by verifying if any other paths start with it
    const hasRelatedPaths = normalizedPaths.some(
      (path) => path !== normalizedNeedle && path.startsWith(testPath + "/")
    )
    if (hasRelatedPaths) {
      return "/" + testPath // Add leading slash back for the result
    }
  }

  // If no common root found with other files, return the parent directory of the needle
  return "/" + needleDir // Add leading slash back for the result
}

Subdomains

Called By

Frequently Asked Questions

What does findCommonRoot() do?
findCommonRoot() is a function in the ui codebase, defined in packages/shadcn/src/utils/updaters/update-files.ts.
Where is findCommonRoot() defined?
findCommonRoot() is defined in packages/shadcn/src/utils/updaters/update-files.ts at line 417.
What calls findCommonRoot()?
findCommonRoot() is called by 1 function(s): updateFiles.

Analyze Your Own Codebase

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

Try Supermodel Free