|
| 1 | +import { Command } from "@cliffy/command" |
| 2 | +import { unicodeWidth } from "@std/cli" |
| 3 | +import { gql } from "../../__codegen__/gql.ts" |
| 4 | +import { getGraphQLClient } from "../../utils/graphql.ts" |
| 5 | +import { padDisplay } from "../../utils/display.ts" |
| 6 | +import { resolveProjectId } from "../../utils/linear.ts" |
| 7 | + |
| 8 | +const GetProjectMilestones = gql(` |
| 9 | + query GetProjectMilestones($projectId: String!) { |
| 10 | + project(id: $projectId) { |
| 11 | + id |
| 12 | + name |
| 13 | + projectMilestones { |
| 14 | + nodes { |
| 15 | + id |
| 16 | + name |
| 17 | + targetDate |
| 18 | + sortOrder |
| 19 | + project { |
| 20 | + id |
| 21 | + name |
| 22 | + } |
| 23 | + } |
| 24 | + } |
| 25 | + } |
| 26 | + } |
| 27 | +`) |
| 28 | + |
| 29 | +export const listCommand = new Command() |
| 30 | + .name("list") |
| 31 | + .description("List milestones for a project") |
| 32 | + .option("--project <projectId:string>", "Project ID", { required: true }) |
| 33 | + .action(async ({ project: projectIdOrSlug }) => { |
| 34 | + const { Spinner } = await import("@std/cli/unstable-spinner") |
| 35 | + const showSpinner = Deno.stdout.isTerminal() |
| 36 | + const spinner = showSpinner ? new Spinner() : null |
| 37 | + spinner?.start() |
| 38 | + |
| 39 | + try { |
| 40 | + // Resolve project slug to full UUID |
| 41 | + const projectId = await resolveProjectId(projectIdOrSlug) |
| 42 | + |
| 43 | + const client = getGraphQLClient() |
| 44 | + const result = await client.request(GetProjectMilestones, { |
| 45 | + projectId, |
| 46 | + }) |
| 47 | + spinner?.stop() |
| 48 | + |
| 49 | + const milestones = result.project?.projectMilestones?.nodes || [] |
| 50 | + |
| 51 | + if (milestones.length === 0) { |
| 52 | + console.log("No milestones found for this project.") |
| 53 | + return |
| 54 | + } |
| 55 | + |
| 56 | + // Sort milestones by targetDate (nulls last) then by name |
| 57 | + const sortedMilestones = milestones.sort((a, b) => { |
| 58 | + if (!a.targetDate && !b.targetDate) return a.name.localeCompare(b.name) |
| 59 | + if (!a.targetDate) return 1 |
| 60 | + if (!b.targetDate) return -1 |
| 61 | + const dateComparison = a.targetDate.localeCompare(b.targetDate) |
| 62 | + return dateComparison !== 0 |
| 63 | + ? dateComparison |
| 64 | + : a.name.localeCompare(b.name) |
| 65 | + }) |
| 66 | + |
| 67 | + // Calculate column widths |
| 68 | + const { columns } = Deno.stdout.isTerminal() |
| 69 | + ? Deno.consoleSize() |
| 70 | + : { columns: 120 } |
| 71 | + |
| 72 | + const ID_WIDTH = 36 // UUID format |
| 73 | + const TARGET_DATE_WIDTH = 12 // "YYYY-MM-DD" format or "No date" |
| 74 | + const PROJECT_WIDTH = Math.min( |
| 75 | + 30, |
| 76 | + Math.max( |
| 77 | + 7, // minimum width for "PROJECT" header |
| 78 | + ...sortedMilestones.map((m) => unicodeWidth(m.project.name)), |
| 79 | + ), |
| 80 | + ) |
| 81 | + |
| 82 | + const SPACE_WIDTH = 4 |
| 83 | + const fixed = ID_WIDTH + TARGET_DATE_WIDTH + PROJECT_WIDTH + SPACE_WIDTH |
| 84 | + const PADDING = 1 |
| 85 | + const maxNameWidth = Math.max( |
| 86 | + ...sortedMilestones.map((m) => unicodeWidth(m.name)), |
| 87 | + ) |
| 88 | + const availableWidth = Math.max(columns - PADDING - fixed, 0) |
| 89 | + const nameWidth = Math.min(maxNameWidth, availableWidth) |
| 90 | + |
| 91 | + // Print header |
| 92 | + const headerCells = [ |
| 93 | + padDisplay("NAME", nameWidth), |
| 94 | + padDisplay("ID", ID_WIDTH), |
| 95 | + padDisplay("TARGET DATE", TARGET_DATE_WIDTH), |
| 96 | + padDisplay("PROJECT", PROJECT_WIDTH), |
| 97 | + ] |
| 98 | + |
| 99 | + let headerMsg = "" |
| 100 | + const headerStyles: string[] = [] |
| 101 | + headerCells.forEach((cell, index) => { |
| 102 | + headerMsg += `%c${cell}` |
| 103 | + headerStyles.push("text-decoration: underline") |
| 104 | + if (index < headerCells.length - 1) { |
| 105 | + headerMsg += "%c %c" |
| 106 | + headerStyles.push("text-decoration: none") |
| 107 | + headerStyles.push("text-decoration: underline") |
| 108 | + } |
| 109 | + }) |
| 110 | + console.log(headerMsg, ...headerStyles) |
| 111 | + |
| 112 | + // Print each milestone |
| 113 | + for (const milestone of sortedMilestones) { |
| 114 | + const targetDate = milestone.targetDate || "No date" |
| 115 | + const projectName = milestone.project.name.length > PROJECT_WIDTH |
| 116 | + ? milestone.project.name.slice(0, PROJECT_WIDTH - 3) + "..." |
| 117 | + : padDisplay(milestone.project.name, PROJECT_WIDTH) |
| 118 | + |
| 119 | + const truncName = milestone.name.length > nameWidth |
| 120 | + ? milestone.name.slice(0, nameWidth - 3) + "..." |
| 121 | + : padDisplay(milestone.name, nameWidth) |
| 122 | + |
| 123 | + console.log( |
| 124 | + `${truncName} ${padDisplay(milestone.id, ID_WIDTH)} ${ |
| 125 | + padDisplay(targetDate, TARGET_DATE_WIDTH) |
| 126 | + } ${projectName}`, |
| 127 | + ) |
| 128 | + } |
| 129 | + } catch (error) { |
| 130 | + spinner?.stop() |
| 131 | + console.error("Failed to fetch milestones:", error) |
| 132 | + Deno.exit(1) |
| 133 | + } |
| 134 | + }) |
0 commit comments