srd/plugins/srd-gear-router/index.js

48 lines
995 B
JavaScript

const path = require('path')
const chokidar = require('chokidar')
const YAML = require('yaml')
const fs = require('fs')
const refresh = async (app, db_dir) => {
const gear_path = path.join(db_dir, 'gear.yaml')
let gear = {}
YAML.parse(await fs.promises.readFile(gear_path, { encoding: 'utf8' })).forEach((v,i) => {
let type = 'untyped'
if (v.type) {
type = v.type+'s'
}
if (!gear[type]) {
gear[type] = []
}
gear[type].push(v)
})
app.live = {
...app.live,
db: {
...app.live.db,
gear: gear,
},
}
}
module.exports = {
priority: 1,
load: async (plugin, app) => {
// Load up our DB.
const db_dir = path.resolve(path.join(app.config.srd, 'db'))
await refresh(app, db_dir)
const watcher = chokidar.watch(db_dir, {persistent: true})
watcher.on('change', p => {
refresh(app, db_dir)
})
plugin.log('watching', db_dir)
app.express.use(require('./route.js')(plugin, app))
},
}