@kwaeri/wizard
Version:
The @kwaeri/wizard component module of the @kwaeri/node-kit platform.
131 lines (130 loc) • 5.51 kB
JavaScript
/*-----------------------------------------------------------------------------
* @package: node-kit wizard
* @author: Richard B Winters
* @copyright: 2014-2020 Massively Modified, Inc.
* @license: Apache-2.0 <http://www.apache.org/licenses/LICENSE-2.0>
* @version: 0.2.0
*---------------------------------------------------------------------------*/
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ExampleWizard = exports.WizardServiceProvider = exports.WizardService = void 0;
// INCLUDES
const rl = require("readline");
const Event = require("events");
const debug_1 = require("debug");
// DEFINES
/* Configure Debug module support */
let DEBUG = debug_1.default('nkm:wizard');
/* Our UserPrompt Interface */
let readline = rl;
/**
* WizardService Class
*
* The { WizardService } is the class from which all wizard services should inherit
* from. A wizard service is one that prompts users for information, and will [or should]
* always need the readline class, and have at least a pair of methods 'run' and `question1`.
*
* This class helps to sensibly build a derived service provider, it should not be
* used directly. Instead, developers should extend from the WizardServiceProvider class.
*/
class WizardService extends Event.EventEmitter {
constructor() {
super();
}
setServiceEventMetadata(data) {
this.emit('ServiceEvent', data);
}
}
exports.WizardService = WizardService;
/**
* WizardServiceProvider Class
*
* The { WizardServiceProvider } class implements a facility for presenting
* a series of user promopts via CLI, in order to gather information
* for a task, much in the same fashion as that of a wizard.
*/
class WizardServiceProvider extends WizardService {
/**
* Class constructor
*
* @param { void }
*
* @since 0.1.10
*/
constructor() {
super();
}
/**
* A method which runs a sequence of user prompts through a CLI
*
* @param { FilesystemDescriptor } options Any options that might have been provided preemptively
*
* @return { Promise<WizardResponsePromise> } An array of answers
*
* @since 0.1.10
*/
run(options) {
return __awaiter(this, void 0, void 0, function* () {
// Any time a Wizard is run, we need a readline interface:
let userPrompt = readline.createInterface({
input: process.stdin,
output: process.stdout
});
// As well as an answers array:
let answerArray = [];
DEBUG(`Call user prompts`);
let answers = yield this.question1(userPrompt, answerArray, options);
// Start the wizard:
return Promise.resolve(answers);
});
}
/**
* A method which implements the first user prompt for a setup wizard that is
* run for assisting the automaton with generating file contents.
*
* @param { rl.ReadLine } instance An instance of readline.createInterface()
* @param { Array<string> } answers An array filled with answers from the automaton project generator wizard.
* @param { Array<Function> } prompts An array of user prompt functions
* @param { FilesystemDescriptor } options An object with options for specifying
*
* @return { Promise<any> } Returns a promise for allowing asynchronous chaining
*
* @since 0.1.10
*/
question1(instance, answers, options) {
return __awaiter(this, void 0, void 0, function* () {
instance.question(`This is a @kwaeri/node-kit wizard example user-prompt question. Use as many pro-\n` +
`mpts as you'd like, simply assign the next prompts resolved promise as this pro-\n` +
`mpts resolved promise. You can chain them in that fashion, passing along requir-\n` +
`ed arguments.\n\n` +
`Press [Enter] to continue...`, (answer) => __awaiter(this, void 0, void 0, function* () {
console.log('You continued...: ', answer);
instance.close();
return Promise.resolve(answer);
}));
});
}
}
exports.WizardServiceProvider = WizardServiceProvider;
// Example of a Wizard Service Provider
class ExampleWizard extends WizardServiceProvider {
getServiceProviderSubscriptions(options) {
return { commands: {}, required: {}, optional: {}, subcommands: {} };
}
getServiceProviderSubscriptionHelpText(options) {
return { helpText: { "command": `To use this service, read this HelpText.` } };
}
testEvents(handler) {
this.on('ServiceEvent', (data) => { DEBUG(`TestServiceEvent Caught and Handled!`); handler(data); });
}
}
exports.ExampleWizard = ExampleWizard;