Skip to content

Commit 59d06f4

Browse files
jameskrausclaude
andauthored
feat: use dynamic port allocation to support multiple plugin instances (#208)
* feat: use dynamic port allocation to support multiple plugin instances BREAKING CHANGE Cypress config callback is now async Replace hardcoded port 8765 with get-port library to automatically find available ports. This allows multiple instances of cypress-watch-and-reload to run simultaneously without port conflicts. - Add [email protected] dependency for dynamic port allocation with CommonJS support - Update plugins.js to use async/await and get available port - Pass selected port to client via environment variable - Update support.js to use dynamic port from environment without fallback 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]> * Remove console log * docs: update README examples for async plugin usage The plugin now returns a Promise due to dynamic port allocation. Updated examples to show async/await usage where needed and added clarifying note about when to use await vs direct return. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]> --------- Co-authored-by: Claude <[email protected]>
1 parent f5ff87e commit 59d06f4

File tree

5 files changed

+40
-7
lines changed

5 files changed

+40
-7
lines changed

README.md

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,19 @@ Note: this plugin will work if you pass both `on` and `config` arguments, or jus
5353

5454
```js
5555
// pass both arguments
56-
require('cypress-watch-and-reload/plugins')(on, config)
56+
await require('cypress-watch-and-reload/plugins')(on, config)
5757
// or pass just the config object
58-
require('cypress-watch-and-reload/plugins')(config)
58+
await require('cypress-watch-and-reload/plugins')(config)
59+
```
60+
61+
Note: The plugin returns a Promise. Use `await` if not directly returning from `setupNodeEvents`:
62+
63+
```js
64+
async setupNodeEvents(on, config) {
65+
await require('cypress-watch-and-reload/plugins')(on, config)
66+
// do other setup...
67+
return config
68+
}
5969
```
6070

6171
**Important:** make sure to return the plugin registration or the `config` object
@@ -66,8 +76,8 @@ setupNodeEvents(on, config) {
6676
return require('cypress-watch-and-reload/plugins')(on, config)
6777
}
6878
// or return the config object
69-
setupNodeEvents(on, config) {
70-
require('cypress-watch-and-reload/plugins')(on, config)
79+
async setupNodeEvents(on, config) {
80+
await require('cypress-watch-and-reload/plugins')(on, config)
7181
return config
7282
}
7383
```

package-lock.json

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"dependencies": {
2323
"async-wait-until": "1.2.6",
2424
"chokidar": "3.6.0",
25+
"get-port": "5.1.1",
2526
"ws": "8.18.3"
2627
},
2728
"devDependencies": {

plugins.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
const WebSocket = require('ws')
22
const chokidar = require('chokidar')
3+
const getPort = require('get-port')
34

4-
module.exports = (on, config) => {
5+
module.exports = async (on, config) => {
56
// sometimes the users might pass both arguments
67
// require('cypress-watch-and-reload')(on, config)
78
// or just the config
@@ -20,7 +21,8 @@ module.exports = (on, config) => {
2021
// https://github.com/websockets/ws#simple-server
2122
// create socket even if not watching files to avoid
2223
// tripping up client trying to connect
23-
const wss = new WebSocket.Server({ port: 8765 })
24+
const port = await getPort({ port: 8765 })
25+
const wss = new WebSocket.Server({ port })
2426
let client // future Cypress client
2527

2628
const env = config.env || {}
@@ -85,5 +87,6 @@ module.exports = (on, config) => {
8587
config.env = {}
8688
}
8789
config.env.cypressWatchAndReloadPluginInitialized = true
90+
config.env.cypressWatchAndReloadPort = port
8891
return config
8992
}

support.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
if (Cypress.config('isInteractive')) {
44
const insertToggleButton = require('./ui/toggle-btn')
55
const waitUntil = require('async-wait-until')
6-
const ws = new WebSocket('ws://localhost:8765')
6+
const port = Cypress.env('cypressWatchAndReloadPort')
7+
const ws = new WebSocket(`ws://localhost:${port}`)
78

89
let watchAndReloadEnabled = true
910
const button = insertToggleButton()

0 commit comments

Comments
 (0)