Create environment variable to flag the current index of the called script
Closed this issue · 5 comments
Say you run a watch
script with wsrun
in a workspace-based project with three packages and all three packages need to listen to a unique port. You could use some "find open port"-lib for this, but because of data races this can become quite flaky in my experiences, when watch
is called parallel. It would probably be less error prone, if we could access an environment variable like WSRUN_INDEX
which has the values 0, 1 and 2 to indicate that the script was called nth-times by wsrun in this case and we could use this value to define a port in the watch
script like this: const port = 8080 + parseInt(WSRUN_INDEX|| 0, 10)
.
(Note: I added the same request to yarns workspace run proposal. yarnpkg/rfcs#87 (comment))
You could base the port on the current package / read it from package.json (using processs.cwd()
)
Yeah, that could be possible, but the algorithm maybe looks a little bit weird, when I write a generic script which doesn't know the packages beforehand 🤔
I would still go with a config option read from process.cwd()
, as the order of execution may not be stable. Getting different ports may cause cross-app data mixup in localStorage / indexedDb / cookies
Hmm, okay. A stable index wasn't important to me so far in my use cases. Feel free to close this, if you dont plan to support it 🙃 thank you for this lib!
Ok, we can revisit if a problematic use case pops up again 😄