-
-
Notifications
You must be signed in to change notification settings - Fork 435
/
Copy pathinstallable.ts
125 lines (118 loc) · 3.73 KB
/
installable.ts
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
import * as semver from 'semver';
import { ExecuteWithProgress } from './progressible';
import { naturalCompare } from '../utils';
import type { ArduinoComponent } from './arduino-component';
import type { MessageService } from '@theia/core/lib/common/message-service';
import type { ResponseServiceClient } from './response-service';
export interface Installable<T extends ArduinoComponent> {
/**
* If `options.version` is specified, that will be installed. Otherwise, `item.availableVersions[0]`.
*/
install(options: {
item: T;
progressId?: string;
version?: Installable.Version;
noOverwrite?: boolean;
}): Promise<void>;
/**
* Uninstalls the given component. It is a NOOP if not installed.
*/
uninstall(options: { item: T; progressId?: string }): Promise<void>;
}
export namespace Installable {
export type Version = string;
export namespace Version {
/**
* Most recent version comes first, then the previous versions. (`1.8.1`, `1.6.3`, `1.6.2`, `1.6.1` and so on.)
*
* If `coerce` is `true` tries to convert any invalid semver strings to a valid semver based on [these](https://github.com/npm/node-semver#coercion) rules.
*/
export const COMPARATOR = (
left: Version,
right: Version,
coerce = false
): number => {
const validLeft = semver.parse(left);
const validRight = semver.parse(right);
if (validLeft && validRight) {
return semver.compare(validLeft, validRight);
}
if (coerce) {
const coercedLeft = validLeft ?? semver.coerce(left);
const coercedRight = validRight ?? semver.coerce(right);
if (coercedLeft && coercedRight) {
return semver.compare(coercedLeft, coercedRight);
}
}
return naturalCompare(left, right);
};
}
export const Installed = <T extends ArduinoComponent>({
installedVersion,
}: T): boolean => {
return !!installedVersion;
};
export const Updateable = <T extends ArduinoComponent>(item: T): boolean => {
const { installedVersion } = item;
if (!installedVersion) {
return false;
}
const latestVersion = item.availableVersions[0];
if (!latestVersion) {
console.warn(
`Installed version ${installedVersion} is available for ${item.name}, but no available versions were available. Skipping.`
);
return false;
}
const result = Installable.Version.COMPARATOR(
latestVersion,
installedVersion,
true
);
return result > 0;
};
export async function installWithProgress<
T extends ArduinoComponent
>(options: {
installable: Installable<T>;
messageService: MessageService;
responseService: ResponseServiceClient;
item: T;
version: Installable.Version;
keepOutput?: boolean;
}): Promise<void> {
const { item, version } = options;
return ExecuteWithProgress.doWithProgress({
...options,
progressText: `Processing ${item.name}:${version}`,
run: ({ progressId }) =>
options.installable.install({
item: options.item,
version: options.version,
progressId,
}),
});
}
export async function uninstallWithProgress<
T extends ArduinoComponent
>(options: {
installable: Installable<T>;
messageService: MessageService;
responseService: ResponseServiceClient;
item: T;
keepOutput?: boolean;
}): Promise<void> {
const { item } = options;
return ExecuteWithProgress.doWithProgress({
...options,
progressText: `Processing ${item.name}${
item.installedVersion ? `:${item.installedVersion}` : ''
}`,
run: ({ progressId }) =>
options.installable.uninstall({
item: options.item,
progressId,
}),
});
}
}