fixed initialization on first run

This commit is contained in:
Alberto Miranda
2017-02-01 14:35:28 -03:00
parent 50c100130a
commit 086d8bf00a
2 changed files with 32 additions and 8 deletions

View File

@@ -1,4 +1,5 @@
const appConfig = require('application-config')('WebTorrent') const appConfig = require('application-config')('WebTorrent')
const fs = require('fs')
const path = require('path') const path = require('path')
const electron = require('electron') const electron = require('electron')
const arch = require('arch') const arch = require('arch')
@@ -105,7 +106,7 @@ const exports = module.exports = {
} }
const configFile = appConfig.filePath const configFile = appConfig.filePath
const config = require(configFile) const config = getConfig()
const watchers = [] const watchers = []
function watch() { function watch() {
@@ -162,13 +163,19 @@ exports.subscribe = function (fn) {
} }
exports.getPlugins = function () { exports.getPlugins = function () {
return config.plugins return config.plugins || {}
} }
exports.getConfigPath = getConfigPath exports.getConfigPath = getConfigPath
exports.getConfig = getConfig
exports.getConfig = function () { function getConfig () {
return config const config = {}
try {
return require(configFile)
} catch (e) {
return config
}
} }
function getConfigPath () { function getConfigPath () {
@@ -214,8 +221,6 @@ function isPortable () {
return false return false
} }
const fs = require('fs')
try { try {
// This line throws if the "Portable Settings" folder does not exist, and does // This line throws if the "Portable Settings" folder does not exist, and does
// nothing otherwise. // nothing otherwise.

View File

@@ -69,7 +69,7 @@ module.exports = class Plugins {
// a bit after the user launches the terminal // a bit after the user launches the terminal
// to prevent slowness // to prevent slowness
// TODO: handle force updates // TODO: handle force updates
if (this.state.saved.installedPlugins !== this.id) { if (this.needsUpdate()) {
// install immediately if the user changed plugins // install immediately if the user changed plugins
console.log('plugins have changed / not init, scheduling plugins installation') console.log('plugins have changed / not init, scheduling plugins installation')
setTimeout(() => { 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) { getId (plugins) {
const hash = crypto.createHash('sha256'); const hash = crypto.createHash('sha256');
hash.update(JSON.stringify(plugins)); hash.update(JSON.stringify(plugins));
@@ -332,6 +348,7 @@ module.exports = class Plugins {
requirePlugins () { requirePlugins () {
console.log('- requirePlugins') console.log('- requirePlugins')
const {plugins} = this.paths const {plugins} = this.paths
let installNeeded = false
console.log('- requirePlugins: paths: ', plugins) console.log('- requirePlugins: paths: ', plugins)
const load = (path) => { const load = (path) => {
@@ -355,13 +372,15 @@ module.exports = class Plugins {
// plugin not installed // plugin not installed
// node_modules removed? did a manual plugin uninstall? // node_modules removed? did a manual plugin uninstall?
// try installing and then loading if successfull // try installing and then loading if successfull
this.installPackages((err) => this.loadPlugins(err)) installNeeded = true
// console.error(err) // console.error(err)
// this.alert(`Plugin error: Plugin "${basename(path)}" failed to load (${err.message})`) // this.alert(`Plugin error: Plugin "${basename(path)}" failed to load (${err.message})`)
} }
} }
if (installNeeded) this.updatePlugins()
return plugins.map(load) return plugins.map(load)
.filter(v => Boolean(v)) .filter(v => Boolean(v))
} }