-
Notifications
You must be signed in to change notification settings - Fork 169
Expand file tree
/
Copy pathagent.ts
More file actions
735 lines (668 loc) · 21.6 KB
/
agent.ts
File metadata and controls
735 lines (668 loc) · 21.6 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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
import fs from 'node:fs';
import fsPromises from 'node:fs/promises';
import path from 'node:path';
import { styleText } from 'node:util';
import * as prompts from '@voidzero-dev/vite-plus-prompts';
import { pkgRoot } from './path.ts';
// --- Interfaces ---
export interface McpConfigTarget {
/** Config file path relative to project root, e.g. ".claude/settings.json" */
filePath: string;
/** JSON key that holds MCP server entries, e.g. "mcpServers" or "servers" */
rootKey: string;
/** Extra fields merged into the server entry, e.g. { type: "stdio" } for VS Code */
extraFields?: Record<string, string>;
}
export interface AgentConfig {
displayName: string;
skillsDir: string;
detect: (root: string) => boolean;
/** Project-level config files where MCP server entries can be auto-written */
mcpConfig?: McpConfigTarget[];
/** Fallback hint printed when the agent has no project-level config support */
mcpHint?: string;
}
// --- Agent registry ---
const DEFAULT_MCP_HINT =
"Run `npx vp mcp` — this starts a stdio MCP server. See your agent's docs for how to add a local MCP server.";
const agents: Record<string, AgentConfig> = {
'claude-code': {
displayName: 'Claude Code',
skillsDir: '.claude/skills',
detect: (root) =>
fs.existsSync(path.join(root, '.claude')) || fs.existsSync(path.join(root, 'CLAUDE.md')),
mcpConfig: [
{ filePath: '.claude/settings.json', rootKey: 'mcpServers' },
{ filePath: '.claude/settings.local.json', rootKey: 'mcpServers' },
],
},
amp: {
displayName: 'Amp',
skillsDir: '.agents/skills',
detect: (root) => fs.existsSync(path.join(root, '.amp')),
mcpHint: DEFAULT_MCP_HINT,
},
codex: {
displayName: 'Codex',
skillsDir: '.agents/skills',
detect: (root) => fs.existsSync(path.join(root, '.codex')),
mcpHint: 'codex mcp add vite-plus -- npx vp mcp',
},
cursor: {
displayName: 'Cursor',
skillsDir: '.agents/skills',
detect: (root) => fs.existsSync(path.join(root, '.cursor')),
mcpConfig: [{ filePath: '.cursor/mcp.json', rootKey: 'mcpServers' }],
},
windsurf: {
displayName: 'Windsurf',
skillsDir: '.windsurf/skills',
detect: (root) => fs.existsSync(path.join(root, '.windsurf')),
mcpConfig: [{ filePath: '.windsurf/mcp.json', rootKey: 'mcpServers' }],
},
'gemini-cli': {
displayName: 'Gemini CLI',
skillsDir: '.agents/skills',
detect: (root) => fs.existsSync(path.join(root, '.gemini')),
mcpHint: 'gemini mcp add vite-plus -- npx vp mcp',
},
'github-copilot': {
displayName: 'GitHub Copilot',
skillsDir: '.agents/skills',
detect: (root) =>
fs.existsSync(path.join(root, '.github', 'copilot-instructions.md')) ||
fs.existsSync(path.join(root, '.vscode', 'mcp.json')),
mcpConfig: [
{ filePath: '.vscode/mcp.json', rootKey: 'servers', extraFields: { type: 'stdio' } },
],
},
cline: {
displayName: 'Cline',
skillsDir: '.cline/skills',
detect: (root) => fs.existsSync(path.join(root, '.cline')),
mcpHint: DEFAULT_MCP_HINT,
},
roo: {
displayName: 'Roo Code',
skillsDir: '.roo/skills',
detect: (root) => fs.existsSync(path.join(root, '.roo')),
mcpConfig: [{ filePath: '.roo/mcp.json', rootKey: 'mcpServers' }],
},
kilo: {
displayName: 'Kilo Code',
skillsDir: '.kilocode/skills',
detect: (root) => fs.existsSync(path.join(root, '.kilocode')),
mcpHint: DEFAULT_MCP_HINT,
},
continue: {
displayName: 'Continue',
skillsDir: '.continue/skills',
detect: (root) => fs.existsSync(path.join(root, '.continue')),
mcpHint: DEFAULT_MCP_HINT,
},
goose: {
displayName: 'Goose',
skillsDir: '.goose/skills',
detect: (root) => fs.existsSync(path.join(root, '.goose')),
mcpHint: DEFAULT_MCP_HINT,
},
opencode: {
displayName: 'OpenCode',
skillsDir: '.agents/skills',
detect: (root) => fs.existsSync(path.join(root, '.opencode')),
mcpHint: DEFAULT_MCP_HINT,
},
trae: {
displayName: 'Trae',
skillsDir: '.trae/skills',
detect: (root) => fs.existsSync(path.join(root, '.trae')),
mcpHint: DEFAULT_MCP_HINT,
},
junie: {
displayName: 'Junie',
skillsDir: '.junie/skills',
detect: (root) => fs.existsSync(path.join(root, '.junie')),
mcpHint: DEFAULT_MCP_HINT,
},
'kiro-cli': {
displayName: 'Kiro CLI',
skillsDir: '.kiro/skills',
detect: (root) => fs.existsSync(path.join(root, '.kiro')),
mcpHint: DEFAULT_MCP_HINT,
},
zencoder: {
displayName: 'Zencoder',
skillsDir: '.zencoder/skills',
detect: (root) => fs.existsSync(path.join(root, '.zencoder')),
mcpHint: DEFAULT_MCP_HINT,
},
'qwen-code': {
displayName: 'Qwen Code',
skillsDir: '.qwen/skills',
detect: (root) => fs.existsSync(path.join(root, '.qwen')),
mcpHint: DEFAULT_MCP_HINT,
},
};
// --- Registry functions ---
export function getAgentById(id: string): AgentConfig | undefined {
return agents[id];
}
export function detectAgents(root: string): AgentConfig[] {
return Object.values(agents).filter((a) => a.detect(root));
}
// --- Backward-compatible exports ---
export const AGENTS = [
{
id: 'agents',
label: 'AGENTS.md',
targetPath: 'AGENTS.md',
hint: 'Codex, Amp, OpenCode, and similar agents',
aliases: [
'agents.md',
'chatgpt',
'chatgpt-codex',
'codex',
'amp',
'kilo',
'kilo-code',
'kiro',
'kiro-cli',
'opencode',
'other',
],
},
{
id: 'claude',
label: 'CLAUDE.md',
targetPath: 'CLAUDE.md',
hint: 'Claude Code',
aliases: ['claude.md', 'claude-code'],
},
{
id: 'gemini',
label: 'GEMINI.md',
targetPath: 'GEMINI.md',
hint: 'Gemini CLI',
aliases: ['gemini.md', 'gemini-cli'],
},
{
id: 'copilot',
label: '.github/copilot-instructions.md',
targetPath: '.github/copilot-instructions.md',
hint: 'GitHub Copilot',
aliases: ['github-copilot', 'copilot-instructions.md'],
},
{
id: 'cursor',
label: '.cursor/rules/viteplus.mdc',
targetPath: '.cursor/rules/viteplus.mdc',
hint: 'Cursor',
aliases: ['viteplus.mdc'],
},
{
id: 'jetbrains',
label: '.aiassistant/rules/viteplus.md',
targetPath: '.aiassistant/rules/viteplus.md',
hint: 'JetBrains AI Assistant',
aliases: ['jetbrains', 'jetbrains-ai-assistant', 'aiassistant', 'viteplus.md'],
},
] as const;
type AgentSelection = string | string[] | false;
const AGENT_DEFAULT_ID = 'agents';
const AGENT_STANDARD_PATH = 'AGENTS.md';
const AGENT_INSTRUCTIONS_START_MARKER = '<!--VITE PLUS START-->';
const AGENT_INSTRUCTIONS_END_MARKER = '<!--VITE PLUS END-->';
const AGENT_ALIASES = Object.fromEntries(
AGENTS.flatMap((option) =>
(option.aliases ?? []).map((alias) => [normalizeAgentName(alias), option.id]),
),
) as Record<string, string>;
export async function selectAgentTargetPaths({
interactive,
agent,
onCancel,
}: {
interactive: boolean;
agent?: AgentSelection;
onCancel: () => void;
}) {
// Skip entirely if --no-agent is passed
if (agent === false) {
return undefined;
}
if (interactive && !agent) {
const selectedAgents = await prompts.multiselect({
message: 'Which coding agent instruction files should Vite+ create?',
options: AGENTS.map((option) => ({
label: option.label,
value: option.id,
hint: option.hint,
})),
initialValues: [AGENT_DEFAULT_ID],
required: false,
});
if (prompts.isCancel(selectedAgents)) {
onCancel();
return undefined;
}
if (selectedAgents.length === 0) {
return undefined;
}
return resolveAgentTargetPaths(selectedAgents);
}
return resolveAgentTargetPaths(agent ?? AGENT_DEFAULT_ID);
}
export async function selectAgentTargetPath({
interactive,
agent,
onCancel,
}: {
interactive: boolean;
agent?: AgentSelection;
onCancel: () => void;
}) {
const targetPaths = await selectAgentTargetPaths({ interactive, agent, onCancel });
return targetPaths?.[0];
}
export function detectExistingAgentTargetPaths(projectRoot: string) {
const detectedPaths: string[] = [];
const seenTargetPaths = new Set<string>();
for (const option of AGENTS) {
if (seenTargetPaths.has(option.targetPath)) {
continue;
}
seenTargetPaths.add(option.targetPath);
const targetPath = path.join(projectRoot, option.targetPath);
if (fs.existsSync(targetPath) && !fs.lstatSync(targetPath).isSymbolicLink()) {
detectedPaths.push(option.targetPath);
}
}
return detectedPaths.length > 0 ? detectedPaths : undefined;
}
export function detectExistingAgentTargetPath(projectRoot: string) {
return detectExistingAgentTargetPaths(projectRoot)?.[0];
}
export function hasExistingAgentInstructions(projectRoot: string): boolean {
const targetPaths = detectExistingAgentTargetPaths(projectRoot);
if (!targetPaths) {
return false;
}
for (const targetPath of targetPaths) {
const content = fs.readFileSync(path.join(projectRoot, targetPath), 'utf-8');
if (content.includes(AGENT_INSTRUCTIONS_START_MARKER)) {
return true;
}
}
return false;
}
/**
* Silently update agent instruction files that contain Vite+ markers.
* - No agent files → no writes
* - No Vite+ markers → no writes
* - Markers present, content up to date → no writes
* - Markers present, content outdated → update marked section
*/
export function updateExistingAgentInstructions(projectRoot: string): void {
const targetPaths = detectExistingAgentTargetPaths(projectRoot);
if (!targetPaths) {
return;
}
const templatePath = path.join(pkgRoot, 'AGENTS.md');
if (!fs.existsSync(templatePath)) {
return;
}
const templateContent = fs.readFileSync(templatePath, 'utf-8');
for (const targetPath of targetPaths) {
try {
const fullPath = path.join(projectRoot, targetPath);
const existing = fs.readFileSync(fullPath, 'utf-8');
const updated = replaceMarkedAgentInstructionsSection(existing, templateContent);
if (updated !== undefined && updated !== existing) {
fs.writeFileSync(fullPath, updated);
}
} catch {
// Best-effort: skip files that can't be read or written
}
}
}
export function resolveAgentTargetPaths(agent?: string | string[]) {
const agentNames = parseAgentNames(agent);
const resolvedAgentNames = agentNames.length > 0 ? agentNames : ['other'];
const dedupedTargetPaths: string[] = [];
const seenTargetPaths = new Set<string>();
for (const name of resolvedAgentNames) {
const targetPath = resolveSingleAgentTargetPath(name);
if (seenTargetPaths.has(targetPath)) {
continue;
}
seenTargetPaths.add(targetPath);
dedupedTargetPaths.push(targetPath);
}
return dedupedTargetPaths;
}
export function resolveAgentTargetPath(agent?: string) {
return resolveAgentTargetPaths(agent)[0] ?? 'AGENTS.md';
}
function parseAgentNames(agent?: string | string[]) {
if (!agent) {
return [];
}
const values = Array.isArray(agent) ? agent : [agent];
return values
.filter((value): value is string => typeof value === 'string')
.flatMap((value) => value.split(','))
.map((value) => value.trim())
.filter((value) => value.length > 0);
}
function resolveSingleAgentTargetPath(agent: string) {
const normalized = normalizeAgentName(agent);
const alias = AGENT_ALIASES[normalized];
const resolved = alias ? normalizeAgentName(alias) : normalized;
const match = AGENTS.find(
(option) =>
normalizeAgentName(option.id) === resolved ||
normalizeAgentName(option.label) === resolved ||
normalizeAgentName(option.targetPath) === resolved ||
option.aliases?.some((candidate) => normalizeAgentName(candidate) === resolved),
);
return match?.targetPath ?? AGENT_STANDARD_PATH;
}
export interface AgentConflictInfo {
targetPath: string;
}
/**
* Detect agent instruction files that would conflict (exist without markers).
* Returns only files that need a user decision (append or skip).
* Read-only — does not write or modify any files.
*/
export async function detectAgentConflicts({
projectRoot,
targetPaths,
}: {
projectRoot: string;
targetPaths?: string[];
}): Promise<AgentConflictInfo[]> {
if (!targetPaths || targetPaths.length === 0) {
return [];
}
const sourcePath = path.join(pkgRoot, 'AGENTS.md');
if (!fs.existsSync(sourcePath)) {
return [];
}
const incomingContent = await fsPromises.readFile(sourcePath, 'utf-8');
const shouldLinkToAgents = targetPaths.includes(AGENT_STANDARD_PATH);
const orderedPaths = shouldLinkToAgents
? [AGENT_STANDARD_PATH, ...targetPaths.filter((p) => p !== AGENT_STANDARD_PATH)]
: targetPaths;
const conflicts: AgentConflictInfo[] = [];
const seenDestinationPaths = new Set<string>();
const seenRealPaths = new Set<string>();
for (const targetPathToCheck of orderedPaths) {
const destinationPath = path.join(projectRoot, targetPathToCheck);
const destinationKey = path.resolve(destinationPath);
if (seenDestinationPaths.has(destinationKey)) {
continue;
}
seenDestinationPaths.add(destinationKey);
// If linking to AGENTS.md, non-AGENTS.md paths that are not regular files get linked
if (shouldLinkToAgents && targetPathToCheck !== AGENT_STANDARD_PATH) {
const existing = await getExistingPathKind(destinationPath);
if (existing !== 'file') {
continue;
}
}
if (fs.existsSync(destinationPath)) {
if (fs.lstatSync(destinationPath).isSymbolicLink()) {
continue;
}
const destinationRealPath = await fsPromises.realpath(destinationPath);
if (seenRealPaths.has(destinationRealPath)) {
continue;
}
const existingContent = await fsPromises.readFile(destinationPath, 'utf-8');
const updatedContent = replaceMarkedAgentInstructionsSection(
existingContent,
incomingContent,
);
if (updatedContent !== undefined) {
// Has markers — will auto-update, no conflict
seenRealPaths.add(destinationRealPath);
continue;
}
// Conflict — needs user decision
conflicts.push({ targetPath: targetPathToCheck });
seenRealPaths.add(destinationRealPath);
}
}
return conflicts;
}
export async function writeAgentInstructions({
projectRoot,
targetPath,
targetPaths,
interactive,
conflictDecisions,
silent = false,
}: {
projectRoot: string;
targetPath?: string;
targetPaths?: string[];
interactive: boolean;
conflictDecisions?: Map<string, 'append' | 'skip'>;
silent?: boolean;
}) {
const paths = [...(targetPaths ?? []), ...(targetPath ? [targetPath] : [])];
if (paths.length === 0) {
return;
}
const sourcePath = path.join(pkgRoot, 'AGENTS.md');
if (!fs.existsSync(sourcePath)) {
if (!silent) {
prompts.log.warn('Agent instructions template not found; skipping.');
}
return;
}
const seenDestinationPaths = new Set<string>();
const seenRealPaths = new Set<string>();
const incomingContent = await fsPromises.readFile(sourcePath, 'utf-8');
const shouldLinkToAgents = paths.includes(AGENT_STANDARD_PATH);
const orderedPaths = shouldLinkToAgents
? [AGENT_STANDARD_PATH, ...paths.filter((p) => p !== AGENT_STANDARD_PATH)]
: paths;
for (const targetPathToWrite of orderedPaths) {
const destinationPath = path.join(projectRoot, targetPathToWrite);
const destinationKey = path.resolve(destinationPath);
if (seenDestinationPaths.has(destinationKey)) {
continue;
}
seenDestinationPaths.add(destinationKey);
await fsPromises.mkdir(path.dirname(destinationPath), { recursive: true });
if (shouldLinkToAgents && targetPathToWrite !== AGENT_STANDARD_PATH) {
const linked = await tryLinkTargetToAgents(projectRoot, targetPathToWrite, silent);
if (linked) {
continue;
}
}
if (fs.existsSync(destinationPath)) {
if (fs.lstatSync(destinationPath).isSymbolicLink()) {
if (!silent) {
prompts.log.info(`Skipped writing ${targetPathToWrite} (symlink)`);
}
continue;
}
const destinationRealPath = await fsPromises.realpath(destinationPath);
if (seenRealPaths.has(destinationRealPath)) {
if (!silent) {
prompts.log.info(`Skipped writing ${targetPathToWrite} (duplicate target)`);
}
continue;
}
const existingContent = await fsPromises.readFile(destinationPath, 'utf-8');
const updatedContent = replaceMarkedAgentInstructionsSection(
existingContent,
incomingContent,
);
if (updatedContent !== undefined) {
if (updatedContent !== existingContent) {
await fsPromises.writeFile(destinationPath, updatedContent);
}
seenRealPaths.add(destinationRealPath);
continue;
}
// Determine conflict action from pre-resolved decisions, interactive prompt, or default
let conflictAction: 'append' | 'skip';
const preResolved = conflictDecisions?.get(targetPathToWrite);
if (preResolved) {
conflictAction = preResolved;
} else if (interactive) {
const action = await prompts.select({
message:
`Agent instructions already exist at ${targetPathToWrite}.\n ` +
styleText(
'gray',
'The Vite+ template includes guidance on `vp` commands, the build pipeline, and project conventions.',
),
options: [
{
label: 'Append',
value: 'append',
hint: 'Add template content to the end',
},
{
label: 'Skip',
value: 'skip',
hint: 'Leave existing file unchanged',
},
],
initialValue: 'skip',
});
conflictAction = prompts.isCancel(action) || action === 'skip' ? 'skip' : 'append';
} else {
conflictAction = 'skip';
}
if (conflictAction === 'append') {
await appendAgentContent(
destinationPath,
targetPathToWrite,
existingContent,
incomingContent,
silent,
);
} else {
const suffix = !preResolved && !interactive ? ' (already exists)' : '';
if (!silent) {
prompts.log.info(`Skipped writing ${targetPathToWrite}${suffix}`);
}
}
seenRealPaths.add(destinationRealPath);
continue;
}
await fsPromises.writeFile(destinationPath, incomingContent);
if (!silent) {
prompts.log.success(`Wrote agent instructions to ${targetPathToWrite}`);
}
seenRealPaths.add(await fsPromises.realpath(destinationPath));
}
}
async function appendAgentContent(
destinationPath: string,
targetPath: string,
existingContent: string,
incomingContent: string,
silent = false,
) {
const separator = existingContent.endsWith('\n') ? '' : '\n';
await fsPromises.appendFile(destinationPath, `${separator}\n${incomingContent}`);
if (!silent) {
prompts.log.success(`Appended agent instructions to ${targetPath}`);
}
}
function normalizeAgentName(value: string) {
return value
.trim()
.toLowerCase()
.replace(/[^a-z0-9]+/g, '');
}
export function replaceMarkedAgentInstructionsSection(existing: string, incoming: string) {
const existingRange = getMarkedRange(
existing,
AGENT_INSTRUCTIONS_START_MARKER,
AGENT_INSTRUCTIONS_END_MARKER,
);
if (!existingRange) {
return undefined;
}
const incomingRange = getMarkedRange(
incoming,
AGENT_INSTRUCTIONS_START_MARKER,
AGENT_INSTRUCTIONS_END_MARKER,
);
if (!incomingRange) {
return undefined;
}
return `${existing.slice(0, existingRange.start)}${incoming.slice(
incomingRange.start,
incomingRange.end,
)}${existing.slice(existingRange.end)}`;
}
async function tryLinkTargetToAgents(projectRoot: string, targetPath: string, silent = false) {
const destinationPath = path.join(projectRoot, targetPath);
const agentsPath = path.join(projectRoot, AGENT_STANDARD_PATH);
const symlinkTarget = path.relative(path.dirname(destinationPath), agentsPath);
const existing = await getExistingPathKind(destinationPath);
if (existing === 'file') {
return false;
}
if (existing === 'symlink') {
const currentLink = await fsPromises.readlink(destinationPath);
const resolvedCurrentLink = path.resolve(path.dirname(destinationPath), currentLink);
if (resolvedCurrentLink === agentsPath) {
if (!silent) {
prompts.log.info(
`Skipped linking ${targetPath} (already linked to ${AGENT_STANDARD_PATH})`,
);
}
return true;
}
await fsPromises.unlink(destinationPath);
}
try {
await fsPromises.symlink(symlinkTarget, destinationPath);
} catch (err: unknown) {
if ((err as NodeJS.ErrnoException).code === 'EPERM') {
// On Windows, symlinks require admin privileges.
// Fall back to copying the file instead.
await fsPromises.copyFile(agentsPath, destinationPath);
if (!silent) {
prompts.log.success(`Copied ${AGENT_STANDARD_PATH} to ${targetPath}`);
}
return true;
}
throw err;
}
if (!silent) {
prompts.log.success(`Linked ${targetPath} to ${AGENT_STANDARD_PATH}`);
}
return true;
}
async function getExistingPathKind(filePath: string) {
if (!fs.existsSync(filePath)) {
return 'missing' as const;
}
const stat = await fsPromises.lstat(filePath);
return stat.isSymbolicLink() ? ('symlink' as const) : ('file' as const);
}
function getMarkedRange(content: string, startMarker: string, endMarker: string) {
const start = content.indexOf(startMarker);
if (start === -1) {
return undefined;
}
const endMarkerIndex = content.indexOf(endMarker, start + startMarker.length);
if (endMarkerIndex === -1) {
return undefined;
}
return {
start,
end: endMarkerIndex + endMarker.length,
};
}