64 lines
1.7 KiB
JavaScript
64 lines
1.7 KiB
JavaScript
const path = require('path')
|
|
const chokidar = require('chokidar')
|
|
const YAML = require('yaml')
|
|
const fs = require('fs')
|
|
const loki = require('lokijs')
|
|
|
|
const { readSpecimens } = require('./specimen.js')
|
|
const { readGear } = require('./gear.js')
|
|
|
|
const { UnhandledTypeError } = require('./errors.js')
|
|
|
|
const refresh = async (plugin, app, db_dir) => {
|
|
let files = await fs.promises.readdir(path.join(db_dir))
|
|
|
|
let db = new loki('Hord')
|
|
let specimens = db.getCollection('specimens') || db.addCollection('specimens', {indices: ['family', 'name']})
|
|
let gear = db.getCollection('gear') || db.addCollection('gear', {indices: ['type', 'name']})
|
|
|
|
for (let file of files) {
|
|
if (path.extname(file) === '.aedifex' || path.extname(file) === '.aed') {
|
|
plugin.log(`parsing aedifex file: "${path.join(db_dir, file)}"`)
|
|
let o = YAML.parse(await fs.promises.readFile(path.join(db_dir, file), { encoding: 'utf8' }))
|
|
try {
|
|
switch(o.type) {
|
|
case 'hord-specimens':
|
|
readSpecimens(specimens, o)
|
|
break;
|
|
case 'hord-gear':
|
|
readGear(gear, o)
|
|
break;
|
|
default:
|
|
throw new UnhandledTypeError(o.type)
|
|
break;
|
|
}
|
|
} catch(err) {
|
|
plugin.error(err)
|
|
}
|
|
}
|
|
}
|
|
|
|
app.live = {
|
|
...app.live,
|
|
db: db,
|
|
}
|
|
}
|
|
|
|
module.exports = {
|
|
priority: 1,
|
|
load: async (plugin, app) => {
|
|
// Load up our DB.
|
|
const db_dir = path.resolve(app.config.aedifex)
|
|
|
|
await refresh(plugin, app, db_dir)
|
|
|
|
const watcher = chokidar.watch(db_dir, {persistent: true})
|
|
watcher.on('change', p => {
|
|
refresh(plugin, app, db_dir)
|
|
})
|
|
plugin.log('watching', db_dir)
|
|
|
|
app.express.use(require('./route.js')(plugin, app))
|
|
},
|
|
}
|