Skip to content
Source
Sign inSign up
TanStack/router
Open History to see the last commit

derivePort.ts

27 lines859 Bytes

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
20​
21export async function getDummyServerPort(input: string): Promise<number> {
22 return await derivePort(`${input}-external`)
24​
25export async function getTestServerPort(input: string): Promise<number> {
26 return await derivePort(input)