UNPKG

4.42 kBJavaScriptView Raw
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5Object.defineProperty(exports, "__esModule", { value: true });
6exports.run = void 0;
7const pkg_conf_1 = __importDefault(require("pkg-conf"));
8const index_1 = require("./index");
9const setup_1 = require("./apps/setup");
10const get_log_1 = require("./helpers/get-log");
11const read_cli_options_1 = require("./bin/read-cli-options");
12const read_env_options_1 = require("./bin/read-env-options");
13const server_1 = require("./server/server");
14const default_1 = require("./apps/default");
15const resolve_app_function_1 = require("./helpers/resolve-app-function");
16/**
17 *
18 * @param appFnOrArgv set to either a probot application function: `(app) => { ... }` or to process.argv
19 */
20async function run(appFnOrArgv, additionalOptions) {
21 require("dotenv").config();
22 const envOptions = read_env_options_1.readEnvOptions(additionalOptions === null || additionalOptions === void 0 ? void 0 : additionalOptions.env);
23 const cliOptions = Array.isArray(appFnOrArgv)
24 ? read_cli_options_1.readCliOptions(appFnOrArgv)
25 : {};
26 const {
27 // log options
28 logLevel: level, logFormat, logLevelInString, logMessageKey, sentryDsn,
29 // server options
30 host, port, webhookPath, webhookProxy,
31 // probot options
32 appId, privateKey, redisConfig, secret, baseUrl,
33 // others
34 args, } = { ...envOptions, ...cliOptions };
35 const logOptions = {
36 level,
37 logFormat,
38 logLevelInString,
39 logMessageKey,
40 sentryDsn,
41 };
42 const log = get_log_1.getLog(logOptions);
43 const probotOptions = {
44 appId,
45 privateKey,
46 redisConfig,
47 secret,
48 baseUrl,
49 log: log.child({ name: "probot" }),
50 };
51 const serverOptions = {
52 host,
53 port,
54 webhookPath,
55 webhookProxy,
56 log: log.child({ name: "server" }),
57 Probot: index_1.Probot.defaults(probotOptions),
58 };
59 let server;
60 if (!appId || !privateKey) {
61 if (process.env.NODE_ENV === "production") {
62 if (!appId) {
63 throw new Error("App ID is missing, and is required to run in production mode. " +
64 "To resolve, ensure the APP_ID environment variable is set.");
65 }
66 else if (!privateKey) {
67 throw new Error("Certificate is missing, and is required to run in production mode. " +
68 "To resolve, ensure either the PRIVATE_KEY or PRIVATE_KEY_PATH environment variable is set and contains a valid certificate");
69 }
70 }
71 // Workaround for setup (#1512)
72 // When probot is started for the first time, it gets into a setup mode
73 // where `appId` and `privateKey` are not present. The setup mode gets
74 // these credentials. In order to not throw an error, we set the values
75 // to anything, as the Probot instance is not used in setup it makes no
76 // difference anyway.
77 server = new server_1.Server({
78 ...serverOptions,
79 Probot: index_1.Probot.defaults({
80 ...probotOptions,
81 appId: 1,
82 privateKey: "dummy value for setup, see #1512",
83 }),
84 });
85 await server.load(setup_1.setupAppFactory(host, port));
86 await server.start();
87 return server;
88 }
89 if (Array.isArray(appFnOrArgv)) {
90 const pkg = await pkg_conf_1.default("probot");
91 const combinedApps = async (app) => {
92 await server.load(default_1.defaultApp);
93 if (Array.isArray(pkg.apps)) {
94 for (const appPath of pkg.apps) {
95 const appFn = await resolve_app_function_1.resolveAppFunction(appPath);
96 await server.load(appFn);
97 }
98 }
99 const [appPath] = args;
100 const appFn = await resolve_app_function_1.resolveAppFunction(appPath);
101 await server.load(appFn);
102 };
103 server = new server_1.Server(serverOptions);
104 await server.load(combinedApps);
105 await server.start();
106 return server;
107 }
108 server = new server_1.Server(serverOptions);
109 await server.load(appFnOrArgv);
110 await server.start();
111 return server;
112}
113exports.run = run;
114//# sourceMappingURL=run.js.map
\No newline at end of file