-
Notifications
You must be signed in to change notification settings - Fork 44
Fix port configuration to match browser URL #589
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,7 +13,8 @@ const config = require('./app/config') | |
| const { findAvailablePort } = require('./lib/utils') | ||
|
|
||
| // Set configuration variables | ||
| const port = parseInt(process.env.PORT || config.port, 10) || 2000 | ||
| const browserPort = parseInt(process.env.PORT || config.port, 10) || 3000 | ||
| const nodemonPort = Math.max(1024, browserPort - 1000) // Ensure port is >= 1024 | ||
|
|
||
| // Delete all the files in /public build directory | ||
| function cleanPublic() { | ||
|
|
@@ -82,9 +83,15 @@ async function startNodemon(done) { | |
| let availablePort | ||
|
|
||
| try { | ||
| availablePort = await findAvailablePort(port) | ||
| availablePort = await findAvailablePort(nodemonPort) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this should be finding an available port for the The nodemon port could then be a random available port selected, which the user is not even aware of? |
||
| if (!availablePort) { | ||
| throw new Error(`Port ${port} in use`) | ||
| throw new Error(`Port ${nodemonPort} in use`) | ||
| } | ||
|
|
||
| // Also check if the browser port is available | ||
| const { checkPortStatus } = require('portscanner') | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @copilot move this line to the top? |
||
| if ((await checkPortStatus(browserPort, '127.0.0.1')) === 'open') { | ||
| throw new Error(`Browser port ${browserPort} in use`) | ||
| } | ||
| } catch (error) { | ||
| done(new PluginError('startNodemon', error)) | ||
|
|
@@ -128,7 +135,7 @@ async function startBrowserSync(done) { | |
| browserSync.init( | ||
| { | ||
| proxy: `localhost:${proxyPort}`, | ||
| port: proxyPort + 1000, | ||
| port: browserPort, | ||
| ui: false, | ||
| files: ['app/views/**/*.*', 'lib/example-templates/**/*.*'], | ||
| ghostMode: false, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| // Mock inquirer to avoid interactive prompts in tests | ||
| jest.mock('@inquirer/prompts', () => ({ | ||
| confirm: jest.fn() | ||
| })) | ||
|
|
||
| // Mock portscanner to control port availability | ||
| jest.mock('portscanner', () => ({ | ||
| checkPortStatus: jest.fn(), | ||
| findAPortNotInUse: jest.fn() | ||
| })) | ||
|
|
||
| const inquirer = require('@inquirer/prompts') | ||
| const { checkPortStatus, findAPortNotInUse } = require('portscanner') | ||
|
|
||
| const { findAvailablePort } = require('../../lib/utils/find-available-port') | ||
|
|
||
| describe('Port configuration', () => { | ||
| beforeEach(() => { | ||
| jest.clearAllMocks() | ||
| }) | ||
|
|
||
| it('findAvailablePort returns provided port when available', async () => { | ||
| checkPortStatus.mockResolvedValue('closed') | ||
|
|
||
| const result = await findAvailablePort(2000) | ||
|
|
||
| expect(result).toBe(2000) | ||
| expect(checkPortStatus).toHaveBeenCalledWith(2000, '127.0.0.1') | ||
| }) | ||
|
|
||
| it('findAvailablePort finds alternative when port is in use and user agrees', async () => { | ||
| checkPortStatus.mockResolvedValue('open') | ||
| inquirer.confirm.mockResolvedValue(true) | ||
| findAPortNotInUse.mockResolvedValue(2001) | ||
|
|
||
| const result = await findAvailablePort(2000) | ||
|
|
||
| expect(result).toBe(2001) | ||
| expect(inquirer.confirm).toHaveBeenCalled() | ||
| expect(findAPortNotInUse).toHaveBeenCalledWith(2000, 3000, '127.0.0.1') | ||
| }) | ||
|
|
||
| it('findAvailablePort returns undefined when port is in use and user declines', async () => { | ||
| checkPortStatus.mockResolvedValue('open') | ||
| inquirer.confirm.mockResolvedValue(false) | ||
|
|
||
| const result = await findAvailablePort(2000) | ||
|
|
||
| expect(result).toBeUndefined() | ||
| expect(inquirer.confirm).toHaveBeenCalled() | ||
| expect(findAPortNotInUse).not.toHaveBeenCalled() | ||
| }) | ||
| }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot why does it need to be above 1024?