|
| 1 | +import { readFileSync } from "node:fs"; |
| 2 | +import { join } from "node:path"; |
| 3 | +import type { Command } from "commander"; |
| 4 | +import { readMountsJson } from "../doctor/mounts.js"; |
| 5 | + |
| 6 | +const TIP = |
| 7 | + "Tip: use 'codeforge mount add <path>' to register a directory for volume mounting."; |
| 8 | + |
| 9 | +interface ComposeVolume { |
| 10 | + name: string; |
| 11 | + mountPath: string; |
| 12 | +} |
| 13 | + |
| 14 | +function readComposeVolumes(workspaceRoot: string): ComposeVolume[] { |
| 15 | + const composePath = join( |
| 16 | + workspaceRoot, |
| 17 | + ".devcontainer", |
| 18 | + "docker-compose.yml", |
| 19 | + ); |
| 20 | + try { |
| 21 | + const content = readFileSync(composePath, "utf-8"); |
| 22 | + const volumes: ComposeVolume[] = []; |
| 23 | + |
| 24 | + // Match named volume mount lines: "- volume-name:/path" |
| 25 | + // Named volumes start with a letter (not . or / like bind mounts) |
| 26 | + const regex = /^\s+-\s+([a-zA-Z][a-zA-Z0-9_.-]*):(\/.+)$/gm; |
| 27 | + let match = regex.exec(content); |
| 28 | + while (match !== null) { |
| 29 | + volumes.push({ name: match[1], mountPath: match[2] }); |
| 30 | + match = regex.exec(content); |
| 31 | + } |
| 32 | + |
| 33 | + return volumes; |
| 34 | + } catch { |
| 35 | + return []; |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +interface DisplayRow { |
| 40 | + path: string; |
| 41 | + source: string; |
| 42 | + signal: string; |
| 43 | + added: string; |
| 44 | +} |
| 45 | + |
| 46 | +export function registerMountListCommand(parent: Command): void { |
| 47 | + parent |
| 48 | + .command("list") |
| 49 | + .description("List configured volume mount directories") |
| 50 | + .option("--format <format>", "Output format: text or json", "text") |
| 51 | + .action(async (options) => { |
| 52 | + const workspaceRoot = process.env.WORKSPACE_ROOT || "/workspaces"; |
| 53 | + const mountsPath = join(workspaceRoot, ".codeforge", "mounts.json"); |
| 54 | + const mounts = await readMountsJson(mountsPath); |
| 55 | + const composeVolumes = readComposeVolumes(workspaceRoot); |
| 56 | + |
| 57 | + if (options.format === "json") { |
| 58 | + const output = { |
| 59 | + ...mounts, |
| 60 | + composeVolumes: composeVolumes.map((v) => ({ |
| 61 | + path: v.mountPath, |
| 62 | + source: "compose" as const, |
| 63 | + volumeName: v.name, |
| 64 | + })), |
| 65 | + }; |
| 66 | + console.log(JSON.stringify(output, null, "\t")); |
| 67 | + console.log(""); |
| 68 | + console.log(TIP); |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + const rows: DisplayRow[] = [ |
| 73 | + ...composeVolumes.map((v) => ({ |
| 74 | + path: v.mountPath, |
| 75 | + source: "compose", |
| 76 | + signal: v.name, |
| 77 | + added: "—", |
| 78 | + })), |
| 79 | + ...mounts.volumes.map((v) => ({ |
| 80 | + path: v.path, |
| 81 | + source: v.source, |
| 82 | + signal: v.signal, |
| 83 | + added: v.added, |
| 84 | + })), |
| 85 | + ]; |
| 86 | + |
| 87 | + if (rows.length === 0) { |
| 88 | + console.log("No mounts configured."); |
| 89 | + console.log(""); |
| 90 | + console.log(TIP); |
| 91 | + return; |
| 92 | + } |
| 93 | + |
| 94 | + // Column widths — compute dynamically from data |
| 95 | + const headers = { |
| 96 | + path: "PATH", |
| 97 | + source: "SOURCE", |
| 98 | + signal: "SIGNAL", |
| 99 | + added: "ADDED", |
| 100 | + }; |
| 101 | + const widths = { |
| 102 | + path: Math.max( |
| 103 | + headers.path.length, |
| 104 | + ...rows.map((r) => r.path.length), |
| 105 | + ), |
| 106 | + source: Math.max( |
| 107 | + headers.source.length, |
| 108 | + ...rows.map((r) => r.source.length), |
| 109 | + ), |
| 110 | + signal: Math.max( |
| 111 | + headers.signal.length, |
| 112 | + ...rows.map((r) => r.signal.length), |
| 113 | + ), |
| 114 | + added: Math.max( |
| 115 | + headers.added.length, |
| 116 | + ...rows.map((r) => r.added.length), |
| 117 | + ), |
| 118 | + }; |
| 119 | + |
| 120 | + const row = (p: string, s: string, sig: string, a: string) => |
| 121 | + `${p.padEnd(widths.path)} ${s.padEnd(widths.source)} ${sig.padEnd(widths.signal)} ${a}`; |
| 122 | + |
| 123 | + console.log( |
| 124 | + row(headers.path, headers.source, headers.signal, headers.added), |
| 125 | + ); |
| 126 | + for (const r of rows) { |
| 127 | + console.log(row(r.path, r.source, r.signal, r.added)); |
| 128 | + } |
| 129 | + console.log(""); |
| 130 | + console.log(TIP); |
| 131 | + }); |
| 132 | +} |
0 commit comments