UNPKG

2.02 kBJavaScriptView Raw
1"use strict";
2var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3 function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4 return new (P || (P = Promise))(function (resolve, reject) {
5 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7 function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8 step((generator = generator.apply(thisArg, _arguments || [])).next());
9 });
10};
11Object.defineProperty(exports, "__esModule", { value: true });
12const util_1 = require("util");
13const fs = require("fs");
14function updateConfig(newConfig, key) {
15 return __awaiter(this, void 0, void 0, function* () {
16 var config = yield getConfig();
17 var keys = key.split('.');
18 keys.reduce(function (config, key, i) {
19 if (keys.length == i + 1) {
20 config[key] = newConfig;
21 console.log(config);
22 }
23 else if (typeof (config[key]) == 'undefined')
24 config[key] = {};
25 return config[key];
26 }, config);
27 writeConfig(config);
28 });
29}
30exports.updateConfig = updateConfig;
31function writeConfig(config) {
32 return util_1.promisify(fs.writeFile)('./config.json', JSON.stringify(config, null, 4), 'utf8').catch(function (err) {
33 if (err)
34 console.error(err);
35 });
36}
37exports.writeConfig = writeConfig;
38function getConfig() {
39 return util_1.promisify(fs.readFile)('./config.json', 'utf8').then(function (content) {
40 return JSON.parse(content);
41 }, function (err) {
42 writeConfig({}).then(function (config) {
43 return {};
44 });
45 });
46}
47exports.getConfig = getConfig;
48//# sourceMappingURL=config.js.map
\No newline at end of file