2016-07-07 06:46:58 -08:00
|
|
|
// const { ipcMain } = require('electron');
|
|
|
|
|
const { homedir } = require('os');
|
|
|
|
|
const { resolve } = require('path');
|
|
|
|
|
const { readFileSync, writeFileSync } = require('fs');
|
|
|
|
|
const gaze = require('gaze');
|
|
|
|
|
const vm = require('vm');
|
|
|
|
|
|
|
|
|
|
const path = resolve(homedir(), '.hyperterm.js');
|
|
|
|
|
|
|
|
|
|
let cfg = {};
|
|
|
|
|
|
|
|
|
|
function watch () {
|
|
|
|
|
gaze(path, () => {
|
|
|
|
|
console.log('a change happened');
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function exec (str) {
|
|
|
|
|
const script = new vm.Script(str);
|
2016-07-07 06:50:18 -08:00
|
|
|
const module = {};
|
|
|
|
|
script.runInNewContext({ module });
|
|
|
|
|
const cfg = module.exports.config;
|
|
|
|
|
if (!module.exports) {
|
|
|
|
|
throw new Error('Error reading configuration: `module.exports` not set');
|
|
|
|
|
}
|
2016-07-07 06:49:11 -08:00
|
|
|
if (!cfg) {
|
|
|
|
|
throw new Error('Error reading configuration: `config` key is missing');
|
|
|
|
|
}
|
|
|
|
|
return cfg;
|
2016-07-07 06:46:58 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
exports.init = function () {
|
|
|
|
|
try {
|
|
|
|
|
exec(readFileSync(path, 'utf8'));
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.log('read error', path, err.message);
|
|
|
|
|
const defaultConfig = readFileSync(resolve(__dirname, 'config-default.js'));
|
|
|
|
|
try {
|
|
|
|
|
console.log('attempting to write default config to', path);
|
|
|
|
|
cfg = exec(defaultConfig);
|
|
|
|
|
writeFileSync(path, defaultConfig);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
throw new Error(`Failed to write config to ${path}`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
watch();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
exports.get = function () {
|
|
|
|
|
return cfg;
|
|
|
|
|
};
|