start interaction module
This commit is contained in:
@ -1,59 +0,0 @@
|
||||
import "typings-global";
|
||||
|
||||
import "./smartcli.interfaces";
|
||||
import plugins = require("./smartcli.plugins");
|
||||
|
||||
/**
|
||||
* executes callback with answer to question as argument
|
||||
* @param questionString the question you want to ask the user
|
||||
* @param cb the function to execute with answer as param
|
||||
* @returns {null}
|
||||
*/
|
||||
export let getAnswer = function(questionString:string, cb) {
|
||||
if (typeof questionString != 'string') {
|
||||
plugins.beautylog.error('no question specified');
|
||||
return null;
|
||||
}
|
||||
//make inquirer compatible question object
|
||||
let question = {
|
||||
type: "input",
|
||||
name: "userFeedback",
|
||||
message: questionString,
|
||||
validate: function( value ) {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
plugins.inquirer.prompt([question],function(answers){
|
||||
let answer = answers.userFeedback;
|
||||
cb(answer);
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
*
|
||||
* @param questionString
|
||||
* @param choiceOptions
|
||||
* @param cb
|
||||
* @returns {null}
|
||||
*/
|
||||
export let getChoice = function(questionString:string, choiceOptions:string[], cb) {
|
||||
if(!Array.isArray(choiceOptions)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
//make inquirer compatible question object
|
||||
let question = {
|
||||
type: "list",
|
||||
name: "userFeedback",
|
||||
message: questionString,
|
||||
choices: choiceOptions,
|
||||
filter: function( val ) { return val.toLowerCase(); }
|
||||
};
|
||||
|
||||
plugins.inquirer.prompt(question,function(answers){
|
||||
let answer = answers.userFeedback;
|
||||
cb(answer);
|
||||
});
|
||||
|
||||
};
|
@ -1,8 +1,60 @@
|
||||
import "typings-global";
|
||||
import * as plugins from "./smartcli.plugins";
|
||||
|
||||
export class Question {
|
||||
constructor(){
|
||||
|
||||
/**
|
||||
* allows to specify an user interaction during runtime
|
||||
*/
|
||||
|
||||
export type questionType = "input" | "confirm" | "list" | "rawlist" | "expand" | "checkbox" | "password" | "editor"
|
||||
export interface choiceObject {
|
||||
name: string;
|
||||
value: any
|
||||
}
|
||||
export interface validateFunction {
|
||||
(any):boolean
|
||||
}
|
||||
|
||||
export class Interaction {
|
||||
constructor() {
|
||||
};
|
||||
|
||||
askQuestion(optionsArg: {
|
||||
type: questionType,
|
||||
message: string
|
||||
default: any
|
||||
choices: string[] | choiceObject[];
|
||||
validate: validateFunction
|
||||
}) {
|
||||
let done = plugins.q.defer();
|
||||
plugins.inquirer.prompt([{
|
||||
type: optionsArg.type,
|
||||
message: optionsArg.message,
|
||||
default: optionsArg.default,
|
||||
choices:optionsArg.choices,
|
||||
validate: optionsArg.validate
|
||||
}]).then(answers => {
|
||||
done.resolve(answers);
|
||||
});
|
||||
};
|
||||
askQuestionArray
|
||||
}
|
||||
|
||||
|
||||
export class QuestionTree {
|
||||
|
||||
constructor(questionString: string, optionsArray) {
|
||||
|
||||
};
|
||||
};
|
||||
|
||||
export class QuestionTreeNode {
|
||||
constructor() {
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
export class QuestionStorage {
|
||||
constructor() {
|
||||
|
||||
}
|
||||
}
|
@ -1,7 +1,16 @@
|
||||
import "typings-global";
|
||||
|
||||
import * as plugins from "./smartcli.plugins";
|
||||
import {Question} from "./smartcli.classes.interaction";
|
||||
import * as interaction from "./smartcli.classes.interaction";
|
||||
|
||||
// import classes
|
||||
import {Objectmap} from "lik";
|
||||
|
||||
// interfaces
|
||||
export interface commandPromiseObject {
|
||||
commandName:string;
|
||||
promise: plugins.q.Promise<any>;
|
||||
};
|
||||
|
||||
export class Smartcli {
|
||||
argv;
|
||||
@ -10,6 +19,9 @@ export class Smartcli {
|
||||
commands;
|
||||
questions;
|
||||
version:string;
|
||||
|
||||
// maps
|
||||
allCommandPromises = new Objectmap<commandPromiseObject>();
|
||||
constructor(){
|
||||
this.argv = plugins.argv;
|
||||
this.questionsDone = plugins.q.defer();
|
||||
@ -44,15 +56,28 @@ export class Smartcli {
|
||||
/**
|
||||
* gets a Promise for a command word
|
||||
*/
|
||||
getCommandPromise(commandNameArg){
|
||||
//TODO
|
||||
}
|
||||
addQuestion(definitionArg:{questionString:string,questionType:string}){
|
||||
|
||||
getCommandPromiseByName(commandNameArg:string){
|
||||
return this.allCommandPromises.find(commandPromiseObjectArg => {
|
||||
return commandPromiseObjectArg.commandName === commandNameArg;
|
||||
}).promise;
|
||||
};
|
||||
addHelp(){
|
||||
|
||||
}
|
||||
/**
|
||||
* allows to specify help text to be printed above the rest of the help text
|
||||
*/
|
||||
addHelp(optionsArg:{
|
||||
helpText:string
|
||||
}){
|
||||
this.addCommand({
|
||||
commandName:"help"
|
||||
}).then(argvArg => {
|
||||
plugins.beautylog.log(optionsArg.helpText);
|
||||
})
|
||||
};
|
||||
|
||||
/**
|
||||
* specify version to be printed for -v --version
|
||||
*/
|
||||
addVersion(versionArg:string){
|
||||
this.version = versionArg;
|
||||
this.addAlias("v","version");
|
||||
@ -62,7 +87,11 @@ export class Smartcli {
|
||||
console.log(this.version);
|
||||
}
|
||||
})
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* returns promise that is resolved when no commands are specified
|
||||
*/
|
||||
standardTask(){
|
||||
let done = plugins.q.defer();
|
||||
this.parseStarted.promise
|
||||
|
@ -3,7 +3,7 @@ import "typings-global";
|
||||
export let argv = require('yargs');
|
||||
export import beautylog = require("beautylog");
|
||||
export let cliff = require("cliff");
|
||||
export let inquirer = require("inquirer");
|
||||
export import inquirer = require("inquirer");
|
||||
export import lik = require("lik");
|
||||
export let path = require("path");
|
||||
export import q = require("q");
|
||||
|
Reference in New Issue
Block a user