Use commander for CLI

This commit is contained in:
Brandon Keepers 2017-03-17 21:04:23 -07:00
parent 6888f45ff3
commit 89e3310582
No known key found for this signature in database
GPG Key ID: F9533396D5FACBF6
5 changed files with 96 additions and 54 deletions

7
bin/probot Executable file
View File

@ -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)

57
bin/probot-run Executable file
View File

@ -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();
});

View File

@ -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 || []));

View File

@ -1,33 +1,39 @@
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;
module.exports = options => {
const webhook = createWebhook({path: '/', secret: options.secret});
const integration = createIntegration({
id: options.id,
cert: options.cert,
debug: process.env.LOG_LEVEL === 'trace'
});
const server = createServer(webhook);
const robot = createRobot(integration, webhook);
const webhook = createWebhook({path: '/', secret: WEBHOOK_SECRET});
const integration = createIntegration({
id: process.env.INTEGRATION_ID,
cert: process.env.PRIVATE_KEY || fs.readFileSync('private-key.pem'),
debug: process.env.LOG_LEVEL === 'trace'
});
const server = createServer(webhook);
const robot = createRobot(integration, webhook);
// Show trace for any unhandled rejections
process.on('unhandledRejection', reason => {
robot.log.error(reason);
});
server.listen(PORT);
// Handle case when webhook creation fails
webhook.on('error', err => {
robot.log.error(err);
});
console.log('Listening on http://localhost:' + PORT);
return {
server,
robot,
// Show trace for any unhandled rejections
process.on('unhandledRejection', reason => {
robot.log.error(reason);
});
start() {
server.listen(options.port);
console.log('Listening on http://localhost:' + options.port);
},
// Handle case when webhook creation fails
webhook.on('error', err => {
robot.log.error(err);
});
module.exports = robot;
load(plugin) {
plugin(robot);
}
};
};

View File

@ -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",