2015-04-08 22:54:59 +02:00
|
|
|
var Path = require('path')
|
|
|
|
|
|
|
|
function try_load(path) {
|
|
|
|
try {
|
|
|
|
return require(path)
|
|
|
|
} catch(err) {
|
|
|
|
if (err.code === 'MODULE_NOT_FOUND') {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
throw err
|
|
|
|
}
|
|
|
|
}
|
2015-02-24 20:28:16 +01:00
|
|
|
|
2015-04-08 22:54:59 +02:00
|
|
|
function load_plugins(config, plugin_configs, params, sanity_check) {
|
2015-02-24 20:28:16 +01:00
|
|
|
var plugins = Object.keys(plugin_configs || {}).map(function(p) {
|
2015-04-08 22:54:59 +02:00
|
|
|
var plugin
|
|
|
|
|
2016-05-01 11:02:01 +02:00
|
|
|
// try local plugins first
|
2016-05-09 20:05:28 +02:00
|
|
|
plugin = try_load(Path.resolve(__dirname + '/plugins', p))
|
2016-05-01 11:02:01 +02:00
|
|
|
|
2015-04-08 22:54:59 +02:00
|
|
|
// npm package
|
2016-05-01 11:02:01 +02:00
|
|
|
if (plugin === null && p.match(/^[^\.\/]/)) {
|
2015-04-08 22:54:59 +02:00
|
|
|
plugin = try_load('sinopia-' + p)
|
|
|
|
}
|
|
|
|
|
2016-05-01 11:02:01 +02:00
|
|
|
if (plugin === null) {
|
2015-04-08 22:54:59 +02:00
|
|
|
plugin = try_load(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
// relative to config path
|
2016-05-01 11:02:01 +02:00
|
|
|
if (plugin === null && p.match(/^\.\.?($|\/)/)) {
|
2015-04-08 22:54:59 +02:00
|
|
|
plugin = try_load(Path.resolve(Path.dirname(config.self_path), p))
|
2015-02-24 20:28:16 +01:00
|
|
|
}
|
|
|
|
|
2016-05-01 11:02:01 +02:00
|
|
|
if (plugin === null) {
|
|
|
|
throw Error('"' + p + '" plugin not found\ntry "npm install sinopia-' + p + '"')
|
2015-02-24 20:28:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof(plugin) !== 'function')
|
2015-04-08 22:54:59 +02:00
|
|
|
throw Error('"' + p + '" doesn\'t look like a valid plugin')
|
2015-02-24 20:28:16 +01:00
|
|
|
|
|
|
|
plugin = plugin(plugin_configs[p], params)
|
|
|
|
|
2016-05-01 11:02:01 +02:00
|
|
|
if (plugin === null || !sanity_check(plugin))
|
2015-04-08 22:54:59 +02:00
|
|
|
throw Error('"' + p + '" doesn\'t look like a valid plugin')
|
2015-02-24 20:28:16 +01:00
|
|
|
|
|
|
|
return plugin
|
|
|
|
})
|
|
|
|
|
|
|
|
return plugins
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.load_plugins = load_plugins;
|