Implement plugin object support; pass dot to app

We now support plugins that are more than functions. At the moment these
plugin modules should export a plain object with the following possible
properties:

  * priority number
    * preload/load priority. Higher numbers result in earlier calling.
  * preload function
    * Any logic for preloading, just as dynamic/initial setup that may be used by other plugins.
  * load
    * Any logic for setting up the bulk of the plugin, such as express paths and similar.
master
kts of kettek (nyaa) 2021-05-28 21:42:24 -07:00
parent 6d6d0379b9
commit e22e606d2d
1 changed files with 52 additions and 4 deletions

View File

@ -24,13 +24,14 @@ const app = {
menu: '',
},
express: exp,
dot: null,
}
const pub = __dirname + '/public'
const views = __dirname + '/views'
const routes = __dirname + '/routes'
const dot = doT.process({'path': views})
app.dot = doT.process({'path': views})
try {
app.config = {...app.config, ...require(__dirname + '/config.js')}
@ -43,7 +44,7 @@ try {
exp.use(express.static(pub))
exp.use(express.static(path.join(__dirname, app.config.srd)))
exp.engine('dot', (template, options, cb) => {
return cb(null, dot[path.parse(template).name](options))
return cb(null, app.dot[path.parse(template).name](options))
})
exp.set('views', views)
@ -55,7 +56,7 @@ try {
fs.readdirSync('plugins').forEach(f => {
let plugin_path = path.resolve('plugins', f)
if (fs.lstatSync(plugin_path).isDirectory()) {
console.log('plugin preload:', f)
console.log('plugin require:', f)
try {
let plugin = {
defaults: null,
@ -87,6 +88,47 @@ try {
})
if (plugins.length === 0) {
console.warn('no plugins found');
} else {
// Sort our plugins according to desired priorities. Higher numbers
plugins.sort((first, second) => {
let first_priority = 0
let second_priority = 0
if (first.config.priority !== undefined) {
first_priority = first.config.priority
} else if (first.module === Object(first.module)) {
if (first.module.priority !== undefined) {
first_priority = first.module.priority
}
}
if (second.config.priority !== undefined) {
second_priority = second.config.priority
} else if (second.module === Object(second.module)) {
if (second.module.priority !== undefined) {
second_priority = second.module.priority
}
}
if (first_priority > second_priority) {
return -1
} else if (first_priority < second_priority) {
return 1
}
return 0
})
// Call any needed preloads.
for (let plugin of plugins) {
// Call preload if the module is an object.
if (plugin.module === Object(plugin.module)) {
if (plugin.module.preload) {
console.log('plugin preload:', f)
try {
plugin.module.preload(plugin, app)
console.log('...ok')
} catch(err) {
console.error('...fail: ', err)
}
}
}
}
}
} catch(err) {
if (err.code === 'ENOENT') {
@ -120,7 +162,13 @@ const server = exp.listen(port, async () => {
for (let plugin of plugins) {
try {
await plugin.module(plugin, app)
if (plugin.module instanceof Function) {
await plugin.module(plugin, app)
} else if (plugin.module === Object(plugin.module)) {
if (plugin.module.load) {
await plugin.module.load(plugin, app)
}
}
} catch(err) {
console.error('plugin', err)
}