UNPKG

@sudoo/coco

Version:

:ocean: A simple command line tool framework

59 lines (58 loc) 1.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const readline_1 = require("readline"); const declare_1 = require("../panic/declare"); class Reverse { constructor(stdin, stdout) { this._in = stdin; this._out = stdout; this._useEnvironmentVariable = false; } static create(stdin = process.stdin, stdout = process.stdout) { return new Reverse(stdin, stdout); } get isTTY() { const isInTTY = this._in.isTTY || false; const isOutTTY = this._out.isTTY || false; return isInTTY && isOutTTY; } get isUsingEnvironmentVariable() { return this._useEnvironmentVariable; } useEnvironmentVariable() { this._useEnvironmentVariable = true; return this; } useReadline() { this._useEnvironmentVariable = false; return this; } question(question, envVariable) { return new Promise((resolve, reject) => { if (this.isUsingEnvironmentVariable) { if (!envVariable) { reject(declare_1.panic.code(declare_1.ERROR_CODE.NO_ENVIRONMENT_VARIABLE_ASSIGNED)); return; } const value = process.env[envVariable]; if (value) { resolve(value); } else { reject(declare_1.panic.code(declare_1.ERROR_CODE.NO_TARGET_ENVIRONMENT_VARIABLE_FOUND, envVariable)); } return; } const readline = readline_1.createInterface({ input: this._in, output: this._out, }); readline.question(question, (answer) => { readline.close(); resolve(answer); return; }); }); } } exports.Reverse = Reverse;