-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathdev.ts
More file actions
83 lines (73 loc) · 2.54 KB
/
dev.ts
File metadata and controls
83 lines (73 loc) · 2.54 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 { debuglog } from 'node:util';
import { getFrameworkPath } from '@eggjs/utils';
import { Flags } from '@oclif/core';
import { detect } from 'detect-port';
import { BaseCommand } from '../baseCommand.ts';
import { getSourceFilename } from '../utils.ts';
const debug = debuglog('egg/bin/commands/dev');
export default class Dev<T extends typeof Dev> extends BaseCommand<T> {
static override description = 'Start server at local dev mode';
static override examples = ['<%= config.bin %> <%= command.id %>'];
static override flags = {
port: Flags.integer({
description: 'listening port, default to 7001',
char: 'p',
}),
workers: Flags.integer({
char: 'c',
aliases: ['cluster'],
description: 'numbers of app workers',
default: 1,
}),
framework: Flags.string({
description: 'specify framework that can be absolute path or npm package, default is "egg"',
}),
sticky: Flags.boolean({
description: 'start a sticky cluster server',
}),
};
public async run(): Promise<void> {
this.env.NODE_ENV = this.env.NODE_ENV ?? 'development';
debug('NODE_ENV: %o', this.env.NODE_ENV);
this.env.EGG_MASTER_CLOSE_TIMEOUT = '1000';
const ext = this.isESM ? 'mjs' : 'cjs';
const serverBin = getSourceFilename(`../scripts/start-cluster.${ext}`);
const eggStartOptions = await this.formatEggStartOptions();
const args = [JSON.stringify(eggStartOptions)];
const execArgv = await this.buildRequiresExecArgv();
await this.forkNode(serverBin, args, { execArgv });
}
protected async formatEggStartOptions(): Promise<{
baseDir: string;
workers: number;
port: number;
framework: string;
typescript: boolean;
tscompiler: string | undefined;
sticky: boolean | undefined;
}> {
const { flags } = this;
flags.framework = getFrameworkPath({
framework: flags.framework,
baseDir: flags.base,
});
if (!flags.port) {
const defaultPort = parseInt(process.env.EGG_BIN_DEFAULT_PORT ?? '7001');
debug('detect available port');
flags.port = await detect(defaultPort);
if (flags.port !== defaultPort) {
console.warn('[@eggjs/bin] server port %o is unavailable, now using port %o', defaultPort, flags.port);
}
debug(`use available port ${flags.port}`);
}
return {
baseDir: flags.base,
workers: flags.workers,
port: flags.port,
framework: flags.framework,
typescript: flags.typescript,
tscompiler: flags.tscompiler,
sticky: flags.sticky,
};
}
}