From 086d8bf00a66588bbea7742a612240dbba8a8032 Mon Sep 17 00:00:00 2001 From: Alberto Miranda Date: Wed, 1 Feb 2017 14:35:28 -0300 Subject: [PATCH] fixed initialization on first run --- src/config.js | 17 +++++++++++------ src/plugins.js | 23 +++++++++++++++++++++-- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/src/config.js b/src/config.js index cfd0d266..4b622fce 100644 --- a/src/config.js +++ b/src/config.js @@ -1,4 +1,5 @@ const appConfig = require('application-config')('WebTorrent') +const fs = require('fs') const path = require('path') const electron = require('electron') const arch = require('arch') @@ -105,7 +106,7 @@ const exports = module.exports = { } const configFile = appConfig.filePath -const config = require(configFile) +const config = getConfig() const watchers = [] function watch() { @@ -162,13 +163,19 @@ exports.subscribe = function (fn) { } exports.getPlugins = function () { - return config.plugins + return config.plugins || {} } exports.getConfigPath = getConfigPath +exports.getConfig = getConfig -exports.getConfig = function () { - return config +function getConfig () { + const config = {} + try { + return require(configFile) + } catch (e) { + return config + } } function getConfigPath () { @@ -214,8 +221,6 @@ function isPortable () { return false } - const fs = require('fs') - try { // This line throws if the "Portable Settings" folder does not exist, and does // nothing otherwise. diff --git a/src/plugins.js b/src/plugins.js index bbefcfe3..c75eeb64 100644 --- a/src/plugins.js +++ b/src/plugins.js @@ -69,7 +69,7 @@ module.exports = class Plugins { // a bit after the user launches the terminal // to prevent slowness // TODO: handle force updates - if (this.state.saved.installedPlugins !== this.id) { + if (this.needsUpdate()) { // install immediately if the user changed plugins console.log('plugins have changed / not init, scheduling plugins installation') setTimeout(() => { @@ -86,6 +86,22 @@ module.exports = class Plugins { `) } + didPluginsChange () { + return this.state.saved.installedPlugins !== this.id + } + + hasPlugins () { + return !this.isEmptyObject(this.plugins) + } + + isFirstInstall () { + return (!this.state.saved.installedPlugins && this.hasPlugins()) + } + + needsUpdate () { + return (this.didPluginsChange() || this.isFirstInstall()) + } + getId (plugins) { const hash = crypto.createHash('sha256'); hash.update(JSON.stringify(plugins)); @@ -332,6 +348,7 @@ module.exports = class Plugins { requirePlugins () { console.log('- requirePlugins') const {plugins} = this.paths + let installNeeded = false console.log('- requirePlugins: paths: ', plugins) const load = (path) => { @@ -355,13 +372,15 @@ module.exports = class Plugins { // plugin not installed // node_modules removed? did a manual plugin uninstall? // try installing and then loading if successfull - this.installPackages((err) => this.loadPlugins(err)) + installNeeded = true // console.error(err) // this.alert(`Plugin error: Plugin "${basename(path)}" failed to load (${err.message})`) } } + if (installNeeded) this.updatePlugins() + return plugins.map(load) .filter(v => Boolean(v)) }