@yeoman/adapter
Version:
Input Output adapter for yeoman's generator/environment stack
85 lines • 2.58 kB
JavaScript
import process from 'node:process';
import console from 'node:console';
import chalk from 'chalk';
import { Separator } from '@inquirer/core';
import { createLogger } from './log.js';
import { createAdapterPromptModule } from './inquirer.js';
export class TerminalAdapter {
stdin;
stdout;
stderr;
console;
log;
promptModule;
abortController = new AbortController();
signal = this.abortController.signal;
/**
* `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 ??
createAdapterPromptModule({
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.abort();
}
abort(reason) {
this.abortController.abort(reason);
}
/**
* 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) {
try {
return (await this.promptModule(questions, initialAnswers));
}
catch (error) {
this.abortController.abort(error);
throw error;
}
}
separator(separator) {
return new Separator(separator);
}
}
//# sourceMappingURL=adapter.js.map