Skip to content

Commit e0793ee

Browse files
committed
feat(utils): support custom return values in logger.task
1 parent 52266a7 commit e0793ee

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,23 @@ ${ansis.green('✔')} Uploaded report to portal ${ansis.gray('(42 ms)')}
376376
);
377377
});
378378

379+
it('should resolve value from worker', async () => {
380+
const config = { plugins: ['eslint'] };
381+
382+
await expect(
383+
new Logger().task('Loading configuration', async () => ({
384+
message: 'Loaded configuration',
385+
result: config,
386+
})),
387+
).resolves.toBe(config);
388+
389+
expect(stdout).toBe(
390+
`
391+
${ansis.green('✔')} Loaded configuration ${ansis.gray('(42 ms)')}
392+
`.trimStart(),
393+
);
394+
});
395+
379396
it('should allow spinners to be run in sequence', async () => {
380397
performanceNowSpy
381398
.mockReset()

packages/utils/src/lib/logger.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,12 +212,16 @@ export class Logger {
212212
* @param title Display text used as pending message.
213213
* @param worker Asynchronous implementation. Returned promise determines spinner status and final message. Support for inner logs has some limitations (described above).
214214
*/
215-
async task(title: string, worker: () => Promise<string>): Promise<void> {
216-
await this.#spinner(worker, {
215+
async task<T = undefined>(
216+
title: string,
217+
worker: () => Promise<string | { message: string; result: T }>,
218+
): Promise<T> {
219+
const result = await this.#spinner(worker, {
217220
pending: title,
218-
success: value => value,
221+
success: value => (typeof value === 'string' ? value : value.message),
219222
failure: error => `${title}${ansis.red(String(error))}`,
220223
});
224+
return typeof result === 'object' ? result.result : (undefined as T);
221225
}
222226

223227
/**

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ beforeAll(async () => {
3535
return typeof value === 'object' ? value.result : undefined;
3636
}),
3737
vi.spyOn(logger, 'task').mockImplementation(async (_, worker) => {
38-
await worker();
38+
const value = await worker();
39+
return typeof value === 'object' ? value.result : undefined;
3940
}),
4041
vi.spyOn(logger, 'command').mockImplementation((_, worker) => worker()),
4142
);

0 commit comments

Comments
 (0)