sc-manager/client/updater/updater.js

149 lines
4 KiB
JavaScript
Raw Normal View History

2020-09-08 07:53:42 +00:00
const m = require('mithril')
const socket = require('../socket')
const Module = require('../module')
const util = require('../util')
2020-09-08 07:53:42 +00:00
const Updater = Module({
init: function(vnode) {
this.activeApp = null
this.activeAppName = null
2020-09-08 07:53:42 +00:00
this.logUpdated = false
this._socketOn(() => this.socketOpen())
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) {
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() {
if (this.activeApp) {
socket.emit('core.listentoapp', { name: this.activeApp.name })
}
},
2020-09-08 07:53:42 +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
this.activeAppName = name
this.activeApp = null
2020-09-08 07:53:42 +00:00
if (!name) {
return
}
2020-09-08 07:53:42 +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
}
}
if (!this.activeApp) {
return
2020-09-08 07:53:42 +00:00
}
socket.emit('core.listentoapp', { name: this.activeApp.name })
2020-09-08 07:53:42 +00:00
},
remove: function() {
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', {
name: this.activeApp.name,
2020-09-08 07:53:42 +00:00
})
},
startSoftware: function() {
socket.emit('core.start', {
name: this.activeApp.name,
2020-09-08 07:53:42 +00:00
})
},
view: function() {
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', [
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
]),
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
}
},
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