-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextensions-parameter.ts
More file actions
76 lines (68 loc) · 3.13 KB
/
extensions-parameter.ts
File metadata and controls
76 lines (68 loc) · 3.13 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
import { ArrayParameterSetting, Plan, SpawnStatus, StatefulParameter, Utils, getPty } from '@codifycli/plugin-core';
import os from 'node:os';
import path from 'node:path';
import { CURSOR_APPLICATION_NAME, CURSOR_LOCAL_BIN, CursorConfig } from './cursor.js';
function getCursorBinary(directory?: string | null): string {
if (Utils.isMacOS()) {
// On macOS the cursor binary lives inside the app bundle. Use the full path so it
// works immediately after install without requiring a new shell session.
return path.join(
directory ?? '/Applications',
CURSOR_APPLICATION_NAME,
'Contents', 'Resources', 'app', 'bin', 'cursor',
);
}
// On Linux, prefer the directory-scoped path (AppImage install), but fall back to
// the system PATH location (apt/dnf install puts it at /usr/bin/cursor).
return path.join(directory ?? CURSOR_LOCAL_BIN, 'cursor');
}
async function resolveCursorBinary(directory?: string | null): Promise<string> {
if (Utils.isMacOS()) return getCursorBinary(directory);
const candidate = getCursorBinary(directory);
const $ = getPty();
const check = await $.spawnSafe(`test -x "${candidate}"`);
if (check.status === SpawnStatus.SUCCESS) return candidate;
// Fall back to whatever is on PATH (e.g. /usr/bin/cursor from apt install)
const which = await $.spawnSafe('which cursor');
if (which.status === SpawnStatus.SUCCESS) return which.data.trim();
return candidate;
}
export class ExtensionsParameter extends StatefulParameter<CursorConfig, string[]> {
getSettings(): ArrayParameterSetting {
return {
type: 'array',
isElementEqual(desired, current) {
return desired.toLowerCase() === current.toLowerCase();
},
};
}
override async refresh(desired: string[] | null, config: Partial<CursorConfig>): Promise<string[] | null> {
const $ = getPty();
const cursor = await resolveCursorBinary(config.directory);
const result = await $.spawnSafe(`"${cursor}" --list-extensions`);
if (result.status !== SpawnStatus.SUCCESS || result.data == null) {
return null;
}
return result.data.split('\n').filter(Boolean);
}
async add(valueToAdd: string[], plan: Plan<CursorConfig>): Promise<void> {
const $ = getPty();
const cursor = await resolveCursorBinary(plan.desiredConfig?.directory);
for (const ext of valueToAdd) {
await $.spawn(`"${cursor}" --install-extension ${ext}`, { interactive: true });
}
}
async modify(newValue: string[], previousValue: string[], plan: Plan<CursorConfig>): Promise<void> {
const toAdd = newValue.filter((n) => !previousValue.some((p) => p.toLowerCase() === n.toLowerCase()));
const toRemove = previousValue.filter((p) => !newValue.some((n) => n.toLowerCase() === p.toLowerCase()));
await this.remove(toRemove, plan);
await this.add(toAdd, plan);
}
async remove(valueToRemove: string[], plan: Plan<CursorConfig>): Promise<void> {
const $ = getPty();
const cursor = await resolveCursorBinary(plan.desiredConfig?.directory ?? plan.currentConfig?.directory);
for (const ext of valueToRemove) {
await $.spawnSafe(`"${cursor}" --uninstall-extension ${ext}`);
}
}
}