All files / src load-from-sqlite.ts

88.88% Statements 136/153
58.82% Branches 20/34
81.25% Functions 13/16
88.88% Lines 136/153

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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 1541x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 13x 13x 1x 1x 13x 13x 1x 1x 7x 7x 7x 7x 7x     7x 7x 7x 1x 1x 1x 1x 1x 1x 1x     1x 1x 7x 7x 7x 7x     7x 7x           7x 7x 7x 7x 7x 7x 7x 7x 7x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 1x 1x 1x 1x 1x 1x 1x 7x 7x 7x             7x  
import { execFileSync } from "node:child_process"
import * as fs from "node:fs"
import * as path from "node:path"
 
import type { EpicSummary } from "./load-epics.js"
import type { SprintSummary } from "./load-sprints.js"
import type { VersionSummary } from "./load-versions.js"
import type { UserStory } from "./domain/types.js"
import type { PlanningPayload } from "./planning-payload.js"
import { findKitRoot, findKitScriptsRoot } from "./resolve-meridian-projects.js"
import { resolveExportScriptPath } from "./resolve-kit-scripts.js"
 
type PlanningExport = {
  userStories: Array<{
    id: string
    title: string
    epic: string
    version: string
    sprint: string | null
    status: UserStory["status"]
    moscow: UserStory["moscow"]
    dependsOn: string[]
    doneWhen: string
    tests: UserStory["tests"]
    testsStatus: UserStory["testsStatus"]
    ready: boolean
    summary: string | null
    preamble: string | null
  }>
  versions: Array<{
    id: string
    title: string
    status: string
    outcome: string
    summary: string | null
  }>
  epics: Array<{
    id: string
    title: string
    status: string
    outcome: string
    versions: string[]
    summary: string | null
  }>
  sprints: Array<{
    id: string
    version: string
    title: string
    status: string
    goal: string
    doneWhen: string
    stories: string[]
    summary: string | null
  }>
}
 
export function kitRootFromPackageRoot(packageRoot: string): string {
  return findKitScriptsRoot(packageRoot) ?? findKitRoot(packageRoot) ?? packageRoot
}
 
export function sqliteDbExists(packageRoot: string): boolean {
  return fs.existsSync(path.join(packageRoot, ".meridian", "meridian.db"))
}
 
export function resolvePythonCommand(): string {
  for (const cmd of ["python3", "python"]) {
    try {
      execFileSync(cmd, ["--version"], { encoding: "utf-8", stdio: "pipe" })
      return cmd
    } catch {
      continue
    }
  }
  return "python3"
}
 
export type SqlitePlanningResult = {
  payload: PlanningPayload | null
  error: string | null
}
 
export function loadPlanningPayloadFromSqlite(packageRoot: string): PlanningPayload | null {
  return loadPlanningPayloadFromSqliteDetailed(packageRoot).payload
}
 
export function loadPlanningPayloadFromSqliteDetailed(
  packageRoot: string,
  extensionPath?: string,
): SqlitePlanningResult {
  if (!sqliteDbExists(packageRoot)) {
    return { payload: null, error: null }
  }
  const script = resolveExportScriptPath(packageRoot, extensionPath)
  if (!script) {
    return {
      payload: null,
      error: `Kit script not found for ${packageRoot}`,
    }
  }
  const python = resolvePythonCommand()
  try {
    const stdout = execFileSync(
      python,
      [script, packageRoot, "--format", "planning"],
      { encoding: "utf-8", maxBuffer: 32 * 1024 * 1024 },
    )
    const data = JSON.parse(stdout) as PlanningExport
    const stories: UserStory[] = data.userStories.map((s) => ({
      id: s.id,
      title: s.title,
      epic: s.epic,
      version: s.version,
      sprint: s.sprint ?? null,
      status: s.status,
      moscow: s.moscow,
      dependsOn: s.dependsOn ?? [],
      doneWhen: s.doneWhen ?? "",
      tests: s.tests,
      testsStatus: s.testsStatus,
      ready: s.ready,
      preamble: s.preamble ?? null,
    }))
    const versions: VersionSummary[] = data.versions.map((v) => ({
      id: v.id,
      title: v.title,
      status: v.status,
      outcome: v.outcome,
    }))
    const epics: EpicSummary[] = data.epics.map((e) => ({
      id: e.id,
      title: e.title,
      status: e.status,
      outcome: e.outcome,
      versions: e.versions ?? [],
    }))
    const sprints: SprintSummary[] = data.sprints.map((s) => ({
      id: s.id,
      title: s.title,
      version: s.version,
      status: s.status,
      goal: s.goal,
      doneWhen: s.doneWhen,
      storyIds: s.stories ?? [],
    }))
    return { payload: { versions, epics, sprints, stories }, error: null }
  } catch (err) {
    const message = err instanceof Error ? err.message : String(err)
    return {
      payload: null,
      error: `SQLite export failed (${python}): ${message}`,
    }
  }
}