Skip to content

Commit

Permalink
fix(web): provide free port to http-server (#16917)
Browse files Browse the repository at this point in the history
  • Loading branch information
barbados-clemens authored May 19, 2023
1 parent 34bdc4f commit 4896126
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
1 change: 1 addition & 0 deletions packages/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
15 changes: 8 additions & 7 deletions packages/web/src/executors/file-server/file-server.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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}`);
}
Expand Down Expand Up @@ -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,
Expand All @@ -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);
Expand All @@ -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) => {
Expand Down

1 comment on commit 4896126

@vercel
Copy link

@vercel vercel bot commented on 4896126 May 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

nx-dev – ./

nx-five.vercel.app
nx-dev-git-master-nrwl.vercel.app
nx.dev
nx-dev-nrwl.vercel.app

Please sign in to comment.