-
Notifications
You must be signed in to change notification settings - Fork 127
/
index.ts
99 lines (87 loc) Β· 2.44 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
//
// Copyright (c) Microsoft.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
// bump
import express from 'express';
import Debug from 'debug';
import type { ExecutionEnvironment, IReposApplication, SiteConfiguration } from './interfaces';
import configResolver from './lib/config';
import initialize from './middleware/initialize';
export * from './interfaces';
type InitializeCall = (
executionEnvironment: ExecutionEnvironment,
config: SiteConfiguration,
configurationError: Error
) => Promise<ExecutionEnvironment>;
export function createExpressApplication(): IReposApplication {
Debug.debug('startup')('starting web framework...');
const app = express() as any as IReposApplication;
app.initializeApplication = initializeApp.bind(undefined, app, express, __dirname);
app.startupApplication = commonStartup.bind(
undefined,
app.initializeApplication,
false /* not a job */,
true /* enable all apps */,
app
);
return app;
}
function initializeApp(
app: IReposApplication,
express: any,
dirname: string,
executionEnvironment: ExecutionEnvironment,
config: SiteConfiguration,
configurationError: Error
) {
return initialize(executionEnvironment, app, express, dirname, config, configurationError);
}
export async function commonStartup(
call: InitializeCall,
isJob: boolean,
enableAllGitHubApps: boolean,
app?: IReposApplication,
entrypointName?: string
) {
const executionEnvironment: ExecutionEnvironment = {
isJob,
enableAllGitHubApps,
entrypointName,
//
expressApplication: app,
//
providers: undefined,
skipModules: new Set(),
//
started: new Date(),
};
let painlessConfigResolver = null;
try {
painlessConfigResolver = configResolver();
} catch (error) {
console.warn('Painless config resolver initialization error:');
console.error(error);
throw error;
}
let config: any = null;
let configurationError: Error = null;
try {
config = await painlessConfigResolver.resolve();
} catch (error) {
configurationError = error;
}
if (isJob) {
executionEnvironment.skipModules.add('web');
}
try {
await call(executionEnvironment, config, configurationError);
} catch (startupError) {
console.error(`Startup error: ${startupError}`);
if (startupError.stack) {
console.error(startupError.stack);
}
process.exit(1);
}
return executionEnvironment;
}