-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathresources.ts
More file actions
120 lines (103 loc) · 3.4 KB
/
Copy pathresources.ts
File metadata and controls
120 lines (103 loc) · 3.4 KB
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
/**
* `agents resources` — show the merged DotAgents resource surface.
*/
import * as path from 'path';
import type { Command } from 'commander';
import chalk from 'chalk';
import { listResources, type ResolvedResource, type ResourceKind } from '../lib/resources.js';
import { terminalWidth, truncateToWidth, stringWidth } from '../lib/session/width.js';
const DRILLABLE_KINDS = [
'skills',
'commands',
'mcp',
'hooks',
'rules',
'plugins',
'workflows',
'subagents',
] as const;
type DrillableKind = typeof DRILLABLE_KINDS[number];
const KIND_LABELS: Record<DrillableKind, string> = {
skills: 'Skills',
commands: 'Commands',
mcp: 'MCP',
hooks: 'Hooks',
rules: 'Rules',
plugins: 'Plugins',
workflows: 'Workflows',
subagents: 'Subagents',
};
interface ResourceGroup {
kind: DrillableKind;
rows: ResolvedResource[];
}
interface ResourcesOptions {
merged?: boolean;
}
export function registerResourcesCommand(program: Command): void {
program
.command('resources')
.description('Show the merged DotAgents resources resolved across project, user, system, and extras')
.option('--merged', 'Show the merged first-wins resource surface (default)')
.addHelpText('after', `
Examples:
agents resources
agents resources --merged
`)
.action((options: ResourcesOptions) => {
void options;
renderMergedResources();
});
}
function renderMergedResources(): void {
const groups: ResourceGroup[] = DRILLABLE_KINDS.map((kind) => ({
kind,
rows: listResources(kind as ResourceKind),
}));
const total = groups.reduce((sum, group) => sum + group.rows.length, 0);
if (total === 0) {
console.log(chalk.gray('No merged resources found.'));
return;
}
console.log(chalk.bold(`Resources (${total} merged)`));
for (const group of groups) {
console.log();
console.log(chalk.bold(`${KIND_LABELS[group.kind]} (${group.rows.length})`));
if (group.rows.length === 0) {
console.log(chalk.gray(' none'));
continue;
}
for (const line of renderResourceRows(group.rows)) console.log(line);
}
}
function renderResourceRows(rows: ResolvedResource[]): string[] {
const nameW = Math.min(28, Math.max('Name'.length, ...rows.map((row) => stringWidth(row.name))));
const layerW = Math.min(16, Math.max('Layer'.length, ...rows.map((row) => stringWidth(row.source))));
const prefixW = 2 + nameW + 2 + layerW + 2;
const pathW = Math.max(12, terminalWidth() - prefixW);
const lines = [
` ${chalk.bold(pad(row('Name', nameW), nameW))} ${chalk.bold(pad(row('Layer', layerW), layerW))} ${chalk.bold('Path')}`,
` ${chalk.gray(`${'-'.repeat(nameW)} ${'-'.repeat(layerW)} ${'-'.repeat(Math.min(pathW, 40))}`)}`,
];
for (const resource of rows) {
lines.push(
` ${chalk.cyan(pad(truncateToWidth(resource.name, nameW), nameW))} ${pad(resource.source, layerW)} ${chalk.gray(truncateToWidth(formatPath(resource.path), pathW))}`,
);
}
return lines;
}
function row(value: string, width: number): string {
return truncateToWidth(value, width);
}
function pad(value: string, width: number): string {
return value + ' '.repeat(Math.max(0, width - stringWidth(value)));
}
function formatPath(value: string): string {
const home = process.env.HOME;
if (home) {
const rel = path.relative(home, value);
if (rel === '') return '~';
if (!rel.startsWith('..') && !path.isAbsolute(rel)) return `~/${rel}`;
}
return value;
}