-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathconfig.ts
More file actions
65 lines (52 loc) · 1.95 KB
/
Copy pathconfig.ts
File metadata and controls
65 lines (52 loc) · 1.95 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import { existsSync, readFileSync, writeFileSync } from 'fs'
import { FAILSAFE_SCHEMA, dump, load } from 'js-yaml'
import PACKAGE_JSON from '../package.json'
import { getPath } from './path'
export const SUPPORTED_LEVELS = ['critical', 'error', 'warn', 'info', 'verbose', 'debug']
export const DEFAULT_LOG_LEVEL = 'info'
const DESKTOP_VERSION_FILE = 'desktop.version'
export const logLevel =
process.env.LOG_LEVEL && SUPPORTED_LEVELS.includes(process.env.LOG_LEVEL) ? process.env.LOG_LEVEL : DEFAULT_LOG_LEVEL
export function configYamlExists(): boolean {
return existsSync(getPath('config.yaml'))
}
export function readConfigYaml(fileName = 'config.yaml'): Record<string, unknown> {
const raw = readFileSync(getPath(fileName), 'utf-8')
const data = load(raw, {
schema: FAILSAFE_SCHEMA,
})
return data as Record<string, unknown>
}
export function writeConfigYaml(newValues: Record<string, unknown>, fileName = 'config.yaml') {
const data = readConfigYaml(fileName)
for (const [key, value] of Object.entries(newValues)) {
data[key] = value
}
writeFileSync(getPath(fileName), dump(data))
}
export function deleteKeyFromConfigYaml(key: string, fileName = 'config.yaml') {
const data = readConfigYaml(fileName)
delete data[key]
writeFileSync(getPath(fileName), dump(data))
}
export function getDesktopVersionFromFile(): string | undefined {
try {
const desktopFile = readFileSync(getPath(DESKTOP_VERSION_FILE))
return desktopFile.toString('utf-8')
} catch (e) {
return
}
}
export function writeDesktopVersionFile() {
writeFileSync(getPath(DESKTOP_VERSION_FILE), PACKAGE_JSON.version)
}
export function readWalletPasswordOrThrow(): string {
if (!configYamlExists()) {
throw Error('Attempted to read password, but config.yaml is not found')
}
const config = readConfigYaml()
if (!config.password) {
throw Error('Attempted to read password, but config.yaml does not contain it')
}
return config.password as string
}