-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode-versions-parameter.ts
More file actions
43 lines (35 loc) · 1.26 KB
/
node-versions-parameter.ts
File metadata and controls
43 lines (35 loc) · 1.26 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
import { ArrayParameterSetting, ArrayStatefulParameter, getPty, SpawnStatus } from '@codifycli/plugin-core';
import { FnmConfig } from './fast-node-manager.js';
export class FnmNodeVersionsParameter extends ArrayStatefulParameter<FnmConfig, string> {
getSettings(): ArrayParameterSetting {
return {
type: 'array',
isElementEqual: (desired, current) => current.includes(desired),
};
}
override async refresh(_desired: string[] | null): Promise<string[] | null> {
const $ = getPty();
const { data, status } = await $.spawnSafe('fnm list', { interactive: true });
if (status === SpawnStatus.ERROR) {
return null;
}
return parseInstalledVersions(data);
}
override async addItem(version: string): Promise<void> {
const $ = getPty();
await $.spawn(`fnm install ${version}`, { interactive: true });
}
override async removeItem(version: string): Promise<void> {
const $ = getPty();
await $.spawn(`fnm uninstall ${version}`, { interactive: true });
}
}
function parseInstalledVersions(output: string): string[] {
return output
.split('\n')
.map((line) => {
const match = line.match(/v?(\d+\.\d+\.\d+)/);
return match ? match[1] : null;
})
.filter((v): v is string => v !== null);
}