hot reloading config; hot loading plugins when config changes.

This commit is contained in:
Alberto Miranda
2017-02-05 13:15:20 -03:00
parent 086d8bf00a
commit 830312842b
2 changed files with 22 additions and 53 deletions

View File

@@ -106,9 +106,13 @@ const exports = module.exports = {
}
const configFile = appConfig.filePath
const config = getConfig()
let config = getConfig()
const watchers = []
function updateConfig () {
config = JSON.parse(fs.readFileSync(configFile))
}
function watch() {
gaze(configFile, function (err) {
if (err) {
@@ -116,15 +120,13 @@ function watch() {
}
this.on('changed', () => {
try {
if (exec(readFileSync(configFile, 'utf8'))) {
notify('WebTorrent configuration reloaded!')
watchers.forEach(fn => fn())
}
console.log('-- config updated: ', configFile)
updateConfig()
console.log('WebTorrent configuration reloaded!')
watchers.forEach(fn => fn())
} catch (err) {
dialog.showMessageBox({
message: `An error occurred loading your configuration (${configFile}): ${err.message}`,
buttons: ['Ok']
})
// TODO: display notification
console.log(`An error occurred loading your configuration (${configFile}): ${err.message}`)
}
})
this.on('error', () => {
@@ -133,27 +135,8 @@ function watch() {
})
}
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
}
// start watching for config changes
watch()
exports.subscribe = function (fn) {
watchers.push(fn)
@@ -162,9 +145,10 @@ exports.subscribe = function (fn) {
}
}
exports.getPlugins = function () {
function getPlugins () {
return config.plugins || {}
}
exports.getPlugins = getPlugins
exports.getConfigPath = getConfigPath
exports.getConfig = getConfig

View File

@@ -13,8 +13,6 @@ const config = require('./config')
module.exports = class Plugins {
constructor () {
console.log('-- constructing plugins')
// modules path
this.path = resolve(config.getConfigPath(), 'webtorrent-plugins')
console.log('- plugins path: ', this.path)
@@ -54,12 +52,15 @@ module.exports = class Plugins {
// we listen on configuration updates to trigger
// plugin installation
config.subscribe(() => {
console.log('--> CONFIG UPDATED, check if plugins update is needed')
const plugins = config.getPlugins()
if (plugins !== this.plugins) {
const id = this.getId(plugins)
if (this.id !== id) {
console.log('--> UPDATE PLUGINS')
this.id = id
this.plugins = plugins
this.paths = this.getPaths(this.plugins)
this.updatePlugins()
}
}
@@ -79,11 +80,6 @@ module.exports = class Plugins {
// otherwise update plugins every 5 hours
setInterval(this.updatePlugins, ms('5h'))
console.log(`
-- id: ${this.id}
-- installedPlugins: ${this.state.saved.installedPlugins})}
`)
}
didPluginsChange () {
@@ -106,7 +102,6 @@ module.exports = class Plugins {
const hash = crypto.createHash('sha256');
hash.update(JSON.stringify(plugins));
return hash.digest('hex')
// return JSON.stringify(plugins)
}
updatePlugins (forceUpdate = false) {
@@ -117,17 +112,8 @@ module.exports = class Plugins {
// return notify('Plugin update in progress')
}
this.updating = true
const hasPackages = this.syncPackageJSON()
// there are plugins loaded from repositories
// npm install must run for these ones
if (hasPackages) {
this.installPackages((err) => this.loadPlugins(err))
return
}
// only local plugins to be loaded
this.loadPlugins(null, true)
this.syncPackageJSON()
this.installPackages((err) => this.loadPlugins(err))
}
loadPlugins (err, localOnly = false) {
@@ -150,8 +136,8 @@ module.exports = class Plugins {
return
}
// cache paths
// this.paths = this.getPaths(this.plugins)
// update state with latest plugins
this.state.saved.plugins = this.plugins
// cache modules
this.modules = this.requirePlugins()
@@ -231,7 +217,6 @@ module.exports = class Plugins {
syncPackageJSON () {
console.log('- syncPackageJSON')
const dependencies = this.toDependencies(this.plugins)
if (this.isEmptyObject(dependencies)) return false
console.log('- set plugins package file')
const pkg = {