Skip to content

Commit

Permalink
Automatically update options in local configuration file
Browse files Browse the repository at this point in the history
  • Loading branch information
klaudiosinani committed Jan 11, 2019
1 parent d82b190 commit b5ed489
Showing 1 changed file with 38 additions and 7 deletions.
45 changes: 38 additions & 7 deletions src/config.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,54 @@
'use strict';
const {readFileSync} = require('fs');
const {ensureFileSync} = require('./util');
const fs = require('fs');
const defaultConfig = require('./configs');
const file = require('./file');

const {log} = console;

class Config {
constructor() {
this._default = Object.assign({}, defaultConfig);
}

get _local() {
try {
return JSON.parse(fs.readFileSync(file.localConfig, 'utf8'));
} catch (error) {
return log(error);
}
}

get configuration() {
this._ensureLocalConfig(file.localConfig);
return this._local;
}

get shortcutKeys() {
return this._load.shortcutKeys;
return this.configuration.shortcutKeys;
}

get _load() {
ensureFileSync(file.localConfig, JSON.stringify(defaultConfig, null, 4));
_updateConfig(data) {
const result = Object.assign({}, this._default);

Object.keys(data).forEach(type => {
result[type] = Object.assign({}, result[type], data[type]);
});

Object.keys(result).forEach(type => {
const [opts, defaultOpts] = [data[type], this._default[type]].map(Object.keys);
const deprecated = opts.filter(x => !defaultOpts.includes(x));
deprecated.forEach(x => delete result[type][x]);
});

return result;
}

_ensureLocalConfig(path) {
const data = fs.existsSync(path) ? this._updateConfig(this._local) : this._default;
try {
return JSON.parse(readFileSync(file.localConfig, 'utf8'));
fs.writeFileSync(path, JSON.stringify(data, null, 4));
} catch (error) {
return log(error);
log(error);
}
}
}
Expand Down

0 comments on commit b5ed489

Please sign in to comment.