implement standardJS
This commit is contained in:
@ -1,4 +1 @@
|
||||
import "typings-global";
|
||||
|
||||
import {Smartcli} from "./smartcli.classes.smartcli";
|
||||
export {Smartcli} from "./smartcli.classes.smartcli";
|
||||
export { Smartcli } from './smartcli.classes.smartcli'
|
||||
|
@ -1,41 +1,40 @@
|
||||
import "typings-global";
|
||||
import * as plugins from "./smartcli.plugins";
|
||||
import * as plugins from './smartcli.plugins'
|
||||
|
||||
/**
|
||||
* allows to specify an user interaction during runtime
|
||||
*/
|
||||
|
||||
export type questionType = "input" | "confirm" | "list" | "rawlist" | "expand" | "checkbox" | "password" | "editor"
|
||||
export interface choiceObject {
|
||||
name: string;
|
||||
export type questionType = 'input' | 'confirm' | 'list' | 'rawlist' | 'expand' | 'checkbox' | 'password' | 'editor'
|
||||
export interface IChoiceObject {
|
||||
name: string
|
||||
value: any
|
||||
}
|
||||
export interface validateFunction {
|
||||
(any):boolean
|
||||
export interface IValidateFunction {
|
||||
(any): boolean
|
||||
}
|
||||
|
||||
export class Interaction {
|
||||
constructor() {
|
||||
};
|
||||
}
|
||||
|
||||
askQuestion(optionsArg: {
|
||||
type: questionType,
|
||||
message: string
|
||||
default: any
|
||||
choices: string[] | choiceObject[];
|
||||
validate: validateFunction
|
||||
choices: string[] | IChoiceObject[]
|
||||
validate: IValidateFunction
|
||||
}) {
|
||||
let done = plugins.q.defer();
|
||||
let done = plugins.q.defer()
|
||||
plugins.inquirer.prompt([{
|
||||
type: optionsArg.type,
|
||||
message: optionsArg.message,
|
||||
default: optionsArg.default,
|
||||
choices:optionsArg.choices,
|
||||
choices: optionsArg.choices,
|
||||
validate: optionsArg.validate
|
||||
}]).then(answers => {
|
||||
done.resolve(answers);
|
||||
});
|
||||
};
|
||||
done.resolve(answers)
|
||||
})
|
||||
}
|
||||
askQuestionArray
|
||||
}
|
||||
|
||||
@ -44,17 +43,17 @@ export class QuestionTree {
|
||||
|
||||
constructor(questionString: string, optionsArray) {
|
||||
|
||||
};
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export class QuestionTreeNode {
|
||||
constructor() {
|
||||
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export class QuestionStorage {
|
||||
constructor() {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,118 +1,117 @@
|
||||
import "typings-global";
|
||||
import * as q from 'q'
|
||||
|
||||
import * as plugins from "./smartcli.plugins";
|
||||
import * as interaction from "./smartcli.classes.interaction";
|
||||
import * as plugins from './smartcli.plugins'
|
||||
import * as interaction from './smartcli.classes.interaction'
|
||||
|
||||
// import classes
|
||||
import {Objectmap} from "lik";
|
||||
import {Objectmap} from 'lik'
|
||||
|
||||
// interfaces
|
||||
export interface commandPromiseObject {
|
||||
commandName:string;
|
||||
promise: plugins.q.Promise<any>;
|
||||
};
|
||||
export interface ICommandPromiseObject {
|
||||
commandName: string
|
||||
promise: q.Promise<any>
|
||||
}
|
||||
|
||||
export class Smartcli {
|
||||
argv:any;
|
||||
questionsDone;
|
||||
parseStarted;
|
||||
commands;
|
||||
questions;
|
||||
version:string;
|
||||
argv: any
|
||||
questionsDone
|
||||
parseStarted
|
||||
commands
|
||||
questions
|
||||
version: string
|
||||
|
||||
// maps
|
||||
allCommandPromises = new Objectmap<commandPromiseObject>();
|
||||
constructor(){
|
||||
this.argv = plugins.yargs;
|
||||
this.questionsDone = plugins.q.defer();
|
||||
this.parseStarted = plugins.q.defer();
|
||||
};
|
||||
allCommandPromises = new Objectmap<ICommandPromiseObject>()
|
||||
constructor() {
|
||||
this.argv = plugins.yargs
|
||||
this.questionsDone = q.defer()
|
||||
this.parseStarted = q.defer()
|
||||
}
|
||||
|
||||
/**
|
||||
* adds an alias, meaning one equals the other in terms of triggering associated commands
|
||||
*/
|
||||
addAlias(keyArg,aliasArg):void {
|
||||
this.argv = this.argv.alias(keyArg,aliasArg);
|
||||
return;
|
||||
};
|
||||
addAlias(keyArg,aliasArg): void {
|
||||
this.argv = this.argv.alias(keyArg,aliasArg)
|
||||
return
|
||||
}
|
||||
|
||||
/**
|
||||
* adds a Command by returning a Promise that reacts to the specific commandString given.
|
||||
*
|
||||
* Note: in e.g. "npm install something" the "install" is considered the command.
|
||||
*/
|
||||
addCommand(definitionArg:{commandName:string}):plugins.q.Promise<any>{
|
||||
let done = plugins.q.defer<any>();
|
||||
addCommand(definitionArg: {commandName: string}): q.Promise<any> {
|
||||
let done = q.defer<any>()
|
||||
this.parseStarted.promise
|
||||
.then(() => {
|
||||
if (this.argv._.indexOf(definitionArg.commandName) == 0) {
|
||||
done.resolve(this.argv);
|
||||
if (this.argv._.indexOf(definitionArg.commandName) === 0) {
|
||||
done.resolve(this.argv)
|
||||
} else {
|
||||
done.reject(this.argv);
|
||||
done.reject(this.argv)
|
||||
}
|
||||
});
|
||||
return done.promise;
|
||||
};
|
||||
})
|
||||
return done.promise
|
||||
}
|
||||
|
||||
/**
|
||||
* gets a Promise for a command word
|
||||
*/
|
||||
getCommandPromiseByName(commandNameArg:string){
|
||||
getCommandPromiseByName(commandNameArg: string) {
|
||||
return this.allCommandPromises.find(commandPromiseObjectArg => {
|
||||
return commandPromiseObjectArg.commandName === commandNameArg;
|
||||
}).promise;
|
||||
};
|
||||
return commandPromiseObjectArg.commandName === commandNameArg
|
||||
}).promise
|
||||
}
|
||||
|
||||
/**
|
||||
* allows to specify help text to be printed above the rest of the help text
|
||||
*/
|
||||
addHelp(optionsArg:{
|
||||
helpText:string
|
||||
}){
|
||||
addHelp(optionsArg: {
|
||||
helpText: string
|
||||
}) {
|
||||
this.addCommand({
|
||||
commandName:"help"
|
||||
commandName: 'help'
|
||||
}).then(argvArg => {
|
||||
plugins.beautylog.log(optionsArg.helpText);
|
||||
plugins.beautylog.log(optionsArg.helpText)
|
||||
})
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* specify version to be printed for -v --version
|
||||
*/
|
||||
addVersion(versionArg:string){
|
||||
this.version = versionArg;
|
||||
this.addAlias("v","version");
|
||||
addVersion(versionArg: string) {
|
||||
this.version = versionArg
|
||||
this.addAlias('v','version')
|
||||
this.parseStarted.promise
|
||||
.then(() => {
|
||||
if(this.argv.v){
|
||||
console.log(this.version);
|
||||
if (this.argv.v) {
|
||||
console.log(this.version)
|
||||
}
|
||||
})
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* returns promise that is resolved when no commands are specified
|
||||
*/
|
||||
standardTask():plugins.q.Promise<any>{
|
||||
let done = plugins.q.defer<any>();
|
||||
standardTask(): q.Promise<any> {
|
||||
let done = q.defer<any>()
|
||||
this.parseStarted.promise
|
||||
.then(() => {
|
||||
if(this.argv._.length == 0 && !this.argv.v){
|
||||
done.resolve(this.argv);
|
||||
if (this.argv._.length === 0 && !this.argv.v) {
|
||||
done.resolve(this.argv)
|
||||
} else {
|
||||
done.reject(this.argv);
|
||||
};
|
||||
});
|
||||
return done.promise;
|
||||
done.reject(this.argv)
|
||||
}
|
||||
})
|
||||
return done.promise
|
||||
}
|
||||
|
||||
/**
|
||||
* start the process of evaluating commands
|
||||
*/
|
||||
startParse():void{
|
||||
this.argv = this.argv.argv;
|
||||
this.parseStarted.resolve();
|
||||
return;
|
||||
startParse(): void {
|
||||
this.argv = this.argv.argv
|
||||
this.parseStarted.resolve()
|
||||
return
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -1,10 +1,18 @@
|
||||
import "typings-global";
|
||||
import 'typings-global';
|
||||
|
||||
export import yargs = require('yargs');
|
||||
export import beautylog = require("beautylog");
|
||||
export import cliff = require("cliff");
|
||||
export import inquirer = require("inquirer");
|
||||
export import lik = require("lik");
|
||||
export import path = require("path");
|
||||
export import q = require("q");
|
||||
export import smartparam = require("smartparam");
|
||||
import * as yargs from 'yargs'
|
||||
import * as beautylog from 'beautylog'
|
||||
import * as inquirer from 'inquirer'
|
||||
import * as lik from 'lik'
|
||||
import * as path from 'path'
|
||||
import * as smartparam from 'smartparam'
|
||||
|
||||
export {
|
||||
yargs,
|
||||
beautylog,
|
||||
cliff,
|
||||
inquirer,
|
||||
lik,
|
||||
path,
|
||||
smartparam
|
||||
}
|
||||
|
Reference in New Issue
Block a user