UNPKG

1.04 kBPlain TextView Raw
1import { promisify } from 'util'
2import * as fs from 'fs';
3
4export async function updateConfig(newConfig, key: string)
5{
6 var config = await getConfig();
7 var keys = key.split('.');
8 keys.reduce(function (config, key, i)
9 {
10 if (keys.length == i + 1)
11 {
12 config[key] = newConfig;
13 console.log(config);
14 }
15 else if (typeof (config[key]) == 'undefined')
16 config[key] = {};
17
18 return config[key];
19 }, config);
20 writeConfig(config);
21}
22
23export function writeConfig(config)
24{
25 return promisify(fs.writeFile)('./config.json', JSON.stringify(config, null, 4), 'utf8').catch(function (err)
26 {
27 if (err)
28 console.error(err);
29 });
30}
31
32export function getConfig()
33{
34 return promisify(fs.readFile)('./config.json', 'utf8').then(function (content)
35 {
36 return JSON.parse(content);
37 }, function (err)
38 {
39 writeConfig({}).then(function (config)
40 {
41 return {};
42 })
43 });
44}
\No newline at end of file