-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextensions-parameter.ts
More file actions
64 lines (56 loc) · 2.41 KB
/
Copy pathextensions-parameter.ts
File metadata and controls
64 lines (56 loc) · 2.41 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
import { ArrayParameterSetting, Plan, SpawnStatus, StatefulParameter, Utils, getPty } from '@codifycli/plugin-core';
import path from 'node:path';
import { VscodeConfig } from './vscode.js';
const VSCODE_APPLICATION_NAME = 'Visual Studio Code.app';
function getCodeBinary(directory?: string | null): string {
if (Utils.isMacOS()) {
// On macOS the code 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',
VSCODE_APPLICATION_NAME,
'Contents', 'Resources', 'app', 'bin', 'code',
);
}
// On Linux, the package manager installs code to /usr/bin/code (already on PATH).
return 'code';
}
export class ExtensionsParameter extends StatefulParameter<VscodeConfig, string[]> {
getSettings(): ArrayParameterSetting {
return {
type: 'array',
isElementEqual(desired, current) {
return desired.toLowerCase() === current.toLowerCase();
},
};
}
override async refresh(desired: string[] | null, config: Partial<VscodeConfig>): Promise<string[] | null> {
const $ = getPty();
const code = getCodeBinary(config.directory);
const result = await $.spawnSafe(`"${code}" --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<VscodeConfig>): Promise<void> {
const $ = getPty();
const code = getCodeBinary(plan.desiredConfig?.directory);
for (const ext of valueToAdd) {
await $.spawn(`"${code}" --install-extension ${ext} --force`, { interactive: true });
}
}
async modify(newValue: string[], previousValue: string[], plan: Plan<VscodeConfig>): 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<VscodeConfig>): Promise<void> {
const $ = getPty();
const code = getCodeBinary(plan.desiredConfig?.directory ?? plan.currentConfig?.directory);
for (const ext of valueToRemove) {
await $.spawnSafe(`"${code}" --uninstall-extension ${ext}`);
}
}
}