mirror of
https://github.com/quine-global/hyper.git
synced 2026-01-16 21:58:39 -09:00
config: added ability to subscribe to changes
This commit is contained in:
parent
318f684da4
commit
24f1ae657e
1 changed files with 31 additions and 7 deletions
38
config.js
38
config.js
|
|
@ -1,4 +1,4 @@
|
||||||
// const { ipcMain } = require('electron');
|
const { dialog } = require('electron');
|
||||||
const { homedir } = require('os');
|
const { homedir } = require('os');
|
||||||
const { resolve } = require('path');
|
const { resolve } = require('path');
|
||||||
const { readFileSync, writeFileSync } = require('fs');
|
const { readFileSync, writeFileSync } = require('fs');
|
||||||
|
|
@ -6,29 +6,53 @@ const gaze = require('gaze');
|
||||||
const vm = require('vm');
|
const vm = require('vm');
|
||||||
|
|
||||||
const path = resolve(homedir(), '.hyperterm.js');
|
const path = resolve(homedir(), '.hyperterm.js');
|
||||||
|
const watchers = [];
|
||||||
|
|
||||||
let cfg = {};
|
let cfg = {};
|
||||||
|
|
||||||
function watch () {
|
function watch () {
|
||||||
gaze(path, () => {
|
gaze(path, function (err) {
|
||||||
console.log('a change happened');
|
if (err) throw err;
|
||||||
|
this.on('changed', () => {
|
||||||
|
try {
|
||||||
|
if (exec(readFileSync(path, 'utf8'))) {
|
||||||
|
watchers.forEach((fn) => fn());
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
dialog.showMessageBox({
|
||||||
|
message: `An error occurred loading your configuration (${path}): ${err.message}`,
|
||||||
|
buttons: ['Ok']
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let _str; // last script
|
||||||
function exec (str) {
|
function exec (str) {
|
||||||
|
if (str === _str) return false;
|
||||||
|
_str = str;
|
||||||
const script = new vm.Script(str);
|
const script = new vm.Script(str);
|
||||||
const module = {};
|
const module = {};
|
||||||
script.runInNewContext({ module });
|
script.runInNewContext({ module });
|
||||||
const cfg = module.exports.config;
|
|
||||||
if (!module.exports) {
|
if (!module.exports) {
|
||||||
throw new Error('Error reading configuration: `module.exports` not set');
|
throw new Error('Error reading configuration: `module.exports` not set');
|
||||||
}
|
}
|
||||||
if (!cfg) {
|
const _cfg = module.exports.config;
|
||||||
|
if (!_cfg) {
|
||||||
throw new Error('Error reading configuration: `config` key is missing');
|
throw new Error('Error reading configuration: `config` key is missing');
|
||||||
}
|
}
|
||||||
return cfg;
|
cfg = _cfg;
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
exports.subscribe = function (fn) {
|
||||||
|
watchers.push(fn);
|
||||||
|
return () => {
|
||||||
|
watchers.splice(watchers.indexOf(fn), 1);
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
exports.init = function () {
|
exports.init = function () {
|
||||||
try {
|
try {
|
||||||
exec(readFileSync(path, 'utf8'));
|
exec(readFileSync(path, 'utf8'));
|
||||||
|
|
@ -37,7 +61,7 @@ exports.init = function () {
|
||||||
const defaultConfig = readFileSync(resolve(__dirname, 'config-default.js'));
|
const defaultConfig = readFileSync(resolve(__dirname, 'config-default.js'));
|
||||||
try {
|
try {
|
||||||
console.log('attempting to write default config to', path);
|
console.log('attempting to write default config to', path);
|
||||||
cfg = exec(defaultConfig);
|
exec(defaultConfig);
|
||||||
writeFileSync(path, defaultConfig);
|
writeFileSync(path, defaultConfig);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
throw new Error(`Failed to write config to ${path}`);
|
throw new Error(`Failed to write config to ${path}`);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue