Installed plugins are automatically detected when the server starts. They are no more loaded from the config file.

Plugins (ie installed modules of which names start with `xo-server-`) are automatically detected and registered from `${__dirname}/node_modules/` and `/usr/local/lib/node_modules/`.
This commit is contained in:
Pierre
2015-12-04 17:24:02 +01:00
parent f5423bb314
commit f22ece403f
2 changed files with 49 additions and 36 deletions

View File

@@ -13,6 +13,7 @@ import pick from 'lodash.pick'
import proxyConsole from './proxy-console'
import proxyRequest from 'proxy-http-request'
import serveStatic from 'serve-static'
import startsWith from 'lodash.startswith'
import WebSocket from 'ws'
import {compile as compileJade} from 'jade'
@@ -24,7 +25,10 @@ import {
NotImplemented
} from './api-errors'
import JsonRpcPeer from 'json-rpc-peer'
import {readFile} from 'fs-promise'
import {
readFile,
readdir
} from 'fs-promise'
import * as apiMethods from './api/index'
import Api from './api'
@@ -204,19 +208,7 @@ async function setUpPassport (express, xo) {
// ===================================================================
const debugPlugin = createLogger('xo:plugin')
async function registerPlugin (pluginConf, pluginName) {
debugPlugin('register %s', pluginName)
const pluginPath = (function (name) {
try {
return require.resolve('xo-server-' + name)
} catch (e) {
return require.resolve(name)
}
})(pluginName)
async function registerPlugin (pluginPath, pluginName) {
const plugin = require(pluginPath)
// Supports both “normal” CommonJS and Babel's ES2015 modules.
@@ -234,25 +226,50 @@ async function registerPlugin (pluginConf, pluginName) {
await this._registerPlugin(
pluginName,
instance,
configurationSchema,
pluginConf
configurationSchema
)
}
function registerPlugins (plugins, xo) {
return Promise.all(mapToArray(plugins, (conf, name) => {
return registerPlugin.call(xo, conf, name).then(
() => {
debugPlugin(`successfully register ${name}`)
},
error => {
debugPlugin(`failed register ${name}`)
debugPlugin(error)
}
)
const debugPlugin = createLogger('xo:plugin')
function registerPluginWrapper (pluginPath, pluginName) {
debugPlugin('register %s', pluginName)
return registerPlugin.call(this, pluginPath, pluginName).then(
() => {
debugPlugin(`successfully register ${pluginName}`)
},
error => {
debugPlugin(`failed register ${pluginName}`)
debugPlugin(error)
}
)
}
const PLUGIN_PREFIX = 'xo-server-'
const PLUGIN_PREFIX_LENGTH = PLUGIN_PREFIX.length
async function registerPluginsInPath (path) {
const files = await readdir(path)
await Promise.all(mapToArray(files, name => {
if (startsWith(name, PLUGIN_PREFIX)) {
return registerPluginWrapper.call(
this,
`${path}/${name}`,
name.slice(PLUGIN_PREFIX_LENGTH)
)
}
}))
}
async function registerPlugins (xo) {
await Promise.all(mapToArray([
`${__dirname}/../node_modules/`,
'/usr/local/lib/node_modules/'
], registerPluginsInPath, xo))
}
// ===================================================================
async function makeWebServerListen (opts) {
@@ -631,9 +648,7 @@ export default async function main (args) {
setUpStaticFiles(express, config.http.mounts)
if (config.plugins) {
await registerPlugins(config.plugins, xo)
}
await registerPlugins(xo)
if (!(await xo._users.exists())) {
const email = 'admin@admin.net'

View File

@@ -1408,8 +1408,7 @@ export default class Xo extends EventEmitter {
async _registerPlugin (
name,
instance,
configurationSchema,
legacyConfiguration
configurationSchema
) {
const id = name
@@ -1424,7 +1423,7 @@ export default class Xo extends EventEmitter {
const metadata = await this._getPluginMetadata(id)
let autoload = true
let configuration = legacyConfiguration
let configuration
if (metadata) {
({
@@ -1432,11 +1431,10 @@ export default class Xo extends EventEmitter {
configuration
} = metadata)
} else {
console.log(`[NOTICE] migration config of ${name} plugin to database`)
console.log(`[NOTICE] register plugin ${name} for the first time`)
await this._pluginsMetadata.save({
id,
autoload,
configuration
autoload
})
}