Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/just-cameron/tangled-cli. Automation-first CLI for Tangled.org
Something went wrong. Try again.
2.5 kB · 84 lines
TypeScript
at main
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485/** * Format a date for display with human-readable relative time * @param dateString - ISO date string * @returns Human-readable date string */export function formatDate(dateString: string): string { const date = new Date(dateString); const now = new Date(); const diff = now.getTime() - date.getTime(); const days = Math.floor(diff / (1000 * 60 * 60 * 24));
if (days === 0) return 'today'; if (days === 1) return 'yesterday'; if (days < 7) return `${days} days ago`; if (days < 30) return `${Math.floor(days / 7)} weeks ago`; if (days < 365) return `${Math.floor(days / 30)} months ago`; return date.toLocaleDateString();}
/** * Format issue state for display * @param state - Issue state ('open', 'closed', or 'completed' for a ticket) * @returns Formatted state badge string */export function formatIssueState(state: 'open' | 'closed' | 'completed'): string { return `[${state.toUpperCase()}]`;}
/** * Pick specific fields from an object, omitting fields not present in the object */function pickFields(obj: Record<string, unknown>, fields: string[]): Record<string, unknown> { const result: Record<string, unknown> = {}; for (const field of fields) { if (field in obj) { result[field] = obj[field]; } } return result;}
/** * Output data as JSON to stdout, following GitHub CLI conventions. * * @param data - The data to output (object or array of objects) * @param fields - Comma-separated field names to include; omit for all fields */export function outputJson<T extends object>(data: T | T[], fields?: string): void { if (fields) { const fieldList = fields .split(',') .map((f) => f.trim()) .filter(Boolean); if (Array.isArray(data)) { console.log( JSON.stringify( data.map((item) => pickFields(item as Record<string, unknown>, fieldList)), null, 2 ) ); } else { const object = data as Record<string, unknown>; if (Array.isArray(object.items)) { console.log( JSON.stringify( { ...object, items: object.items.map((item) => pickFields(item as Record<string, unknown>, fieldList) ), }, null, 2 ) ); } else { console.log(JSON.stringify(pickFields(object, fieldList), null, 2)); } } } else { console.log(JSON.stringify(data, null, 2)); }}