ask-cli-x
Version:
Alexa Skills Kit (ASK) Command Line Interfaces
155 lines (154 loc) • 6.2 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CliReplView = void 0;
const repl_1 = __importDefault(require("repl"));
const spinner_view_1 = __importDefault(require("./spinner-view"));
const messenger_1 = __importDefault(require("./messenger"));
const stringUtils = __importStar(require("../utils/string-utils"));
class CliReplView {
/**
* Constructor for CLI REPL View
* @param {String} prompt What string to display as the prompt for the REPL.
* @throws {Error} throws error if an undefined configuration is passed.
* @throws {Error} throws error if prompt is not a non-empty string.
*/
constructor(configuration) {
if (!configuration) {
throw "Cannot have an undefined configuration.";
}
const { prompt, evalFunc, header, footer, prettifyHeaderFooter, inputStream } = configuration;
this.prompt = prompt || "> ";
this.eval = evalFunc;
this.header = header;
this.footer = footer;
this.prettifyHeaderFooter = prettifyHeaderFooter || ((arg) => arg);
this.inputStream = inputStream || process.stdin;
this.progressSpinner = new spinner_view_1.default();
if (!stringUtils.isNonEmptyString(this.prompt)) {
throw "Prompt must be a non-empty string.";
}
this.printHeaderFooter(this.header);
// Initialize custom REPL server.
const replConfig = {
prompt: this.prompt,
eval: (cmd, _context, _filename, callback) => {
this.eval(cmd, callback);
},
ignoreUndefined: true,
input: this.inputStream,
output: process.stdout,
};
this.replServer = repl_1.default.start(replConfig);
this.replServer.removeAllListeners("SIGINT");
this.clearSpecialCommands();
this.registerQuitCommand(() => { });
}
/**
* Function to print a header or footer line.
* @param {String} data the string that contains the information the caller whats to embed into header/footer.
*/
printHeaderFooter(data) {
if (stringUtils.isNonEmptyString(data)) {
messenger_1.default.getInstance().info(this.prettifyHeaderFooter(data));
}
}
/**
* Register a special command to the repl
* @param {String} name name of new special command.
* @param {String} name name of new special command.
* @param {String} helpMessage description of special command
* @param {Function} func function to execute when special command received
* @param {Boolean} displayPrompt specify whether or not to display the prompt after the special command executes. Default: true
*/
registerSpecialCommand(name, helpMessage, func, displayPrompt) {
var _a;
const shouldDisplayPrompt = displayPrompt === undefined ? true : displayPrompt;
this.replServer.defineCommand(name, {
help: helpMessage || ((_a = this.replServer.commands[name]) === null || _a === void 0 ? void 0 : _a.help),
action: (args) => {
func(args);
if (shouldDisplayPrompt)
this.replServer.displayPrompt();
},
});
}
/**
* Register a special exit command to the repl
* @param {Function} func function to execute when special command received
*/
registerQuitCommand(func) {
this.replServer.removeAllListeners("close");
this.registerSpecialCommand("quit", "Quit repl session.", () => {
this.close();
}, false);
this.replServer.on("close", () => {
func();
this.progressSpinner.terminate();
this.printHeaderFooter(this.footer);
});
}
/**
* Remove all special commands from REPL server
* Remove close event listeners to remove all quit handlers.
*/
clearSpecialCommands() {
this.replServer.commands = {
help: this.replServer.commands.help,
};
this.replServer.removeAllListeners("close");
}
/**
* Wrapper to close instance. This involves terminating the SpinnerView, disposing the Messenger View, and closing the REPL Server.
*/
close() {
this.replServer.close();
}
// SpinnerView wrapper functions
/**
* Wrapper for starting the SpinnerView with a specified message.
* @param {String} text text to display when the spinner starts.
*/
startProgressSpinner(text) {
this.progressSpinner.start(text);
}
/**
* Wrapper for updating the text of the SpinnerView
* @param {String} text text to replace current message of spinner.
*/
updateProgressSpinner(text) {
this.progressSpinner.update(text);
}
/**
* Wrapper for terminating the SpinnerView, clearing it from the console
*/
terminateProgressSpinner() {
this.progressSpinner.terminate();
}
}
exports.CliReplView = CliReplView;