sc-manager/api/util.mjs

57 lines
1.4 KiB
JavaScript
Raw Normal View History

import defaults from './defaults.mjs'
2020-09-08 07:53:42 +00:00
export function safeWrap(log, name, fn) {
return function(data, cb) {
try {
let out = fn(data, cb)
if (out && out.then) {
out.then(function() {}, function(err) {
log.error(err, 'Unknown error in ' + name)
log.event.error('Unknown error occured in ' + name + ': ' + err.message)
})
}
} catch (err) {
log.error(err, 'Unknown error in ' + name)
log.event.error('Unknown error occured in ' + name + ': ' + err.message)
}
}
}
export function getConfig(ctx) {
let merge = {
applications: []
}
for (let app of ctx.core.applications) {
merge[app.name] = app.config
merge.applications.push(app.name)
}
return defaults(ctx.db.config, merge)
}
export function getStatus(ctx) {
let status = {}
for (let app of ctx.core.applications) {
if (app.provider.static) {
status[app.name] = {
active: 'static',
latestInstalled: 'static',
updated: '',
running: app.running,
updating: false,
}
} else {
let appDb = ctx.db.data.core[app.name]
let active = ctx.db.get(appDb.versions, appDb.active)
let installed = ctx.db.get(appDb.versions, appDb.latestInstalled)
status[app.name] = {
active: active,
latestInstalled: installed,
updated: appDb.updater,
running: app.running,
updating: app.updating,
}
}
}
return status
}