-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextensions-parameter.ts
More file actions
63 lines (56 loc) · 2.5 KB
/
extensions-parameter.ts
File metadata and controls
63 lines (56 loc) · 2.5 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
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, use the full path to the AppImage/binary so it works before PATH is sourced.
return path.join(directory ?? CURSOR_LOCAL_BIN, 'cursor');
}
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 = getCursorBinary(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 = getCursorBinary(plan.desiredConfig?.directory);
for (const ext of valueToAdd) {
await $.spawn(`"${cursor}" --install-extension ${ext} --force`, { 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 = getCursorBinary(plan.desiredConfig?.directory ?? plan.currentConfig?.directory);
for (const ext of valueToRemove) {
await $.spawnSafe(`"${cursor}" --uninstall-extension ${ext}`);
}
}
}