Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: supports retrieving the port from the configuration file (#250) #251

Merged
merged 4 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.

Expand Down
32 changes: 23 additions & 9 deletions src/cmd/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
12 changes: 12 additions & 0 deletions test/cmd/dev.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
});
7 changes: 7 additions & 0 deletions test/fixtures/example-port/app/router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

module.exports = app => {
app.get('/', ctx => {
ctx.body = 'hi, egg';
});
};
9 changes: 9 additions & 0 deletions test/fixtures/example-port/config/config.default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

exports.key = '12345';

exports.cluster = {
listen: {
port: 6001,
},
};
3 changes: 3 additions & 0 deletions test/fixtures/example-port/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "example"
}