-
-
Notifications
You must be signed in to change notification settings - Fork 435
/
Copy pathconfig-service.ts
143 lines (136 loc) · 3.59 KB
/
config-service.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import { RecursivePartial } from '@theia/core/lib/common/types';
export const ConfigServicePath = '/services/config-service';
export const ConfigService = Symbol('ConfigService');
export interface ConfigService {
getVersion(): Promise<
Readonly<{ version: string; commit: string; status?: string }>
>;
getCliConfigFileUri(): Promise<string>;
getConfiguration(): Promise<Config>;
setConfiguration(config: Config): Promise<void>;
isInDataDir(uri: string): Promise<boolean>;
isInSketchDir(uri: string): Promise<boolean>;
}
export interface Daemon {
readonly port: string | number;
}
export namespace Daemon {
export function is(
daemon: RecursivePartial<Daemon> | undefined
): daemon is Daemon {
return !!daemon && !!daemon.port;
}
export function sameAs(
left: RecursivePartial<Daemon> | undefined,
right: RecursivePartial<Daemon> | undefined
): boolean {
if (left === undefined) {
return right === undefined;
}
if (right === undefined) {
return left === undefined;
}
return String(left.port) === String(right.port);
}
}
export interface ProxySettings {
protocol: string;
hostname: string;
port: string;
username: string;
password: string;
}
export type Network = 'none' | ProxySettings;
export namespace Network {
export function Default(): Network {
return {
protocol: 'http',
hostname: '',
port: '',
username: '',
password: '',
};
}
export function parse(raw: string | undefined): Network {
if (!raw) {
return 'none';
}
try {
// Patter: PROTOCOL://USER:PASS@HOSTNAME:PORT/
const { protocol, hostname, password, username, port } = new URL(raw);
return {
protocol,
hostname,
password,
username,
port,
};
} catch {
return 'none';
}
}
export function stringify(network: Network): string | undefined {
if (network === 'none') {
return undefined;
}
const { protocol, hostname, password, username, port } = network;
try {
const defaultUrl = new URL(
`${protocol ? protocol : 'http'}://${hostname ? hostname : '_'}`
);
return Object.assign(defaultUrl, {
protocol,
hostname,
password,
username,
port,
}).toString();
} catch {
return undefined;
}
}
export function sameAs(left: Network, right: Network): boolean {
if (left === 'none') {
return right === 'none';
}
if (right === 'none') {
return false;
}
return (
left.hostname === right.hostname &&
left.password === right.password &&
left.protocol === right.protocol &&
left.username === right.username
);
}
}
export interface Config {
readonly locale: string;
readonly sketchDirUri: string;
readonly dataDirUri: string;
readonly downloadsDirUri: string;
readonly additionalUrls: string[];
readonly network: Network;
readonly daemon: Daemon;
}
export namespace Config {
export function sameAs(left: Config, right: Config): boolean {
const leftUrls = left.additionalUrls.sort();
const rightUrls = right.additionalUrls.sort();
if (leftUrls.length !== rightUrls.length) {
return false;
}
for (let i = 0; i < leftUrls.length; i++) {
if (leftUrls[i] !== rightUrls[i]) {
return false;
}
}
return (
left.locale === right.locale &&
left.dataDirUri === right.dataDirUri &&
left.downloadsDirUri === right.downloadsDirUri &&
left.sketchDirUri === right.sketchDirUri &&
Network.sameAs(left.network, right.network)
);
}
}