smartcli/ts/smartcli.classes.smartcli.ts

164 lines
4.3 KiB
TypeScript
Raw Normal View History

2016-10-14 22:56:02 +00:00
import * as q from 'q'
2016-12-18 19:53:50 +00:00
import { Subject } from 'rxjs'
2016-06-10 00:27:04 +00:00
2016-10-14 22:56:02 +00:00
import * as plugins from './smartcli.plugins'
2016-08-26 09:52:09 +00:00
// import classes
2016-12-18 19:53:50 +00:00
import { Objectmap } from 'lik'
2016-08-26 09:52:09 +00:00
// interfaces
2016-12-18 19:53:50 +00:00
export interface ICommandPromiseObject {
commandName: string,
promise: q.Promise<void>
}
export interface ITriggerObservableObject {
triggerName: string
subject: Subject<void>
2016-10-14 22:56:02 +00:00
}
2016-06-10 00:27:04 +00:00
export class Smartcli {
2016-10-14 22:56:02 +00:00
argv: any
questionsDone
parseStarted
commands
questions
version: string
2016-08-26 09:52:09 +00:00
2016-12-18 00:36:19 +00:00
/**
* map of all Command/Promise objects to keep track
*/
2016-12-18 19:53:50 +00:00
allCommandPromisesMap = new Objectmap<ICommandPromiseObject>()
/**
* map of all Trigger/Observable objects to keep track
*/
allTriggerObservablesMap = new Objectmap<ITriggerObservableObject>()
2016-12-18 00:36:19 +00:00
2016-10-14 22:56:02 +00:00
constructor() {
this.argv = plugins.yargs
this.questionsDone = q.defer()
this.parseStarted = q.defer()
}
2016-08-26 07:21:42 +00:00
/**
2016-12-18 19:53:50 +00:00
* adds an alias, meaning one equals the other in terms of command execution.
2016-08-26 07:21:42 +00:00
*/
2016-12-18 19:53:50 +00:00
addCommandAlias(keyArg,aliasArg): void {
2016-10-14 22:56:02 +00:00
this.argv = this.argv.alias(keyArg,aliasArg)
return
}
2016-08-26 07:21:42 +00:00
/**
* 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.
*/
2016-12-18 19:53:50 +00:00
addCommand(commandNameArg: string): q.Promise<any> {
2016-10-14 22:56:02 +00:00
let done = q.defer<any>()
2016-12-18 19:53:50 +00:00
this.allCommandPromisesMap.add({
commandName: commandNameArg,
promise: done.promise
2016-12-18 00:36:19 +00:00
})
2016-06-10 02:13:23 +00:00
this.parseStarted.promise
.then(() => {
2016-12-18 19:53:50 +00:00
if (this.argv._.indexOf(commandNameArg) === 0) {
2016-10-14 22:56:02 +00:00
done.resolve(this.argv)
} else {
2016-10-14 22:56:02 +00:00
done.reject(this.argv)
}
2016-10-14 22:56:02 +00:00
})
return done.promise
}
2016-08-26 07:21:42 +00:00
/**
* gets a Promise for a command word
*/
2016-12-18 00:36:19 +00:00
getCommandPromiseByName(commandNameArg: string): q.Promise<void> {
2016-12-18 19:53:50 +00:00
return this.allCommandPromisesMap.find(commandDeferredObjectArg => {
2016-12-18 00:36:19 +00:00
return commandDeferredObjectArg.commandName === commandNameArg
2016-12-18 19:53:50 +00:00
}).promise
2016-12-18 00:36:19 +00:00
}
/**
2016-12-18 19:53:50 +00:00
* adds a Trigger. Like addCommand(), but returns an subscribable observable
*/
addTrigger(triggerNameArg: string) {
let triggerSubject = new Subject<void>()
this.allTriggerObservablesMap.add({
triggerName: triggerNameArg,
subject: triggerSubject
})
this.addCommand(triggerNameArg).then(() => {
triggerSubject.next()
})
return triggerSubject
}
/**
* execute trigger by name
2016-12-18 00:36:19 +00:00
* @param commandNameArg - the name of the command to trigger
*/
2016-12-18 19:53:50 +00:00
trigger(triggerName: string) {
let triggerSubject = this.allTriggerObservablesMap.find(triggerObservableObjectArg => {
return triggerObservableObjectArg.triggerName === triggerName
}).subject
triggerSubject.next()
return triggerSubject
2016-10-14 22:56:02 +00:00
}
2016-06-13 10:57:24 +00:00
2016-08-26 09:52:09 +00:00
/**
* allows to specify help text to be printed above the rest of the help text
*/
2016-10-14 22:56:02 +00:00
addHelp(optionsArg: {
helpText: string
}) {
2016-12-18 19:53:50 +00:00
this.addCommand('help').then(argvArg => {
2016-10-14 22:56:02 +00:00
plugins.beautylog.log(optionsArg.helpText)
2016-08-26 09:52:09 +00:00
})
2016-10-14 22:56:02 +00:00
}
2016-08-26 09:52:09 +00:00
/**
* specify version to be printed for -v --version
*/
2016-10-14 22:56:02 +00:00
addVersion(versionArg: string) {
this.version = versionArg
2016-12-18 19:53:50 +00:00
this.addCommandAlias('v','version')
2016-06-10 02:13:23 +00:00
this.parseStarted.promise
.then(() => {
2016-10-14 22:56:02 +00:00
if (this.argv.v) {
console.log(this.version)
2016-06-10 02:13:23 +00:00
}
})
2016-10-14 22:56:02 +00:00
}
2016-08-26 09:52:09 +00:00
/**
* returns promise that is resolved when no commands are specified
*/
2016-10-14 22:56:02 +00:00
standardTask(): q.Promise<any> {
let done = q.defer<any>()
2016-12-18 19:53:50 +00:00
this.allCommandPromisesMap.add({
commandName: 'standard',
promise: done.promise
})
2016-06-10 02:13:23 +00:00
this.parseStarted.promise
.then(() => {
2016-10-14 22:56:02 +00:00
if (this.argv._.length === 0 && !this.argv.v) {
done.resolve(this.argv)
} else {
2016-10-14 22:56:02 +00:00
done.reject(this.argv)
}
})
return done.promise
}
2016-09-04 15:32:12 +00:00
/**
* start the process of evaluating commands
*/
2016-10-14 22:56:02 +00:00
startParse(): void {
this.argv = this.argv.argv
this.parseStarted.resolve()
return
}
2016-10-14 22:56:02 +00:00
}