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: Port in use checking for development server #6437

Merged
merged 22 commits into from
Nov 21, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
d0ed643
Initial port in use check implementation
Josh-Walker-GM Sep 21, 2022
0bc39a7
Added suggested port, initial prompt and ensured consistent logging s…
Josh-Walker-GM Sep 22, 2022
ca0b549
Removed excess style import
Josh-Walker-GM Sep 22, 2022
e47928c
Changed to use node-portfinder, tidied console logging and added init…
Josh-Walker-GM Sep 22, 2022
466cecd
fix(deps): pin portfinder and dedupe yarn.lock
jtoar Oct 1, 2022
d0e8950
Improved conditionals
Josh-Walker-GM Oct 1, 2022
8b0488f
Added port flag to the watch api-server
Josh-Walker-GM Oct 2, 2022
d33584c
Added working port checking
Josh-Walker-GM Oct 2, 2022
d1aba20
Merge remote-tracking branch 'upstream/main' into dev-server-port
Josh-Walker-GM Oct 12, 2022
95044cd
Made ports consistently a number type, fixed typo in port use warning…
Josh-Walker-GM Oct 12, 2022
90baa43
Added default port options to mock config and updated inline snapshot…
Josh-Walker-GM Oct 12, 2022
3d06f93
Apply suggestions from code review
Josh-Walker-GM Nov 6, 2022
b1ba9bc
Applying more suggestions from code review
Josh-Walker-GM Nov 6, 2022
94bf0be
Changed to a simple error warning
Josh-Walker-GM Nov 9, 2022
0b15df5
style: remove "=" after flag
jtoar Nov 16, 2022
95938da
fix: substring test and snapshot
jtoar Nov 16, 2022
7652b2c
Apply suggestions from code review
Josh-Walker-GM Nov 16, 2022
a6990a5
Remove unused function
Josh-Walker-GM Nov 16, 2022
7c8676c
Merge branch 'main' into dev-server-port
jtoar Nov 17, 2022
bdb32e5
Merge remote-tracking branch 'upstream/main' into dev-server-port
Josh-Walker-GM Nov 18, 2022
b2aaea0
Merge branch 'main' into dev-server-port
jtoar Nov 21, 2022
b6b0aa6
Merge branch 'main' into dev-server-port
jtoar Nov 21, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"param-case": "3.0.4",
"pascalcase": "1.0.0",
"pluralize": "8.0.0",
"portfinder": "^1.0.32",
"prettier": "2.7.1",
"prisma": "4.3.1",
"prompts": "2.4.2",
Expand Down
74 changes: 73 additions & 1 deletion packages/cli/src/commands/devHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import fs from 'fs'
import { argv } from 'process'

import concurrently from 'concurrently'
import portfinder from 'portfinder'
import prompts from 'prompts'

import { getConfig } from '@redwoodjs/internal/dist/config'
import { shutdownPort } from '@redwoodjs/internal/dist/dev'
Expand Down Expand Up @@ -49,8 +51,78 @@ export const handler = async ({
}

if (side.includes('web')) {
let proposedPort = parseInt(getConfig().web.port)
const forwardedPortMatches = forward.match(
/--port=[0-9][0-9]?[0-9]?[0-9]?[0-9]? ?/
)
const forwardedPortSet =
forwardedPortMatches !== null && forwardedPortMatches.length == 1
if (forwardedPortSet) {
proposedPort = parseInt(
forwardedPortMatches[0]
.substring(forwardedPortMatches[0].indexOf('=') + 1)
.trim()
)
}

const availablePort = await portfinder.getPortPromise({
port: proposedPort,
stopPort: proposedPort + 64,
})
if (availablePort != proposedPort) {
if (availablePort == -1) {
console.error(
c.error(
`${
forwardedPortSet ? 'Forwarded' : 'Configured'
} "web" port ${proposedPort} is already in use and no neighbouring port is available! Cannot start development server.`
)
)
console.log(
c.info(
`Configured port can be updated in 'redwood.toml' or can be forwarded via the command line parameter like so 'yarn rw dev --fwd="--port=12345"'.`
)
)
process.exit(1)
} else {
console.error(
c.error(
`${
forwardedPortSet ? 'Forwarded' : 'Configured'
} "web" port ${proposedPort} is already in use!`
)
)
const useAvailablePort = await prompts({
type: 'confirm',
name: 'port',
message: `Do you wish to use port ${availablePort} instead?`,
initial: true,
active: 'Yes',
inactive: 'No',
})
if (useAvailablePort.port) {
// TODO: Consider if there is a better way to propagate the port?
if (forwardedPortSet) {
forward = forward.replace(
forwardedPortMatches[0],
` --port=${availablePort}`
)
} else {
forward = forward.concat(` --port=${availablePort}`)
}
} else {
console.log(
c.info(
`Configured port can be updated in 'redwood.toml' or can be forwarded via the command line parameter like so 'yarn rw dev --fwd="--port=12345"'.`
)
)
process.exit(1)
}
}
}

try {
await shutdownPort(getConfig().web.port)
await shutdownPort(availablePort)
} catch (e) {
errorTelemetry(process.argv, `Error shutting down "web": ${e.message}`)
console.error(
Expand Down
1 change: 1 addition & 0 deletions packages/internal/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
"@types/babel__core": "7.1.19",
"@types/findup-sync": "4.0.2",
"@types/fs-extra": "9.0.13",
"@types/node": "16.11.47",
"@types/rimraf": "3.0.2",
"babel-plugin-tester": "10.1.0",
"graphql-tag": "2.12.6",
Expand Down