Skip to content

Commit

Permalink
[toolingLog] when indent block is synchronous, dedent synchronously (#…
Browse files Browse the repository at this point in the history
…129269)

Co-authored-by: kibanamachine <[email protected]>
  • Loading branch information
Spencer and kibanamachine authored Apr 4, 2022
1 parent 3fd4621 commit 513c81b
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 16 deletions.
18 changes: 18 additions & 0 deletions packages/kbn-dev-utils/src/tooling_log/tooling_log.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
28 changes: 19 additions & 9 deletions packages/kbn-dev-utils/src/tooling_log/tooling_log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(delta: number, block: () => Promise<T>): Promise<T>;
public indent<T>(delta: number, block: () => T): T;
public indent<T>(delta = 0, block?: () => T | Promise<T>) {
public indent<T>(delta = 0, block?: () => T | Promise<T>): void | T | Promise<T> {
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[]) {
Expand Down
21 changes: 14 additions & 7 deletions packages/kbn-pm/dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 513c81b

Please sign in to comment.