forked from mirrors/probot
Use commander for CLI
This commit is contained in:
parent
6888f45ff3
commit
89e3310582
|
@ -0,0 +1,7 @@
|
|||
#!/usr/bin/env node
|
||||
|
||||
require('commander')
|
||||
.version(require('../package').version)
|
||||
.usage('<command> [options]')
|
||||
.command('run', 'run the bot')
|
||||
.parse(process.argv)
|
|
@ -0,0 +1,57 @@
|
|||
#!/usr/bin/env node
|
||||
|
||||
require('dotenv').config();
|
||||
|
||||
const program = require('commander');
|
||||
|
||||
program
|
||||
.usage('[options] <plugins...>')
|
||||
.option('-i, --integration <id>', 'ID of the GitHub Integration', process.env.INTEGRATION_ID)
|
||||
.option('-s, --secret <secret>', 'Webhook secret of the GitHub Integration', process.env.WEBHOOK_SECRET || 'development')
|
||||
.option('-p, --port <n>', 'Port to start the server on', process.env.PORT || 3000)
|
||||
.option('-c, --cert <file>', 'Path to certificate of the GitHub Integration', readCert, process.env.CERT)
|
||||
.parse(process.argv);
|
||||
|
||||
function readCert(path) {
|
||||
try {
|
||||
return require('fs').readFileSync(path);
|
||||
} catch (err) {
|
||||
console.warn(err.message);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
const pkgConf = require('pkg-conf');
|
||||
const resolve = require('resolve').sync;
|
||||
const createProbot = require('../');
|
||||
|
||||
if(!program.integration) {
|
||||
console.warn("Missing GitHub Integration ID.\nUse --integration flag or set INTEGRATION_ID environment variable.");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
if(!program.cert) {
|
||||
console.warn("Missing GitHub Integration certificate.\nUse --cert flag or set CERT environment variable.");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const probot = createProbot({
|
||||
id: program.integration,
|
||||
secret: program.secret,
|
||||
cert: program.cert,
|
||||
port: program.port
|
||||
});
|
||||
|
||||
pkgConf('probot').then(pkg => {
|
||||
program.args.concat(pkg.plugins || []).map(plugin => {
|
||||
try {
|
||||
const path = resolve(plugin, {basedir: process.cwd()});
|
||||
probot.load(require(path))
|
||||
} catch(err) {
|
||||
console.warn(err.message);
|
||||
process.exit(1);
|
||||
}
|
||||
});
|
||||
|
||||
probot.start();
|
||||
});
|
|
@ -1,29 +0,0 @@
|
|||
#!/usr/bin/env node
|
||||
|
||||
try {
|
||||
require('dotenv-safe').load();
|
||||
} catch (err) {
|
||||
console.log(err.message);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const resolve = require('resolve');
|
||||
const pkgConf = require('pkg-conf');
|
||||
const probot = require('../index.js');
|
||||
|
||||
function loadPlugins(plugins) {
|
||||
plugins.forEach(plugin => {
|
||||
resolve(plugin, {basedir: process.cwd()}, (err, path) => {
|
||||
if (err) {
|
||||
throw err;
|
||||
} else {
|
||||
probot.log.debug('loading plugin %s', path);
|
||||
require(path)(probot);
|
||||
return path;
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
loadPlugins(process.argv.slice(2));
|
||||
pkgConf('probot').then(pkg => loadPlugins(pkg.plugins || []));
|
32
index.js
32
index.js
|
@ -1,26 +1,18 @@
|
|||
const fs = require('fs');
|
||||
const createWebhook = require('github-webhook-handler');
|
||||
const createIntegration = require('github-integration');
|
||||
|
||||
const createRobot = require('./lib/robot');
|
||||
const createServer = require('./lib/server');
|
||||
|
||||
const WEBHOOK_SECRET = process.env.WEBHOOK_SECRET || 'development';
|
||||
const PORT = process.env.PORT || 3000;
|
||||
|
||||
const webhook = createWebhook({path: '/', secret: WEBHOOK_SECRET});
|
||||
module.exports = options => {
|
||||
const webhook = createWebhook({path: '/', secret: options.secret});
|
||||
const integration = createIntegration({
|
||||
id: process.env.INTEGRATION_ID,
|
||||
cert: process.env.PRIVATE_KEY || fs.readFileSync('private-key.pem'),
|
||||
id: options.id,
|
||||
cert: options.cert,
|
||||
debug: process.env.LOG_LEVEL === 'trace'
|
||||
});
|
||||
const server = createServer(webhook);
|
||||
const robot = createRobot(integration, webhook);
|
||||
|
||||
server.listen(PORT);
|
||||
|
||||
console.log('Listening on http://localhost:' + PORT);
|
||||
|
||||
// Show trace for any unhandled rejections
|
||||
process.on('unhandledRejection', reason => {
|
||||
robot.log.error(reason);
|
||||
|
@ -30,4 +22,18 @@ process.on('unhandledRejection', reason => {
|
|||
webhook.on('error', err => {
|
||||
robot.log.error(err);
|
||||
});
|
||||
module.exports = robot;
|
||||
|
||||
return {
|
||||
server,
|
||||
robot,
|
||||
|
||||
start() {
|
||||
server.listen(options.port);
|
||||
console.log('Listening on http://localhost:' + options.port);
|
||||
},
|
||||
|
||||
load(plugin) {
|
||||
plugin(robot);
|
||||
}
|
||||
};
|
||||
};
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
"repository": "https://github.com/probot/probot",
|
||||
"main": "index.js",
|
||||
"bin": {
|
||||
"probot": "./bin/probot.js"
|
||||
"probot": "./bin/probot"
|
||||
},
|
||||
"scripts": {
|
||||
"start": "probot",
|
||||
|
@ -15,7 +15,8 @@
|
|||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"bunyan": "^1.8.5",
|
||||
"dotenv-safe": "^4.0.3",
|
||||
"commander": "^2.9.0",
|
||||
"dotenv": "~4.0.0",
|
||||
"github-integration": "^1.0.0",
|
||||
"github-webhook-handler": "^0.6.0",
|
||||
"pkg-conf": "^2.0.0",
|
||||
|
|
Loading…
Reference in New Issue