From 4896126c32fd2ad229aa39afb73db78c324e8810 Mon Sep 17 00:00:00 2001 From: Caleb Ukle Date: Fri, 19 May 2023 11:55:10 -0500 Subject: [PATCH] fix(web): provide free port to http-server (#16917) --- packages/web/package.json | 1 + .../src/executors/file-server/file-server.impl.ts | 15 ++++++++------- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/packages/web/package.json b/packages/web/package.json index 1a3085746c1d3..de0a32e3029af 100644 --- a/packages/web/package.json +++ b/packages/web/package.json @@ -32,6 +32,7 @@ "dependencies": { "chalk": "^4.1.0", "chokidar": "^3.5.1", + "detect-port": "^1.5.1", "http-server": "^14.1.0", "ignore": "^5.0.4", "tslib": "^2.3.0", diff --git a/packages/web/src/executors/file-server/file-server.impl.ts b/packages/web/src/executors/file-server/file-server.impl.ts index b998060c392d9..82d06e269b232 100644 --- a/packages/web/src/executors/file-server/file-server.impl.ts +++ b/packages/web/src/executors/file-server/file-server.impl.ts @@ -14,6 +14,7 @@ import { watch } from 'chokidar'; import { platform } from 'os'; import { join, resolve } from 'path'; import { readModulePackageJson } from 'nx/src/utils/package-json'; +import * as detectPort from 'detect-port'; // platform specific command name const pmCmd = platform() === 'win32' ? `npx.cmd` : 'npx'; @@ -24,10 +25,6 @@ function getHttpServerArgs(options: Schema) { if (options.cors) { args.push(`--cors`); } - - if (options.port) { - args.push(`-p=${options.port}`); - } if (options.host) { args.push(`-a=${options.host}`); } @@ -182,6 +179,11 @@ export default async function* fileServerExecutor( pathToHttpServerBin ); + // detect port as close to when used to prevent port being used by another process + // when running in parallel + const port = await detectPort(options.port || 8080); + args.push(`-p=${port}`); + const serve = fork(pathToHttpServer, [outputPath, ...args], { stdio: 'pipe', cwd: context.root, @@ -203,6 +205,7 @@ export default async function* fileServerExecutor( }; process.on('exit', processExitListener); process.on('SIGTERM', processExitListener); + serve.stdout.on('data', (chunk) => { if (chunk.toString().indexOf('GET') === -1) { process.stdout.write(chunk); @@ -214,9 +217,7 @@ export default async function* fileServerExecutor( yield { success: true, - baseUrl: `${options.ssl ? 'https' : 'http'}://${options.host}:${ - options.port - }`, + baseUrl: `${options.ssl ? 'https' : 'http'}://${options.host}:${port}`, }; return new Promise<{ success: boolean }>((res) => {