Skip to content

Commit

Permalink
fix(@nguniversal/builders): strip out newlines
Browse files Browse the repository at this point in the history
only strip out the last new line, add test

(cherry picked from commit f471ad7)
  • Loading branch information
tomscript authored and alan-agius4 committed May 9, 2023
1 parent fe28e8a commit ca8a5bb
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 11 deletions.
39 changes: 38 additions & 1 deletion modules/builders/src/ssr-dev-server/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import * as https from 'https';
import { from, throwError, timer } from 'rxjs';
import { concatMap, debounceTime, mergeMap, retryWhen, take } from 'rxjs/operators';
import { createArchitect, host } from '../../testing/utils';
import { SSRDevServerBuilderOutput } from './index';
import { SSRDevServerBuilderOutput, log } from './index';

// todo check why it resolves to mjs
// [ERR_REQUIRE_ESM]: Must use import to load ES Module
Expand Down Expand Up @@ -168,6 +168,43 @@ describe('Serve SSR Builder', () => {
.toPromise();
});

describe('test logger', () => {
let resp = { stderr: '', stdout: '' };
const logger = {
error: (stderr: string) => (resp.stderr = stderr),
info: (stdout: string) => (resp.stdout = stdout),
};

afterEach(() => (resp = { stderr: '', stdout: '' }));

it('should properly strip out new lines from output', async () => {
const data = { stderr: 'Error message\n', stdout: 'Output message\n' };
log(data, logger as any);
expect(resp.stderr).toBe('Error message');
expect(resp.stdout).toBe('Output message');
});

it('should only strip out the last new line', async () => {
const data = { stderr: 'Error message\n\n\n', stdout: 'Output message\n\n' };
log(data, logger as any);
expect(resp.stderr).toBe('Error message\n\n');
expect(resp.stdout).toBe('Output message\n');
});

it('work fine when nothing to strip out', async () => {
const data = { stderr: 'Typical error', stdout: 'Typical output' };
log(data, logger as any);
expect(resp.stderr).toBe('Typical error');
expect(resp.stdout).toBe('Typical output');
});

it('strip out webpack scheme', async () => {
const data = { stderr: 'webpack://foo', stdout: '' };
log(data, logger as any);
expect(resp.stderr).toBe('.foo');
});
});

it('proxies requests based on the proxy configuration file provided in the option', async () => {
const proxyServer = http.createServer((request, response) => {
if (request.url?.endsWith('/test')) {
Expand Down
27 changes: 17 additions & 10 deletions modules/builders/src/ssr-dev-server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,22 @@ export function execute(
);
}

// Logs output to the terminal.
// Removes any trailing new lines from the output.
export function log(
{ stderr, stdout }: { stderr: string | undefined; stdout: string | undefined },
logger: logging.LoggerApi,
) {
if (stderr) {
// Strip the webpack scheme (webpack://) from error log.
logger.error(stderr.replace(/\n?$/, '').replace(/webpack:\/\//g, '.'));
}

if (stdout && !IGNORED_STDOUT_MESSAGES.some((x) => stdout.includes(x))) {
logger.info(stdout.replace(/\n?$/, ''));
}
}

function startNodeServer(
serverOutput: BuilderOutput,
port: number,
Expand All @@ -191,16 +207,7 @@ function startNodeServer(
return of(null).pipe(
delay(0), // Avoid EADDRINUSE error since it will cause the kill event to be finish.
switchMap(() => spawnAsObservable('node', args, { env, shell: true })),
tap(({ stderr, stdout }) => {
if (stderr) {
// Strip the webpack scheme (webpack://) from error log.
logger.error(stderr.replace(/webpack:\/\//g, '.'));
}

if (stdout && !IGNORED_STDOUT_MESSAGES.some((x) => stdout.includes(x))) {
logger.info(stdout);
}
}),
tap((res) => log({ stderr: res.stderr, stdout: res.stdout }, logger)),
ignoreElements(),
// Emit a signal after the process has been started
startWith(undefined),
Expand Down

0 comments on commit ca8a5bb

Please sign in to comment.