Skip to content

Commit

Permalink
feat(adapter): update @yeoman/types for progress api.
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima committed Oct 11, 2023
1 parent 2baf818 commit b6085f9
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 19 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion workspaces/adapter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
32 changes: 18 additions & 14 deletions workspaces/adapter/src/queued-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -24,14 +34,7 @@ type QueuedAdapterOptions = TerminalAdapterOptions & {
adapter?: InputOutputAdapter;
};

type ProgressCallback<ReturnType> = (progress: { step: (prefix: string, message: string, ...args: any[]) => void }) => ReturnType;
type ProgressOptions = { disabled?: boolean; name?: string };

export type AdapterWithProgress = QueuedAdapterApi & {
progress<ReturnType>(fn: ProgressCallback<ReturnType>, options?: ProgressOptions): Promise<void | ReturnType>;
};

export class QueuedAdapter implements AdapterWithProgress {
export class QueuedAdapter implements QueuedAdapterApi {
#queue: PQueue;
actualAdapter: InputOutputAdapter;
delta: number;
Expand Down Expand Up @@ -137,15 +140,16 @@ export class QueuedAdapter implements AdapterWithProgress {
* @param options
* @returns
*/
async progress<ReturnType>(
fn: (progress: { step: (prefix: string, message: string, ...args: any[]) => void }) => ReturnType,
options?: { disabled?: boolean; name: string },
): Promise<void | ReturnType> {
async progress<ReturnType>(fn: ProgressCallback<ReturnType>, options?: ProgressOptions): Promise<void | ReturnType> {
// 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 {
Expand Down
5 changes: 2 additions & 3 deletions workspaces/adapter/src/testing/test-adapter.ts
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -94,7 +93,7 @@ export const getConfig = () => ({ ...defaultConfig });
/**
* @experimental
*/
export class TestAdapter<LogType extends Logger = Logger, SpyType = any> implements AdapterWithProgress {
export class TestAdapter<LogType extends Logger = Logger, SpyType = any> implements QueuedAdapter {
promptModule: PromptModule;
diff: any & SpyType;
log: LogType & SpyType;
Expand Down

0 comments on commit b6085f9

Please sign in to comment.