mirror of
https://github.com/quine-global/hyper.git
synced 2026-01-12 20:18:41 -09:00
simplify config
This commit is contained in:
parent
bff6909407
commit
b6990f7543
3 changed files with 43 additions and 24 deletions
|
|
@ -1,20 +0,0 @@
|
|||
const { resolve } = require('path');
|
||||
const { statSync, readFileSync, writeFileSync } = require('fs');
|
||||
const file = require('./config-path');
|
||||
|
||||
module.exports = function initConfig () {
|
||||
try {
|
||||
statSync(file);
|
||||
} catch (err) {
|
||||
console.log('stat error', file, err.message);
|
||||
const defaultConfig = readFileSync(resolve(__dirname, 'config-default.js'));
|
||||
try {
|
||||
console.log('attempting to write default config to', file);
|
||||
writeFileSync(file, defaultConfig);
|
||||
} catch (err) {
|
||||
throw new Error(`Failed to write config to ${file}`);
|
||||
}
|
||||
}
|
||||
|
||||
return file;
|
||||
};
|
||||
|
|
@ -1,4 +0,0 @@
|
|||
const { homedir } = require('os');
|
||||
const { resolve } = require('path');
|
||||
|
||||
module.exports = resolve(homedir(), '.hyperterm.js');
|
||||
43
config.js
Normal file
43
config.js
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
// 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);
|
||||
const mod = {};
|
||||
script.runInNewContext(mod);
|
||||
}
|
||||
|
||||
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;
|
||||
};
|
||||
Loading…
Reference in a new issue