Skip to content

Commit aaeed36

Browse files
committed
feat(utils): return custom data from logger.group method
1 parent ef3d3b0 commit aaeed36

File tree

3 files changed

+33
-6
lines changed

3 files changed

+33
-6
lines changed

packages/utils/src/lib/logger.int.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,19 @@ ${ansis.cyan('└')} ${ansis.green(`Total line coverage is ${ansis.bold('82%')}`
229229
);
230230
});
231231

232+
it('should return result from worker', async () => {
233+
const result = { stats: { errors: 0, warnings: 0 }, problems: [] };
234+
235+
await expect(
236+
new Logger().group('Running plugin "ESLint"', async () => ({
237+
message: 'Completed "ESLint" plugin execution',
238+
result,
239+
})),
240+
).resolves.toBe(result);
241+
242+
expect(output).toContain('Completed "ESLint" plugin execution');
243+
});
244+
232245
it('should use log group prefix in child loggers', async () => {
233246
performanceNowSpy.mockReturnValueOnce(0).mockReturnValueOnce(1234); // group duration: 1.23 s
234247

packages/utils/src/lib/logger.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -237,19 +237,25 @@ export class Logger {
237237
* Nested groups are not supported.
238238
*
239239
* @example
240-
* await logger.group('Running plugin "ESLint"', async () => {
240+
* const eslintResult = await logger.group('Running plugin "ESLint"', async () => {
241241
* logger.debug('ESLint version is 9.16.0');
242-
* await logger.command('npx eslint . --format=json', () => {
242+
* const result = await logger.command('npx eslint . --format=json', () => {
243243
* // ...
244244
* })
245245
* logger.info('Found 42 lint errors.');
246-
* return 'Completed "ESLint" plugin execution';
246+
* return {
247+
* message: 'Completed "ESLint" plugin execution',
248+
* result,
249+
* };
247250
* });
248251
*
249252
* @param title Display title for the group.
250253
* @param worker Asynchronous implementation. Returned promise determines group status and ending message. Inner logs are attached to the group.
251254
*/
252-
async group(title: string, worker: () => Promise<string>): Promise<void> {
255+
async group<T = undefined>(
256+
title: string,
257+
worker: () => Promise<string | { message: string; result: T }>,
258+
): Promise<T> {
253259
if (this.#groupColor) {
254260
throw new Error(
255261
'Internal Logger error - nested groups are not supported',
@@ -277,10 +283,12 @@ export class Logger {
277283
const end = performance.now();
278284

279285
if (result.status === 'fulfilled') {
286+
const message =
287+
typeof result.value === 'string' ? result.value : result.value.message;
280288
console.log(
281289
[
282290
this.#colorize(this.#groupSymbols.end, this.#groupColor),
283-
this.#colorize(result.value, 'green'),
291+
this.#colorize(message, 'green'),
284292
this.#formatDurationSuffix({ start, end }),
285293
].join(' '),
286294
);
@@ -306,6 +314,11 @@ export class Logger {
306314
if (result.status === 'rejected') {
307315
throw result.reason;
308316
}
317+
318+
if (typeof result.value === 'object') {
319+
return result.value.result;
320+
}
321+
return undefined as T;
309322
}
310323

311324
#createGroupMarkers(): {

testing/test-setup/src/lib/logger.mock.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ beforeAll(async () => {
2929
vi.spyOn(logger, 'newline').mockImplementation(() => {}),
3030
// make sure worker still gets executed
3131
vi.spyOn(logger, 'group').mockImplementation(async (_, worker) => {
32-
await worker();
32+
const value = await worker();
33+
return typeof value === 'object' ? value.result : undefined;
3334
}),
3435
vi.spyOn(logger, 'task').mockImplementation(async (_, worker) => {
3536
await worker();

0 commit comments

Comments
 (0)