Skip to content
This repository was archived by the owner on Nov 22, 2024. It is now read-only.

Commit ca8a5bb

Browse files
tomscriptalan-agius4
authored andcommitted
fix(@nguniversal/builders): strip out newlines
only strip out the last new line, add test (cherry picked from commit f471ad7)
1 parent fe28e8a commit ca8a5bb

File tree

2 files changed

+55
-11
lines changed

2 files changed

+55
-11
lines changed

modules/builders/src/ssr-dev-server/index.spec.ts

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import * as https from 'https';
1313
import { from, throwError, timer } from 'rxjs';
1414
import { concatMap, debounceTime, mergeMap, retryWhen, take } from 'rxjs/operators';
1515
import { createArchitect, host } from '../../testing/utils';
16-
import { SSRDevServerBuilderOutput } from './index';
16+
import { SSRDevServerBuilderOutput, log } from './index';
1717

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

171+
describe('test logger', () => {
172+
let resp = { stderr: '', stdout: '' };
173+
const logger = {
174+
error: (stderr: string) => (resp.stderr = stderr),
175+
info: (stdout: string) => (resp.stdout = stdout),
176+
};
177+
178+
afterEach(() => (resp = { stderr: '', stdout: '' }));
179+
180+
it('should properly strip out new lines from output', async () => {
181+
const data = { stderr: 'Error message\n', stdout: 'Output message\n' };
182+
log(data, logger as any);
183+
expect(resp.stderr).toBe('Error message');
184+
expect(resp.stdout).toBe('Output message');
185+
});
186+
187+
it('should only strip out the last new line', async () => {
188+
const data = { stderr: 'Error message\n\n\n', stdout: 'Output message\n\n' };
189+
log(data, logger as any);
190+
expect(resp.stderr).toBe('Error message\n\n');
191+
expect(resp.stdout).toBe('Output message\n');
192+
});
193+
194+
it('work fine when nothing to strip out', async () => {
195+
const data = { stderr: 'Typical error', stdout: 'Typical output' };
196+
log(data, logger as any);
197+
expect(resp.stderr).toBe('Typical error');
198+
expect(resp.stdout).toBe('Typical output');
199+
});
200+
201+
it('strip out webpack scheme', async () => {
202+
const data = { stderr: 'webpack://foo', stdout: '' };
203+
log(data, logger as any);
204+
expect(resp.stderr).toBe('.foo');
205+
});
206+
});
207+
171208
it('proxies requests based on the proxy configuration file provided in the option', async () => {
172209
const proxyServer = http.createServer((request, response) => {
173210
if (request.url?.endsWith('/test')) {

modules/builders/src/ssr-dev-server/index.ts

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,22 @@ export function execute(
173173
);
174174
}
175175

176+
// Logs output to the terminal.
177+
// Removes any trailing new lines from the output.
178+
export function log(
179+
{ stderr, stdout }: { stderr: string | undefined; stdout: string | undefined },
180+
logger: logging.LoggerApi,
181+
) {
182+
if (stderr) {
183+
// Strip the webpack scheme (webpack://) from error log.
184+
logger.error(stderr.replace(/\n?$/, '').replace(/webpack:\/\//g, '.'));
185+
}
186+
187+
if (stdout && !IGNORED_STDOUT_MESSAGES.some((x) => stdout.includes(x))) {
188+
logger.info(stdout.replace(/\n?$/, ''));
189+
}
190+
}
191+
176192
function startNodeServer(
177193
serverOutput: BuilderOutput,
178194
port: number,
@@ -191,16 +207,7 @@ function startNodeServer(
191207
return of(null).pipe(
192208
delay(0), // Avoid EADDRINUSE error since it will cause the kill event to be finish.
193209
switchMap(() => spawnAsObservable('node', args, { env, shell: true })),
194-
tap(({ stderr, stdout }) => {
195-
if (stderr) {
196-
// Strip the webpack scheme (webpack://) from error log.
197-
logger.error(stderr.replace(/webpack:\/\//g, '.'));
198-
}
199-
200-
if (stdout && !IGNORED_STDOUT_MESSAGES.some((x) => stdout.includes(x))) {
201-
logger.info(stdout);
202-
}
203-
}),
210+
tap((res) => log({ stderr: res.stderr, stdout: res.stdout }, logger)),
204211
ignoreElements(),
205212
// Emit a signal after the process has been started
206213
startWith(undefined),

0 commit comments

Comments
 (0)