-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathstart.ts
More file actions
83 lines (73 loc) · 2.29 KB
/
start.ts
File metadata and controls
83 lines (73 loc) · 2.29 KB
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
import path from 'node:path';
import { ManifestStore } from '@eggjs/core';
import { importModule } from '@eggjs/utils';
import { readJSON } from 'utility';
import { Agent } from './agent.ts';
import { Application } from './application.ts';
import { type EggPlugin } from './types.ts';
export interface StartEggOptions {
/** specify framework that can be absolute path or npm package */
framework?: string;
/** directory of application, default to `process.cwd()` */
baseDir?: string;
/** ignore single process mode warning */
ignoreWarning?: boolean;
mode?: 'single';
env?: string;
plugins?: EggPlugin;
/** Skip lifecycle hooks, only trigger loadMetadata for manifest generation */
metadataOnly?: boolean;
}
export interface SingleModeApplication extends Application {
agent: SingleModeAgent;
}
export interface SingleModeAgent extends Agent {
app: SingleModeApplication;
}
/**
* Start egg with single process
*/
export async function startEgg(options: StartEggOptions = {}): Promise<SingleModeApplication> {
options.baseDir = options.baseDir ?? process.cwd();
options.mode = 'single';
ManifestStore.enableCompileCache(options.baseDir);
// get agent from options.framework and package.egg.framework
if (!options.framework) {
try {
const pkg = await readJSON(path.join(options.baseDir, 'package.json'));
options.framework = pkg.egg.framework;
} catch {
// ignore
}
}
let AgentClass = Agent;
let ApplicationClass = Application;
if (options.framework) {
const framework = await importModule(options.framework, {
paths: [options.baseDir],
});
AgentClass = framework.Agent;
ApplicationClass = framework.Application;
}
// In metadataOnly mode, skip agent entirely — only app metadata is needed
let agent: SingleModeAgent | undefined;
if (!options.metadataOnly) {
agent = new AgentClass({
...options,
}) as SingleModeAgent;
await agent.ready();
}
const application = new ApplicationClass({
...options,
}) as SingleModeApplication;
if (agent) {
application.agent = agent;
agent.application = application;
}
await application.ready();
if (!options.metadataOnly) {
// emit egg-ready message in agent and application
application.messenger.broadcast('egg-ready');
}
return application;
}