UNPKG

@yeoman/adapter

Version:

Input Output adapter for yeoman's generator/environment stack

73 lines 2.27 kB
import process from 'node:process'; import inquirer, { createPromptModule } from 'inquirer'; import chalk from 'chalk'; import { createLogger } from './log.js'; export class TerminalAdapter { stdin; stdout; stderr; console; log; promptModule; abortController = new AbortController(); /** * `TerminalAdapter` is the default implementation of `Adapter`, an abstraction * layer that defines the I/O interactions. * * It provides a CLI interaction * * @constructor * @param {Object} [options] * @param {Console} [options.console] */ constructor(options) { this.stdin = options?.stdin ?? process.stdin; this.stdout = options?.stdout ?? process.stdout; this.stderr = options?.stderr ?? options?.stdout ?? process.stderr; this.console = options?.console ?? new console.Console(this.stdout, this.stderr); this.promptModule = options?.promptModule ?? createPromptModule({ skipTTYChecks: true, input: this.stdin, output: this.stdout, signal: this.abortController.signal, }); this.log = options?.log ?? createLogger(this); } get _colorDiffAdded() { return chalk.black.bgGreen; } get _colorDiffRemoved() { return chalk.bgRed; } _colorLines(name, string) { return string .split('\n') .map(line => this[`_colorDiff${name}`](line)) .join('\n'); } close() { this.abortController.abort(); } /** * Prompt a user for one or more questions and pass * the answer(s) to the provided callback. * * It shares its interface with `Base.prompt` * * (Defined inside the constructor to keep interfaces separated between * instances) * * @param questions * @param answers Answers to be passed to inquirer * @return promise answers */ async prompt(questions, initialAnswers) { return this.promptModule(questions, initialAnswers); } separator(separator) { return new inquirer.Separator(separator); } } //# sourceMappingURL=adapter.js.map