/
GlobalSettings.ts
69 lines (59 loc) · 1.88 KB
/
GlobalSettings.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
import { CSpellSettings, CSpellSettingsWithSourceTrace } from '@cspell/cspell-types';
import ConfigStore from 'configstore';
import { logError } from '../util/logger';
import { isErrnoException } from '../util/errors';
import { format } from 'util';
const packageName = 'cspell';
export interface GlobalSettingsWithSource extends Partial<GlobalCSpellSettings> {
source: CSpellSettingsWithSourceTrace['source'];
}
export interface GlobalCSpellSettings {
import: CSpellSettings['import'];
}
export function getRawGlobalSettings(): GlobalSettingsWithSource {
const name = 'CSpell Configstore';
const globalConf: GlobalSettingsWithSource = {
source: {
name,
filename: undefined,
},
};
try {
const cfgStore = new ConfigStore(packageName);
const cfg = cfgStore.all;
// Only populate globalConf is there are values.
if (cfg && Object.keys(cfg).length) {
Object.assign(globalConf, cfg);
globalConf.source = {
name,
filename: cfgStore.path,
};
}
} catch (error) {
if (
!isErrnoException(error) ||
!error.code ||
!['ENOENT', 'EACCES', 'ENOTDIR', 'EISDIR'].includes(error.code)
) {
logError(error);
}
}
return globalConf;
}
export function writeRawGlobalSettings(settings: GlobalCSpellSettings): Error | undefined {
const toWrite: GlobalCSpellSettings = {
import: settings.import,
};
try {
const cfgStore = new ConfigStore(packageName);
cfgStore.set(toWrite);
return undefined;
} catch (error) {
if (error instanceof Error) return error;
return new Error(format(error));
}
}
export function getGlobalConfigPath(): string {
const cfgStore = new ConfigStore(packageName);
return cfgStore.path;
}