diff --git a/index.js b/index.js index 4a64dee..634429f 100644 --- a/index.js +++ b/index.js @@ -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) }