sc-manager/client/updater/updater.js

149 lines
4.0 KiB
JavaScript

const m = require('mithril')
const socket = require('../socket')
const Module = require('../module')
const util = require('../util')
const Updater = Module({
init: function(vnode) {
this.activeApp = null
this.activeAppName = null
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)
}
}
},
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)
}
},
socketOpen: function() {
if (this.activeApp) {
socket.emit('core.listentoapp', { name: this.activeApp.name })
}
},
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 })
}
this.activeAppName = name
this.activeApp = null
if (!name) {
return
}
for (let app of this.core.apps) {
if (app.name === this.activeAppName) {
this.activeApp = app
break
}
}
if (!this.activeApp) {
return
}
socket.emit('core.listentoapp', { name: this.activeApp.name })
},
remove: function() {
if (this.activeApp) {
socket.emit('core.unlistentoapp', { name: this.activeApp.name })
}
},
startUpdate: function() {
socket.emit('core.update', {
name: this.activeApp.name,
})
},
startSoftware: function() {
socket.emit('core.start', {
name: this.activeApp.name,
})
},
view: function() {
let apps = this.core.apps.filter(function(app) {
return Boolean(app.config.url)
})
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)
}),
]),
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
}
}
},
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
])
}
})
module.exports = Updater