在花了一段时间追求脚本选项之后,我意识到我应该利用 Node 模块系统和 NPM 来为我处理所有这些。
我最终删除了脚本,而不是框架从插件目录加载插件,它现在期望在实例化时传递一组插件来加载,这最终使整个事情变得非常简单。
框架/服务器
/**
* Configures the Node.js Server.
* @type {*}
*/
var _ = require('underscore'),
express = require('express'),
app = express(),
port = process.env.PORT || 3003;
module.exports = {
/**
* Starts the connector multi server.
* @param {Object} options A hash of options to configure the multi server.
* @param {Array} options.connectors An array of multi server connectors to load.
*/
start: function (options) {
// Sanitize the options
options = options || {connectors: []};
// Configure express
console.log('Configuring express...');
require('./config/config.js')(app, express);
require('./config/http-error.js');
// Load controllers
console.log('Loading controllers...');
require('./controllers/appsecute.js')(app);
require('./controllers/oauth2-client.js')(app);
// Load connectors
console.log('Loading connectors...');
_.each(options.connectors, function (connector) {
connector(app);
});
// Connectors have been loaded, start the multi server
console.log('Finished loading connectors.');
console.log('Starting multi connector server...');
var express_app = app.listen(port);
console.log("Multi connector server listening on port %d", port);
return express_app;
}
};
将框架 + 插件拉到一起进行部署的部署存储库
/**
* A basic wrapper around the multi server that includes specific connector implementations ready for deployment.
*
* To add a new connector to the deployment:
* 1. Add it as a dependency in package.json
* 2. Pass it to the multi connector start() call.
*/
require('appsecute-connector-multi').server.start({
connectors: [
require('appsecute-connector-multi-heroku'),
require('appsecute-connector-multi-tender'),
require('appsecute-connector-multi-circleci'),
require('appsecute-connector-multi-zendesk'),
require('appsecute-connector-multi-github'),
require('appsecute-connector-multi-travisci')
]
});
这样做的好处是除了简单之外,因为框架和所有单独的插件都被指定为部署项目中 package.json 中的依赖项,NPM 负责拉下所有共同依赖项,正如您所期望的那样:)