From d69ae0ffc55451e9b24624216440802b2e61c352 Mon Sep 17 00:00:00 2001 From: tiga Date: Mon, 29 Jan 2024 14:47:55 +0800 Subject: [PATCH] feat: supports retrieving the port from the configuration file (#250) --- src/cmd/dev.ts | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/src/cmd/dev.ts b/src/cmd/dev.ts index 87fd952a..340ff175 100644 --- a/src/cmd/dev.ts +++ b/src/cmd/dev.ts @@ -55,19 +55,33 @@ export class DevCommand extends BaseCommand { } protected async formatEggStartOptions() { - if (!this.port) { - const defaultPort = process.env.EGG_BIN_DEFAULT_PORT ?? 7001; - debug('detect available port'); - this.port = await detect(defaultPort); - if (this.port !== defaultPort) { - console.warn('[egg-bin] server port %s is in use, now using port %o', defaultPort, this.port); - } - debug(`use available port ${this.port}`); - } this.framework = utils.getFrameworkPath({ framework: this.framework, baseDir: this.base, }); + + if (!this.port) { + const configuration = utils.getConfig({ + framework: this.framework, + baseDir: this.base, + env: 'local', + }); + const configuredPort = configuration?.cluster?.listen?.port; + + if (configuredPort) { + this.port = configuredPort; + debug(`use port ${this.port} from configuration file`); + } else { + const defaultPort = process.env.EGG_BIN_DEFAULT_PORT ?? 7001; + debug('detect available port'); + this.port = await detect(defaultPort); + if (this.port !== defaultPort) { + console.warn('[egg-bin] server port %s is in use, now using port %o', defaultPort, this.port); + } + debug(`use available port ${this.port}`); + } + } + return { baseDir: this.base, workers: this.workers,