smartcli/ts/smartcli.classes.smartcli.ts

166 lines
4.1 KiB
TypeScript
Raw Normal View History

import * as smartq from 'smartq';
import { Subject } from 'rxjs';
2016-06-10 00:27:04 +00:00
import * as plugins from './smartcli.plugins';
2016-08-26 09:52:09 +00:00
// import classes
import { Objectmap } from '@pushrocks/lik';
2016-08-26 09:52:09 +00:00
// interfaces
2016-12-18 19:53:50 +00:00
export interface ICommandPromiseObject {
commandName: string;
promise: Promise<void>;
2016-12-18 19:53:50 +00:00
}
export interface ITriggerObservableObject {
triggerName: string;
subject: Subject<any>;
2016-10-14 22:56:02 +00:00
}
/**
* class to create a new instance of Smartcli. Handles parsing of command line arguments.
*/
2016-06-10 00:27:04 +00:00
export class Smartcli {
argv: any;
questionsDone;
parseStarted: smartq.Deferred<any>;
commands;
questions;
version: string;
private onlyOnProcessEnvCliCall = false;
2017-04-22 19:03:28 +00:00
/**
* map of all Trigger/Observable objects to keep track
2017-04-22 19:03:28 +00:00
*/
allTriggerObservablesMap = new Objectmap<ITriggerObservableObject>();
2017-04-22 19:03:28 +00:00
/**
* The constructor of Smartcli
2017-04-22 19:03:28 +00:00
*/
constructor() {
this.argv = plugins.yargs;
this.questionsDone = smartq.defer();
this.parseStarted = smartq.defer();
2017-04-22 19:03:28 +00:00
}
/**
* halts any execution of commands if (process.env.CLI_CALL === false)
*/
onlyTriggerOnProcessEnvCliCall() {
this.onlyOnProcessEnvCliCall = true;
}
2017-04-22 19:03:28 +00:00
/**
* adds an alias, meaning one equals the other in terms of command execution.
*/
addCommandAlias(keyArg, aliasArg): void {
this.argv = this.argv.alias(keyArg, aliasArg);
return;
2017-04-22 19:03:28 +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.
*/
addCommand(commandNameArg: string): Subject<any> {
let triggerSubject = this.addTrigger(commandNameArg);
this.parseStarted.promise.then(() => {
if (this.argv._.indexOf(commandNameArg) === 0) {
this.trigger(commandNameArg);
}
});
return triggerSubject;
2017-04-22 19:03:28 +00:00
}
/**
2017-10-12 18:38:34 +00:00
* adds a Trigger. Like addCommand(), but returns an subscribable observable
2017-04-22 19:03:28 +00:00
*/
addTrigger(triggerNameArg: string) {
let triggerSubject = new Subject<any>();
if (!this.getTriggerSubject(triggerNameArg)) {
this.allTriggerObservablesMap.add({
triggerName: triggerNameArg,
subject: triggerSubject
});
} else {
throw new Error(`you can't add a trigger twice`);
}
return triggerSubject;
2017-04-22 19:03:28 +00:00
}
/**
* execute trigger by name
* @param commandNameArg - the name of the command to trigger
*/
trigger(triggerName: string) {
let triggerSubject = this.getTriggerSubject(triggerName);
triggerSubject.next(this.argv);
return triggerSubject;
}
getTriggerSubject(triggerName: string) {
const triggerObservableObject = this.allTriggerObservablesMap.find(
triggerObservableObjectArg => {
return triggerObservableObjectArg.triggerName === triggerName;
}
);
if (triggerObservableObject) {
return triggerObservableObject.subject;
} else {
return null;
}
2017-04-22 19:03:28 +00:00
}
/**
* allows to specify help text to be printed above the rest of the help text
*/
addHelp(optionsArg: { helpText: string }) {
this.addCommand('help').subscribe(argvArg => {
plugins.smartlog.defaultLogger.info(optionsArg.helpText);
});
2017-04-22 19:03:28 +00:00
}
/**
* specify version to be printed for -v --version
*/
addVersion(versionArg: string) {
this.version = versionArg;
this.addCommandAlias('v', 'version');
this.parseStarted.promise.then(() => {
if (this.argv.v) {
console.log(this.version);
}
});
2017-04-22 19:03:28 +00:00
}
/**
* adds a trigger that is called when no command is specified
2017-04-22 19:03:28 +00:00
*/
standardTask(): Subject<any> {
let standardSubject = this.addTrigger('standardTask');
this.parseStarted.promise.then(() => {
if (this.argv._.length === 0 && !this.argv.v) {
if (this.onlyOnProcessEnvCliCall) {
if (process.env.CLI_CALL === 'true') {
this.trigger('standardTask');
} else {
return;
}
} else {
this.trigger('standardTask');
2017-04-22 19:03:28 +00:00
}
}
});
return standardSubject;
2017-04-22 19:03:28 +00:00
}
/**
* start the process of evaluating commands
*/
startParse(): void {
this.argv = this.argv.argv;
this.parseStarted.resolve();
return;
2017-04-22 19:03:28 +00:00
}
2016-10-14 22:56:02 +00:00
}