Open History to see the last commit
1import fs from 'node:fs'
2import { getRandomPort } from 'get-port-please'
3β
4/**
5 * Check if a port has been allocated, if it hasn't generate a random port and save it.
6 * @param {string} input - port test allocation
7 * @returns {number} A random port.
8 */
9export async function derivePort(input: string): Promise<number> {
10 const portFile = `port-${input}.txt`
11β
12 if (!fs.existsSync(portFile)) {
13 fs.writeFileSync(portFile, (await getRandomPort()).toString())
14 }
15β
16 const portNumber = parseInt(await fs.promises.readFile(portFile, 'utf-8'))
17 console.info(`Mapped "${input}" to port ${portNumber}`)
18 return portNumber
19}
20β
21export async function getDummyServerPort(input: string): Promise<number> {
22 return await derivePort(`${input}-external`)
23}
24β
25export async function getTestServerPort(input: string): Promise<number> {
26 return await derivePort(input)
27}