From b6085f90cf9a43c12e0e1f659d91c999e8d15f50 Mon Sep 17 00:00:00 2001 From: Marcelo Shima Date: Mon, 9 Oct 2023 22:56:56 -0300 Subject: [PATCH] feat(adapter): update @yeoman/types for progress api. --- package-lock.json | 2 +- workspaces/adapter/package.json | 2 +- workspaces/adapter/src/queued-adapter.ts | 32 +++++++++++-------- .../adapter/src/testing/test-adapter.ts | 5 ++- 4 files changed, 22 insertions(+), 19 deletions(-) diff --git a/package-lock.json b/package-lock.json index 3bcde1c..d97a202 100644 --- a/package-lock.json +++ b/package-lock.json @@ -18013,7 +18013,7 @@ "node": "^16.13.0 || >=18.12.0" }, "peerDependencies": { - "@yeoman/types": "^1.0.0" + "@yeoman/types": "^1.1.0" } }, "workspaces/adapter/node_modules/chalk": { diff --git a/workspaces/adapter/package.json b/workspaces/adapter/package.json index eba0772..613787f 100644 --- a/workspaces/adapter/package.json +++ b/workspaces/adapter/package.json @@ -51,7 +51,7 @@ "yeoman-assert": "^3.1.1" }, "peerDependencies": { - "@yeoman/types": "^1.0.0" + "@yeoman/types": "^1.1.0" }, "engines": { "node": "^16.13.0 || >=18.12.0" diff --git a/workspaces/adapter/src/queued-adapter.ts b/workspaces/adapter/src/queued-adapter.ts index c4785c7..d3f3a9a 100644 --- a/workspaces/adapter/src/queued-adapter.ts +++ b/workspaces/adapter/src/queued-adapter.ts @@ -2,9 +2,19 @@ import process from 'node:process'; import { format } from 'node:util'; import ora, { type Ora } from 'ora'; import PQueue from 'p-queue'; -import type { Logger, InputOutputAdapter, PromptAnswers, PromptQuestions, QueuedAdapter as QueuedAdapterApi } from '@yeoman/types'; +import type { + Logger, + InputOutputAdapter, + PromptAnswers, + PromptQuestions, + ProgressCallback, + ProgressOptions, + QueuedAdapter as QueuedAdapterApi, +} from '@yeoman/types'; import { TerminalAdapter, type TerminalAdapterOptions } from './adapter.js'; +export type AdapterWithProgress = QueuedAdapterApi; + // eslint-disable-next-line @typescript-eslint/naming-convention const BLOCKING_PRIORITY = 10; // eslint-disable-next-line @typescript-eslint/naming-convention @@ -24,14 +34,7 @@ type QueuedAdapterOptions = TerminalAdapterOptions & { adapter?: InputOutputAdapter; }; -type ProgressCallback = (progress: { step: (prefix: string, message: string, ...args: any[]) => void }) => ReturnType; -type ProgressOptions = { disabled?: boolean; name?: string }; - -export type AdapterWithProgress = QueuedAdapterApi & { - progress(fn: ProgressCallback, options?: ProgressOptions): Promise; -}; - -export class QueuedAdapter implements AdapterWithProgress { +export class QueuedAdapter implements QueuedAdapterApi { #queue: PQueue; actualAdapter: InputOutputAdapter; delta: number; @@ -137,15 +140,16 @@ export class QueuedAdapter implements AdapterWithProgress { * @param options * @returns */ - async progress( - fn: (progress: { step: (prefix: string, message: string, ...args: any[]) => void }) => ReturnType, - options?: { disabled?: boolean; name: string }, - ): Promise { + async progress(fn: ProgressCallback, options?: ProgressOptions): Promise { // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing if (this.#queue.size > 0 || this.#queue.pending > 0 || options?.disabled || this.#ora.isSpinning) { // Don't show progress if queue is not empty or already spinning. // eslint-disable-next-line @typescript-eslint/no-empty-function - return fn({ step() {} }); + return Promise.resolve(fn({ step() {} })).finally(() => { + if (options?.name) { + this.log.ok(options.name); + } + }); } try { diff --git a/workspaces/adapter/src/testing/test-adapter.ts b/workspaces/adapter/src/testing/test-adapter.ts index 9925280..587e087 100644 --- a/workspaces/adapter/src/testing/test-adapter.ts +++ b/workspaces/adapter/src/testing/test-adapter.ts @@ -1,11 +1,10 @@ /* eslint-disable @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-redundant-type-constituents */ import events from 'node:events'; import { PassThrough } from 'node:stream'; -import type { PromptAnswers, PromptQuestion, Logger, PromptQuestions, Task } from '@yeoman/types'; +import type { PromptAnswers, PromptQuestion, Logger, PromptQuestions, Task, QueuedAdapter } from '@yeoman/types'; import { createPromptModule, type PromptModule } from 'inquirer'; import { createLogger } from '../log.js'; -import { type AdapterWithProgress } from '../queued-adapter.js'; export type DummyPromptCallback = (answer: any, { question, answers }: { question: PromptQuestion; answers: PromptAnswers }) => any; @@ -94,7 +93,7 @@ export const getConfig = () => ({ ...defaultConfig }); /** * @experimental */ -export class TestAdapter implements AdapterWithProgress { +export class TestAdapter implements QueuedAdapter { promptModule: PromptModule; diff: any & SpyType; log: LogType & SpyType;