All files / src resolve-kit-scripts.ts

86.48% Statements 64/74
71.42% Branches 20/28
77.77% Functions 14/18
86.48% Lines 64/74

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 751x 1x 1x 1x 1x 1x 1x 10x 10x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 12x 12x 12x 12x 12x 1x 1x 1x 1x 1x 1x 10x 10x 10x 10x 10x 9x 9x 10x 10x 1x 1x 1x 1x 1x 1x           3x 3x       10x 1x 1x 10x 10x 10x 10x 10x     10x 10x 10x  
import * as fs from "node:fs"
import * as path from "node:path"
 
import { bundledKitAgentDir } from "./kit-installer.js"
import { kitRootFromPackageRoot } from "./load-from-sqlite.js"
 
export function workspaceScriptsDir(packageRoot: string): string {
  return path.join(kitRootFromPackageRoot(packageRoot), ".agent", "scripts")
}
 
export function extensionScriptsDir(extensionPath: string): string {
  return path.join(bundledKitAgentDir(extensionPath), "scripts")
}
 
/** Dev VSIX may ship stale bundled/kit — try monorepo `.agent/scripts` next. */
export function extensionScriptsCandidates(extensionPath: string): string[] {
  const resolved = path.resolve(extensionPath)
  const candidates = [
    extensionScriptsDir(resolved),
    path.join(resolved, "..", ".agent", "scripts"),
  ]
  return [...new Set(candidates.map((dir) => path.resolve(dir)))]
}
 
export function scriptsSupportDeliveryForm(scriptsDir: string): boolean {
  return (
    fs.existsSync(path.join(scriptsDir, "meridian_db_export.py")) &&
    fs.existsSync(path.join(scriptsDir, "meridian_delivery_form.py"))
  )
}
 
/**
 * Prefer workspace `.agent/scripts` when form-capable; otherwise extension bundled/monorepo kit.
 * `packageRoot` is still passed to Python for `.meridian/meridian.db` — only script location changes.
 */
export function resolveKitScriptsDir(
  packageRoot: string,
  extensionPath?: string,
): string | null {
  const workspaceScripts = workspaceScriptsDir(packageRoot)
  if (scriptsSupportDeliveryForm(workspaceScripts)) {
    return workspaceScripts
  }
 
  if (extensionPath) {
    for (const extensionScripts of extensionScriptsCandidates(extensionPath)) {
      if (scriptsSupportDeliveryForm(extensionScripts)) {
        return extensionScripts
      }
    }
    for (const extensionScripts of extensionScriptsCandidates(extensionPath)) {
      if (fs.existsSync(path.join(extensionScripts, "meridian_db_export.py"))) {
        return extensionScripts
      }
    }
  }
 
  if (fs.existsSync(path.join(workspaceScripts, "meridian_db_export.py"))) {
    return workspaceScripts
  }
  return null
}
 
export function resolveExportScriptPath(
  packageRoot: string,
  extensionPath?: string,
): string | null {
  const scriptsDir = resolveKitScriptsDir(packageRoot, extensionPath)
  if (!scriptsDir) {
    return null
  }
  const script = path.join(scriptsDir, "meridian_db_export.py")
  return fs.existsSync(script) ? script : null
}