({
side = ['api', 'web'],
forward = '',
generate = true,
apiDebugPort,
})
| 18 | const defaultApiDebugPort = 18911 |
| 19 | |
| 20 | export const handler = async ({ |
| 21 | side = ['api', 'web'], |
| 22 | forward = '', |
| 23 | generate = true, |
| 24 | apiDebugPort, |
| 25 | }) => { |
| 26 | recordTelemetryAttributes({ |
| 27 | command: 'dev', |
| 28 | side: JSON.stringify(side), |
| 29 | generate, |
| 30 | }) |
| 31 | |
| 32 | const rwjsPaths = getPaths() |
| 33 | |
| 34 | const serverFile = serverFileExists() |
| 35 | |
| 36 | // Starting values of ports from config (redwood.toml) |
| 37 | let apiPreferredPort = parseInt(getConfig().api.port) |
| 38 | let webPreferredPort = parseInt(getConfig().web.port) |
| 39 | |
| 40 | // Assume we can have the ports we want |
| 41 | let apiAvailablePort = apiPreferredPort |
| 42 | let apiPortChangeNeeded = false |
| 43 | let webAvailablePort = webPreferredPort |
| 44 | let webPortChangeNeeded = false |
| 45 | |
| 46 | // Check api port, unless there's a serverFile. If there is a serverFile, we |
| 47 | // don't know what port will end up being used in the end. It's up to the |
| 48 | // author of the server file to decide and handle that |
| 49 | if (side.includes('api') && !serverFile) { |
| 50 | apiAvailablePort = await getFreePort(apiPreferredPort) |
| 51 | if (apiAvailablePort === -1) { |
| 52 | exitWithError(undefined, { |
| 53 | message: `Could not determine a free port for the api server`, |
| 54 | }) |
| 55 | } |
| 56 | apiPortChangeNeeded = apiAvailablePort !== apiPreferredPort |
| 57 | } |
| 58 | |
| 59 | // Check web port |
| 60 | if (side.includes('web')) { |
| 61 | // Extract any ports the user forwarded to the dev server and prefer that instead |
| 62 | const forwardedPortMatches = [ |
| 63 | ...forward.matchAll(/\-\-port(\=|\s)(?<port>[^\s]*)/g), |
| 64 | ] |
| 65 | if (forwardedPortMatches.length) { |
| 66 | webPreferredPort = parseInt(forwardedPortMatches.pop().groups.port) |
| 67 | } |
| 68 | webAvailablePort = await getFreePort(webPreferredPort, [ |
| 69 | apiPreferredPort, |
| 70 | apiAvailablePort, |
| 71 | ]) |
| 72 | if (webAvailablePort === -1) { |
| 73 | exitWithError(undefined, { |
| 74 | message: `Could not determine a free port for the web server`, |
| 75 | }) |
| 76 | } |
| 77 | webPortChangeNeeded = webAvailablePort !== webPreferredPort |
nothing calls this directly
no test coverage detected