-
-
Notifications
You must be signed in to change notification settings - Fork 435
/
Copy pathmonitor-settings-provider-impl.ts
134 lines (113 loc) · 3.91 KB
/
monitor-settings-provider-impl.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
import fs from 'node:fs';
import { join } from 'node:path';
import {
injectable,
inject,
postConstruct,
} from '@theia/core/shared/inversify';
import { EnvVariablesServer } from '@theia/core/lib/common/env-variables';
import { FileUri } from '@theia/core/lib/node/file-uri';
import { promisify } from 'util';
import { MonitorSettingsProvider } from './monitor-settings-provider';
import { Deferred } from '@theia/core/lib/common/promise-util';
import {
longestPrefixMatch,
reconcileSettings,
} from './monitor-settings-utils';
import { ILogger } from '@theia/core';
import { PluggableMonitorSettings } from '../../common/protocol';
const MONITOR_SETTINGS_FILE = 'pluggable-monitor-settings.json';
@injectable()
export class MonitorSettingsProviderImpl implements MonitorSettingsProvider {
@inject(EnvVariablesServer)
protected readonly envVariablesServer: EnvVariablesServer;
@inject(ILogger)
protected logger: ILogger;
// deferred used to guarantee file operations are performed after the service is initialized
protected ready = new Deferred<void>();
// this contains actual values coming from the stored file and edited by the user
// this is a map with MonitorId as key and PluggableMonitorSetting as value
private monitorSettings: Record<string, PluggableMonitorSettings>;
// this is the path to the pluggable monitor settings file, set during init
private pluggableMonitorSettingsPath: string;
@postConstruct()
protected init(): void {
(async () => {
// get the monitor settings file path
const configDirUri = await this.envVariablesServer.getConfigDirUri();
this.pluggableMonitorSettingsPath = join(
FileUri.fsPath(configDirUri),
MONITOR_SETTINGS_FILE
);
// read existing settings
await this.readSettingsFromFS();
// init is done, resolve the deferred and unblock any call that was waiting for it
this.ready.resolve();
})();
}
async getSettings(
monitorId: string,
defaultSettings: PluggableMonitorSettings
): Promise<PluggableMonitorSettings> {
// wait for the service to complete the init
await this.ready.promise;
const { matchingSettings } = this.longestPrefixMatch(monitorId);
this.monitorSettings[monitorId] = this.reconcileSettings(
matchingSettings,
defaultSettings
);
return this.monitorSettings[monitorId];
}
async setSettings(
monitorId: string,
settings: PluggableMonitorSettings
): Promise<PluggableMonitorSettings> {
// wait for the service to complete the init
await this.ready.promise;
const newSettings = this.reconcileSettings(
settings,
this.monitorSettings[monitorId] || {}
);
this.monitorSettings[monitorId] = newSettings;
await this.writeSettingsToFS();
return newSettings;
}
private reconcileSettings(
newSettings: PluggableMonitorSettings,
defaultSettings: PluggableMonitorSettings
): PluggableMonitorSettings {
return reconcileSettings(newSettings, defaultSettings);
}
private async readSettingsFromFS(): Promise<void> {
const rawJson = await promisify(fs.readFile)(
this.pluggableMonitorSettingsPath,
{
encoding: 'utf8',
flag: 'a+', // a+ = append and read, creating the file if it doesn't exist
}
);
if (!rawJson) {
this.monitorSettings = {};
}
try {
this.monitorSettings = JSON.parse(rawJson);
} catch (error) {
this.logger.error(
'Could not parse the pluggable monitor settings file. Using empty file.'
);
this.monitorSettings = {};
}
}
private async writeSettingsToFS(): Promise<void> {
await promisify(fs.writeFile)(
this.pluggableMonitorSettingsPath,
JSON.stringify(this.monitorSettings)
);
}
private longestPrefixMatch(id: string): {
matchingPrefix: string;
matchingSettings: PluggableMonitorSettings;
} {
return longestPrefixMatch(id, this.monitorSettings);
}
}