Skip to content

Commit

Permalink
fix(web): return port from http-server process
Browse files Browse the repository at this point in the history
  • Loading branch information
barbados-clemens committed May 19, 2023
1 parent 3472897 commit a5c66ff
Showing 1 changed file with 39 additions and 6 deletions.
45 changes: 39 additions & 6 deletions packages/web/src/executors/file-server/file-server.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,19 +203,52 @@ 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);
}
});

// TODO(caleb): should this be in devkit?
function stripConsoleColors(log: string): string {
return log?.replace(
/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g,
''
);
}

const getPortFromOuput = async () => {
return new Promise((res, rej) => {
serve.stdout.on('data', (chunk) => {
const str = chunk.toString();
// need to strip colors other wise the port includes color codes
// which cause cypress to throw reading the url;
const lines = stripConsoleColors(str).split('\n');
// Available on:
// http://localhost:8081
// Hit CTRL-C to stop the server
const line = lines?.find((l) => l.includes(options.host))?.trim();
if (line) {
const u = new URL(line);

if (u.port) {
res(u.port);
} else {
rej('Could not find port');
}
}

if (str.indexOf('GET') === -1) {
process.stdout.write(chunk);
}
});
});
};
serve.stderr.on('data', (chunk) => {
process.stderr.write(chunk);
});

const port = await getPortFromOuput();

yield {
success: true,
baseUrl: `${options.ssl ? 'https' : 'http'}://${options.host}:${
options.port
port || options.port
}`,
};

Expand Down

0 comments on commit a5c66ff

Please sign in to comment.