From 513c81b803ee84c6213fc5854c726e89425aa4ac Mon Sep 17 00:00:00 2001 From: Spencer Date: Mon, 4 Apr 2022 10:53:08 -0500 Subject: [PATCH] [toolingLog] when indent block is synchronous, dedent synchronously (#129269) Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com> --- .../src/tooling_log/tooling_log.test.ts | 18 ++++++++++++ .../src/tooling_log/tooling_log.ts | 28 +++++++++++++------ packages/kbn-pm/dist/index.js | 21 +++++++++----- 3 files changed, 51 insertions(+), 16 deletions(-) diff --git a/packages/kbn-dev-utils/src/tooling_log/tooling_log.test.ts b/packages/kbn-dev-utils/src/tooling_log/tooling_log.test.ts index 506f89786917f..8d8ef81e613fd 100644 --- a/packages/kbn-dev-utils/src/tooling_log/tooling_log.test.ts +++ b/packages/kbn-dev-utils/src/tooling_log/tooling_log.test.ts @@ -95,6 +95,24 @@ describe('#indent()', () => { ] `); }); + + it('resets the indent synchrounsly if the block does not return a promise', () => { + const log = new ToolingLog(); + const writer = new ToolingLogCollectingWriter(); + log.setWriters([writer]); + + log.info('foo'); + log.indent(4, () => log.error('bar')); + log.info('baz'); + + expect(writer.messages).toMatchInlineSnapshot(` + Array [ + " info foo", + " │ERROR bar", + " info baz", + ] + `); + }); }); (['verbose', 'debug', 'info', 'success', 'warning', 'error', 'write'] as const).forEach( diff --git a/packages/kbn-dev-utils/src/tooling_log/tooling_log.ts b/packages/kbn-dev-utils/src/tooling_log/tooling_log.ts index e9b5ab04a7390..727f71f85abc4 100644 --- a/packages/kbn-dev-utils/src/tooling_log/tooling_log.ts +++ b/packages/kbn-dev-utils/src/tooling_log/tooling_log.ts @@ -62,23 +62,33 @@ export class ToolingLog { * @param delta the number of spaces to increase/decrease the indentation * @param block a function to run and reset any indentation changes after */ - public indent(delta: number): undefined; + public indent(delta: number): void; public indent(delta: number, block: () => Promise): Promise; public indent(delta: number, block: () => T): T; - public indent(delta = 0, block?: () => T | Promise) { + public indent(delta = 0, block?: () => T | Promise): void | T | Promise { const originalWidth = this.indentWidth$.getValue(); this.indentWidth$.next(Math.max(originalWidth + delta, 0)); if (!block) { return; } - return (async () => { - try { - return await block(); - } finally { - this.indentWidth$.next(originalWidth); - } - })(); + const maybePromise: any = block(); + if ( + typeof maybePromise === 'object' && + maybePromise && + typeof maybePromise.then === 'function' + ) { + return (async () => { + try { + return await maybePromise; + } finally { + this.indentWidth$.next(originalWidth); + } + })(); + } + + this.indentWidth$.next(originalWidth); + return maybePromise; } public verbose(...args: any[]) { diff --git a/packages/kbn-pm/dist/index.js b/packages/kbn-pm/dist/index.js index 0b6ebc9deccb4..6d4ddbd31211a 100644 --- a/packages/kbn-pm/dist/index.js +++ b/packages/kbn-pm/dist/index.js @@ -649,13 +649,20 @@ class ToolingLog { return; } - return (async () => { - try { - return await block(); - } finally { - this.indentWidth$.next(originalWidth); - } - })(); + const maybePromise = block(); + + if (typeof maybePromise === 'object' && maybePromise && typeof maybePromise.then === 'function') { + return (async () => { + try { + return await maybePromise; + } finally { + this.indentWidth$.next(originalWidth); + } + })(); + } + + this.indentWidth$.next(originalWidth); + return maybePromise; } verbose(...args) {