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

View File

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