refactor: move probot commands into `src/bin/` folder

This commit is contained in:
Gregor Martynus 2020-11-18 15:59:04 -08:00
parent 74f1f16bcd
commit 3f0974b4d2
6 changed files with 104 additions and 100 deletions

View File

@ -1,70 +1,3 @@
#!/usr/bin/env node
// Usage: probot receive -e push -p path/to/payload app.js
require("dotenv").config();
const path = require("path");
const uuid = require("uuid");
const program = require("commander");
const { getPrivateKey } = require("@probot/get-private-key");
const {
logWarningsForObsoleteEnvironmentVariables,
} = require("../lib/helpers/log-warnings-for-obsolete-environment-variables");
const { Probot } = require("../");
logWarningsForObsoleteEnvironmentVariables();
program
.usage("[options] [path/to/app.js...]")
.option(
"-e, --event <event-name>",
"Event name",
process.env.GITHUB_EVENT_NAME
)
.option(
"-p, --payload-path <payload-path>",
"Path to the event payload",
process.env.GITHUB_EVENT_PATH
)
.option(
"-t, --token <access-token>",
"Access token",
process.env.GITHUB_TOKEN
)
.option("-a, --app <id>", "ID of the GitHub App", process.env.APP_ID)
.option(
"-P, --private-key <file>",
"Path to certificate of the GitHub App",
getPrivateKey
)
.parse(process.argv);
const githubToken = program.token;
if (!program.event || !program.payloadPath) {
program.help();
}
const privateKey = getPrivateKey();
if (!githubToken && (!program.app || !privateKey)) {
console.warn(
"No token specified and no certificate found, which means you will not be able to do authenticated requests to GitHub"
);
}
const payload = require(path.resolve(program.payloadPath));
const probot = new Probot({
id: program.app,
privateKey,
githubToken: githubToken,
});
probot.setup(program.args);
probot.logger.debug("Receiving event", program.event);
probot.receive({ name: program.event, payload, id: uuid.v4() }).catch(() => {
// Process must exist non-zero to indicate that the action failed to run
process.exit(1);
});
require("../lib/bin/probot-receive");

View File

@ -1,5 +1,3 @@
#!/usr/bin/env node
const { Probot } = require('../')
Probot.run(process.argv)
require("../lib/bin/probot-run");

View File

@ -1,31 +1,3 @@
#!/usr/bin/env node
const semver = require('semver')
const program = require('commander')
const pkg = require('../package')
const updateNotifier = require('update-notifier')
if (!semver.satisfies(process.version, pkg.engines.node)) {
console.log(`Node.js version ${pkg.engines.node} is required. You have ${process.version}.`)
process.exit(1)
}
if (pkg.version !== '0.0.0-development') {
updateNotifier({pkg}).notify()
}
program
.version(pkg.version)
.usage('<command> [options]')
.command('run', 'run the bot')
.command('receive', 'Receive a single event and payload')
.on('command:*', (cmd) => {
if (!program.commands.find(c => c._name == cmd[0])) {
console.error(`Invalid command: ${program.args.join(' ')}\n`)
program.outputHelp()
process.exit(1)
}
})
program
.parse(process.argv)
require("../lib/bin/probot");

67
src/bin/probot-receive.ts Normal file
View File

@ -0,0 +1,67 @@
// Usage: probot receive -e push -p path/to/payload app.js
require("dotenv").config();
import path from "path";
import { v4 as uuidv4 } from "uuid";
import program from "commander";
import { getPrivateKey } from "@probot/get-private-key";
import { Probot } from "../";
import { logWarningsForObsoleteEnvironmentVariables } from "../helpers/log-warnings-for-obsolete-environment-variables";
logWarningsForObsoleteEnvironmentVariables();
program
.usage("[options] [path/to/app.js...]")
.option(
"-e, --event <event-name>",
"Event name",
process.env.GITHUB_EVENT_NAME
)
.option(
"-p, --payload-path <payload-path>",
"Path to the event payload",
process.env.GITHUB_EVENT_PATH
)
.option(
"-t, --token <access-token>",
"Access token",
process.env.GITHUB_TOKEN
)
.option("-a, --app <id>", "ID of the GitHub App", process.env.APP_ID)
.option(
"-P, --private-key <file>",
"Path to certificate of the GitHub App",
process.env.PRIVATE_KEY_PATH
)
.parse(process.argv);
const githubToken = program.token;
if (!program.event || !program.payloadPath) {
program.help();
}
const privateKey = getPrivateKey();
if (!githubToken && (!program.app || !privateKey)) {
console.warn(
"No token specified and no certificate found, which means you will not be able to do authenticated requests to GitHub"
);
}
const payload = require(path.resolve(program.payloadPath));
const probot = new Probot({
id: program.app,
privateKey: String(privateKey),
githubToken: githubToken,
});
probot.setup(program.args);
probot.log.debug("Receiving event", program.event);
probot.receive({ name: program.event, payload, id: uuidv4() }).catch(() => {
// Process must exist non-zero to indicate that the action failed to run
process.exit(1);
});

3
src/bin/probot-run.ts Normal file
View File

@ -0,0 +1,3 @@
import { Probot } from "../";
Probot.run(process.argv);

31
src/bin/probot.ts Normal file
View File

@ -0,0 +1,31 @@
import semver from "semver";
import program from "commander";
import updateNotifier from "update-notifier";
const pkg = require("../../package");
if (!semver.satisfies(process.version, pkg.engines.node)) {
console.log(
`Node.js version ${pkg.engines.node} is required. You have ${process.version}.`
);
process.exit(1);
}
if (pkg.version !== "0.0.0-development") {
updateNotifier({ pkg }).notify();
}
program
.version(pkg.version)
.usage("<command> [options]")
.command("run", "run the bot")
.command("receive", "Receive a single event and payload")
.on("command:*", (cmd) => {
if (!program.commands.find((c) => c._name == cmd[0])) {
console.error(`Invalid command: ${program.args.join(" ")}\n`);
program.outputHelp();
process.exit(1);
}
});
program.parse(process.argv);