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 b998060c392d93..7d7f7e855f2404 100644 --- a/packages/web/src/executors/file-server/file-server.impl.ts +++ b/packages/web/src/executors/file-server/file-server.impl.ts @@ -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 }`, };