2020-09-08 07:53:42 +00:00
|
|
|
const m = require('mithril')
|
|
|
|
const socket = require('../socket')
|
|
|
|
const Module = require('../module')
|
2022-03-29 10:25:25 +00:00
|
|
|
const util = require('../util')
|
2020-09-08 07:53:42 +00:00
|
|
|
|
|
|
|
const Updater = Module({
|
|
|
|
init: function(vnode) {
|
2022-03-29 10:25:25 +00:00
|
|
|
this.activeApp = null
|
|
|
|
this.activeAppName = null
|
2020-09-08 07:53:42 +00:00
|
|
|
this.logUpdated = false
|
|
|
|
this._socketOn(() => this.socketOpen())
|
|
|
|
|
2022-03-29 10:25:25 +00:00
|
|
|
this.on('app.updatelog', (res) => {
|
|
|
|
if (!this.activeApp || this.activeApp.name !== res.name) return
|
|
|
|
this.activeApp.log = res.log.replace(/\. /g, '.\n')
|
|
|
|
this.logUpdated = true
|
|
|
|
m.redraw()
|
|
|
|
})
|
|
|
|
|
|
|
|
if (this.core.apps.length && vnode.attrs.id) {
|
|
|
|
this.updateActiveApp(vnode.attrs.id)
|
|
|
|
} else if (this.core.apps.length) {
|
|
|
|
let apps = this.core.apps.filter(function(app) {
|
|
|
|
return Boolean(app.config.url)
|
|
|
|
})
|
|
|
|
if (apps.length === 1) {
|
|
|
|
return m.route.set('/updater/' + apps[0].name)
|
|
|
|
}
|
2020-09-08 07:53:42 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
onupdate: function(vnode) {
|
2022-03-29 10:25:25 +00:00
|
|
|
if (this.core.apps.length) {
|
|
|
|
if (!vnode.attrs.id) {
|
|
|
|
let apps = this.core.apps.filter(function(app) {
|
|
|
|
return Boolean(app.config.url)
|
|
|
|
})
|
|
|
|
if (apps.length === 1) {
|
|
|
|
return m.route.set('/updater/' + apps[0].name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.updateActiveApp(vnode.attrs.id)
|
2020-09-08 07:53:42 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
socketOpen: function() {
|
2022-03-29 10:25:25 +00:00
|
|
|
if (this.activeApp) {
|
|
|
|
socket.emit('core.listentoapp', { name: this.activeApp.name })
|
|
|
|
}
|
|
|
|
},
|
2020-09-08 07:53:42 +00:00
|
|
|
|
2022-03-29 10:25:25 +00:00
|
|
|
updateActiveApp(orgName) {
|
|
|
|
let name = orgName || null
|
|
|
|
if (this.activeAppName === name && (!name || this.activeApp)) return
|
|
|
|
if (this.activeAppName !== name && this.activeAppName) {
|
|
|
|
socket.emit('core.unlistentoapp', { name: this.activeAppName })
|
|
|
|
}
|
2020-09-08 07:53:42 +00:00
|
|
|
|
2022-03-29 10:25:25 +00:00
|
|
|
this.activeAppName = name
|
|
|
|
this.activeApp = null
|
2020-09-08 07:53:42 +00:00
|
|
|
|
2022-03-29 10:25:25 +00:00
|
|
|
if (!name) {
|
|
|
|
return
|
|
|
|
}
|
2020-09-08 07:53:42 +00:00
|
|
|
|
2022-03-29 10:25:25 +00:00
|
|
|
for (let app of this.core.apps) {
|
|
|
|
if (app.name === this.activeAppName) {
|
|
|
|
this.activeApp = app
|
|
|
|
break
|
2020-09-08 07:53:42 +00:00
|
|
|
}
|
|
|
|
}
|
2022-03-29 10:25:25 +00:00
|
|
|
if (!this.activeApp) {
|
|
|
|
return
|
2020-09-08 07:53:42 +00:00
|
|
|
}
|
2022-03-29 10:25:25 +00:00
|
|
|
socket.emit('core.listentoapp', { name: this.activeApp.name })
|
2020-09-08 07:53:42 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
remove: function() {
|
2022-03-29 10:25:25 +00:00
|
|
|
if (this.activeApp) {
|
|
|
|
socket.emit('core.unlistentoapp', { name: this.activeApp.name })
|
|
|
|
}
|
2020-09-08 07:53:42 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
startUpdate: function() {
|
|
|
|
socket.emit('core.update', {
|
2022-03-29 10:25:25 +00:00
|
|
|
name: this.activeApp.name,
|
2020-09-08 07:53:42 +00:00
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
startSoftware: function() {
|
|
|
|
socket.emit('core.start', {
|
2022-03-29 10:25:25 +00:00
|
|
|
name: this.activeApp.name,
|
2020-09-08 07:53:42 +00:00
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
view: function() {
|
2022-03-29 10:25:25 +00:00
|
|
|
let apps = this.core.apps.filter(function(app) {
|
|
|
|
return Boolean(app.config.url)
|
|
|
|
})
|
2020-09-08 07:53:42 +00:00
|
|
|
return m('div#update', [
|
|
|
|
m('div.actions', [
|
2022-03-29 10:25:25 +00:00
|
|
|
apps.map((app) => {
|
|
|
|
return m(m.route.Link, {
|
|
|
|
class: 'button ' + (this.activeAppName === app.name ? 'active' : 'inactive'),
|
|
|
|
href: '/updater/' + app.name,
|
|
|
|
}, app.name)
|
|
|
|
}),
|
2020-09-08 07:53:42 +00:00
|
|
|
]),
|
2022-03-29 10:25:25 +00:00
|
|
|
this.activeApp ? (() => {
|
|
|
|
let appStatus = this.core.status[this.activeApp.name] || {}
|
|
|
|
console.log(appStatus)
|
|
|
|
|
|
|
|
return [
|
|
|
|
m('div.info', [
|
|
|
|
m('p', util.getRepoMessage(this.activeApp)),
|
|
|
|
m('p', 'Active version: ' + util.getVersionSummary(appStatus.active, appStatus)),
|
|
|
|
m('p', appStatus.updating
|
|
|
|
? 'Updating...'
|
|
|
|
: appStatus.running ? 'Running ' + appStatus.running : 'Not running'),
|
|
|
|
m('p', 'Latest installed: ' + util.getVersionSummary(appStatus.latestInstalled, appStatus)),
|
|
|
|
]),
|
|
|
|
m('div.console', {
|
|
|
|
onupdate: (vnode) => {
|
|
|
|
if (this.logUpdated) {
|
|
|
|
vnode.dom.scrollTop = vnode.dom.scrollHeight
|
|
|
|
this.logUpdated = false
|
|
|
|
}
|
2020-09-08 07:53:42 +00:00
|
|
|
}
|
2022-03-29 10:25:25 +00:00
|
|
|
},
|
|
|
|
m('pre', this.activeApp.log || '')
|
|
|
|
),
|
|
|
|
m('div.actions', {
|
|
|
|
hidden: appStatus.updating,
|
|
|
|
}, [
|
|
|
|
m('button', {
|
|
|
|
onclick: () => this.startSoftware(),
|
|
|
|
}, appStatus.running ? 'Restart' : 'Start'),
|
|
|
|
m('button', {
|
|
|
|
onclick: () => this.startUpdate(),
|
|
|
|
}, 'Check for updates'),
|
|
|
|
]),
|
|
|
|
]
|
|
|
|
})() : null
|
2020-09-08 07:53:42 +00:00
|
|
|
])
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
module.exports = Updater
|