hyper/app/config.js
Guillermo Rauch aaed99abac Reorg (#386)
* Step 1: move electorn into `app/`.

This is to comply with the suggested directory format of
`electron-builder`: https://github.com/electron-userland/electron-builder#two-packagejson-structure

* Step 2: add build directory with icon files for mac / windows

* Step 3: move all development (web) assets into main directory

* Step 4: add `build` namespace to dev `package.json`

* Step 5: move all dev dependencies into dev file and get rid of
old electron packagers in favor of `eletorn-builder`

* Step 6: target build inside `app/` as everything else is excluded at build time

* Step 7: remove old stuff!

* Step 8: update README

* turn off asar for `child_pty`
2016-07-24 10:59:21 -07:00

86 lines
2.1 KiB
JavaScript

const { dialog } = require('electron');
const { homedir } = require('os');
const { resolve } = require('path');
const { readFileSync, writeFileSync } = require('fs');
const gaze = require('gaze');
const vm = require('vm');
const notify = require('./notify');
const path = resolve(homedir(), '.hyperterm.js');
const watchers = [];
let cfg = {};
function watch () {
gaze(path, function (err) {
if (err) throw err;
this.on('changed', () => {
try {
if (exec(readFileSync(path, 'utf8'))) {
notify('HyperTerm configuration reloaded!');
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) {
if (str === _str) return false;
_str = str;
const script = new vm.Script(str);
const module = {};
script.runInNewContext({ module });
if (!module.exports) {
throw new Error('Error reading configuration: `module.exports` not set');
}
const _cfg = module.exports;
if (!_cfg.config) {
throw new Error('Error reading configuration: `config` key is missing');
}
_cfg.plugins = _cfg.plugins || [];
_cfg.localPlugins = _cfg.localPlugins || [];
cfg = _cfg;
return true;
}
exports.subscribe = function (fn) {
watchers.push(fn);
return () => {
watchers.splice(watchers.indexOf(fn), 1);
};
};
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);
exec(defaultConfig);
writeFileSync(path, defaultConfig);
} catch (err) {
throw new Error(`Failed to write config to ${path}`);
}
}
watch();
};
exports.getConfig = function () {
return cfg.config;
};
exports.getPlugins = function () {
return {
plugins: cfg.plugins,
localPlugins: cfg.localPlugins
};
};