From 07e150f0f1cae2a75353428c9d10b4e9a16fd0db Mon Sep 17 00:00:00 2001 From: JarryChung <35088591+JarryChung@users.noreply.github.com> Date: Thu, 1 Feb 2024 17:48:31 +0800 Subject: [PATCH] feat: supports retrieving the port from the configuration file (#251) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adjust the order in which ports are obtained:option `--port` > [_egg.js_ configuration](https://www.eggjs.org/basics/config) `config/config.*.js` > `process.env.EGG_BIN_DEFAULT_PORT` > 7001 > other available ports. ![image](https://github.com/eggjs/egg-bin/assets/35088591/ab8ffafd-c3f3-414b-a46e-a0a8fb897a17) closes https://github.com/eggjs/egg-bin/issues/250 --------- Co-authored-by: tiga --- README.md | 2 +- src/cmd/dev.ts | 35 ++++++++++++++----- test/cmd/dev.test.ts | 12 +++++++ test/fixtures/example-port/app/router.js | 7 ++++ .../example-port/config/config.default.js | 9 +++++ test/fixtures/example-port/package.json | 3 ++ 6 files changed, 58 insertions(+), 10 deletions(-) create mode 100644 test/fixtures/example-port/app/router.js create mode 100644 test/fixtures/example-port/config/config.default.js create mode 100644 test/fixtures/example-port/package.json diff --git a/README.md b/README.md index 6a80b3bb..3da61dae 100644 --- a/README.md +++ b/README.md @@ -74,7 +74,7 @@ egg-bin dev #### dev options - `--framework` egg web framework root path. -- `--port` server port, default to `7001`. +- `--port` server port. If not specified, the port is obtained in the following order: [_egg.js_ configuration](https://www.eggjs.org/basics/config) `config/config.*.js` > `process.env.EGG_BIN_DEFAULT_PORT` > 7001 > other available ports. - `--workers` worker process number, default to `1` worker at local mode. - `--sticky` start a sticky cluster server, default to `false`. diff --git a/src/cmd/dev.ts b/src/cmd/dev.ts index 87fd952a..5a72baa7 100644 --- a/src/cmd/dev.ts +++ b/src/cmd/dev.ts @@ -55,19 +55,36 @@ 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) { + let configuredPort; + try { + const configuration = utils.getConfig({ + framework: this.framework, + baseDir: this.base, + env: 'local', + }); + configuredPort = configuration?.cluster?.listen?.port; + } catch (_) { /** skip when failing to read the configuration */ } + + 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, diff --git a/test/cmd/dev.test.ts b/test/cmd/dev.test.ts index 59b2a2bf..ecea4102 100644 --- a/test/cmd/dev.test.ts +++ b/test/cmd/dev.test.ts @@ -174,4 +174,16 @@ describe('test/cmd/dev.test.ts', () => { .end(done); }); }); + + describe('obtain the port from config.*.js', () => { + const cwd = path.join(fixtures, 'example-port'); + it('should obtain the port from config.default.js', () => { + coffee.fork(eggBin, [ 'dev' ], { + cwd, + }) + .expect('stdout', /"port":6001/) + .expect('code', 0) + .end(); + }); + }); }); diff --git a/test/fixtures/example-port/app/router.js b/test/fixtures/example-port/app/router.js new file mode 100644 index 00000000..5c0b57ca --- /dev/null +++ b/test/fixtures/example-port/app/router.js @@ -0,0 +1,7 @@ +'use strict'; + +module.exports = app => { + app.get('/', ctx => { + ctx.body = 'hi, egg'; + }); +}; diff --git a/test/fixtures/example-port/config/config.default.js b/test/fixtures/example-port/config/config.default.js new file mode 100644 index 00000000..34711bea --- /dev/null +++ b/test/fixtures/example-port/config/config.default.js @@ -0,0 +1,9 @@ +'use strict'; + +exports.key = '12345'; + +exports.cluster = { + listen: { + port: 6001, + }, +}; diff --git a/test/fixtures/example-port/package.json b/test/fixtures/example-port/package.json new file mode 100644 index 00000000..8389bd69 --- /dev/null +++ b/test/fixtures/example-port/package.json @@ -0,0 +1,3 @@ +{ + "name": "example" +} \ No newline at end of file