2022-08-03 15:00:36 +00:00
|
|
|
import * as plugins from './smartcli.plugins.js';
|
2016-08-26 09:52:09 +00:00
|
|
|
|
|
|
|
// interfaces
|
2022-08-03 15:00:36 +00:00
|
|
|
export interface ICommandObservableObject {
|
2018-05-03 10:10:39 +00:00
|
|
|
commandName: string;
|
2022-08-03 15:00:36 +00:00
|
|
|
subject: plugins.smartrx.rxjs.Subject<any>;
|
2016-10-14 22:56:02 +00:00
|
|
|
}
|
2016-06-10 01:10:21 +00:00
|
|
|
|
2021-04-07 20:25:17 +00:00
|
|
|
const logger = new plugins.smartlog.ConsoleLog();
|
|
|
|
|
2018-05-03 10:10:39 +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 {
|
2022-08-03 15:00:36 +00:00
|
|
|
/**
|
|
|
|
* this Deferred contains the parsed result in the end
|
|
|
|
*/
|
|
|
|
public parseCompleted = plugins.smartpromise.defer<any>();
|
2022-08-03 15:07:11 +00:00
|
|
|
|
2022-08-03 15:00:36 +00:00
|
|
|
public version: string;
|
2017-04-22 19:03:28 +00:00
|
|
|
|
|
|
|
/**
|
2018-05-03 10:10:39 +00:00
|
|
|
* map of all Trigger/Observable objects to keep track
|
2017-04-22 19:03:28 +00:00
|
|
|
*/
|
2022-08-03 15:00:36 +00:00
|
|
|
private commandObservableMap = new plugins.lik.ObjectMap<ICommandObservableObject>();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* maps alias
|
|
|
|
*/
|
2022-08-03 15:07:11 +00:00
|
|
|
public aliasObject: { [key: string]: string[] } = {};
|
2017-04-22 19:03:28 +00:00
|
|
|
|
|
|
|
/**
|
2018-05-03 10:10:39 +00:00
|
|
|
* The constructor of Smartcli
|
2017-04-22 19:03:28 +00:00
|
|
|
*/
|
2022-08-03 15:00:36 +00:00
|
|
|
constructor() {}
|
2017-04-22 19:03:28 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* adds an alias, meaning one equals the other in terms of command execution.
|
|
|
|
*/
|
2022-08-03 15:07:11 +00:00
|
|
|
public addCommandAlias(originalArg: string, aliasArg: string): void {
|
2022-08-03 15:00:36 +00:00
|
|
|
this.aliasObject[originalArg] = this.aliasObject[originalArg] || [];
|
|
|
|
this.aliasObject[originalArg].push(aliasArg);
|
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.
|
|
|
|
*/
|
2022-08-03 15:00:36 +00:00
|
|
|
public addCommand(commandNameArg: string): plugins.smartrx.rxjs.Subject<any> {
|
|
|
|
let commandSubject: plugins.smartrx.rxjs.Subject<any>;
|
|
|
|
const existingCommandSubject = this.getCommandSubject(commandNameArg);
|
2022-08-03 15:07:11 +00:00
|
|
|
commandSubject = existingCommandSubject || new plugins.smartrx.rxjs.Subject<any>();
|
|
|
|
|
2022-08-03 15:00:36 +00:00
|
|
|
this.commandObservableMap.add({
|
|
|
|
commandName: commandNameArg,
|
|
|
|
subject: commandSubject,
|
2022-08-03 15:07:11 +00:00
|
|
|
});
|
2022-08-03 15:00:36 +00:00
|
|
|
return commandSubject;
|
2017-04-22 19:03:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* execute trigger by name
|
|
|
|
* @param commandNameArg - the name of the command to trigger
|
|
|
|
*/
|
2022-08-03 15:00:36 +00:00
|
|
|
public triggerCommand(commandNameArg: string, argvObject: any) {
|
|
|
|
const triggerSubject = this.getCommandSubject(commandNameArg);
|
|
|
|
triggerSubject.next(argvObject);
|
2018-05-03 10:10:39 +00:00
|
|
|
return triggerSubject;
|
|
|
|
}
|
|
|
|
|
2022-08-03 15:00:36 +00:00
|
|
|
public getCommandSubject(commandNameArg: string) {
|
|
|
|
const triggerObservableObject = this.commandObservableMap.findSync(
|
2021-04-07 20:25:17 +00:00
|
|
|
(triggerObservableObjectArg) => {
|
2022-08-03 15:00:36 +00:00
|
|
|
return triggerObservableObjectArg.commandName === commandNameArg;
|
2018-05-03 10:10:39 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
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
|
|
|
|
*/
|
2022-08-03 15:00:36 +00:00
|
|
|
public addHelp(optionsArg: { helpText: string }) {
|
2021-04-07 20:25:17 +00:00
|
|
|
this.addCommand('help').subscribe((argvArg) => {
|
|
|
|
logger.log('info', optionsArg.helpText);
|
2018-05-03 10:10:39 +00:00
|
|
|
});
|
2017-04-22 19:03:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* specify version to be printed for -v --version
|
|
|
|
*/
|
2022-08-03 15:00:36 +00:00
|
|
|
public addVersion(versionArg: string) {
|
2018-05-03 10:10:39 +00:00
|
|
|
this.version = versionArg;
|
|
|
|
this.addCommandAlias('v', 'version');
|
2022-08-03 15:07:11 +00:00
|
|
|
this.parseCompleted.promise.then((argv) => {
|
2022-08-03 15:00:36 +00:00
|
|
|
if (argv.v) {
|
2018-05-03 10:10:39 +00:00
|
|
|
console.log(this.version);
|
|
|
|
}
|
|
|
|
});
|
2017-04-22 19:03:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-05-03 10:10:39 +00:00
|
|
|
* adds a trigger that is called when no command is specified
|
2017-04-22 19:03:28 +00:00
|
|
|
*/
|
2022-08-03 15:00:36 +00:00
|
|
|
public standardCommand(): plugins.smartrx.rxjs.Subject<any> {
|
|
|
|
const standardSubject = this.addCommand('standardCommand');
|
2018-05-03 10:10:39 +00:00
|
|
|
return standardSubject;
|
2017-04-22 19:03:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* start the process of evaluating commands
|
|
|
|
*/
|
2022-08-03 15:00:36 +00:00
|
|
|
public startParse(): void {
|
|
|
|
const parsedYArgs = plugins.yargsParser(process.argv);
|
2022-08-03 15:07:11 +00:00
|
|
|
|
2022-08-03 15:00:36 +00:00
|
|
|
// lets handle commands
|
|
|
|
let counter = 0;
|
|
|
|
let foundCommand = false;
|
2022-08-03 16:47:35 +00:00
|
|
|
parsedYArgs._ = parsedYArgs._.filter((commandPartArg) => {
|
2022-08-03 15:07:11 +00:00
|
|
|
counter++;
|
2022-08-03 15:00:36 +00:00
|
|
|
if (typeof commandPartArg === 'number') {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (counter <= 2 && !foundCommand) {
|
|
|
|
const isPath = commandPartArg.startsWith('/');
|
|
|
|
foundCommand = !isPath;
|
|
|
|
return foundCommand;
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
2022-08-03 15:07:11 +00:00
|
|
|
});
|
2022-08-03 15:00:36 +00:00
|
|
|
for (const command of this.commandObservableMap.getArray()) {
|
2022-08-03 16:47:35 +00:00
|
|
|
const wantedCommand = parsedYArgs._[0];
|
|
|
|
if (!wantedCommand) {
|
2022-08-03 15:07:11 +00:00
|
|
|
const standardCommand = this.commandObservableMap.findSync((commandArg) => {
|
|
|
|
return commandArg.commandName === 'standardCommand';
|
2022-08-03 15:00:36 +00:00
|
|
|
});
|
|
|
|
if (standardCommand) {
|
|
|
|
standardCommand.subject.next(parsedYArgs);
|
|
|
|
} else {
|
|
|
|
console.log('no smartcli standard task was created or assigned.');
|
2022-08-03 15:07:11 +00:00
|
|
|
}
|
2022-08-03 15:00:36 +00:00
|
|
|
break;
|
|
|
|
}
|
2022-08-03 16:47:35 +00:00
|
|
|
console.log(`Wanted command: ${wantedCommand}`);
|
2022-08-03 15:00:36 +00:00
|
|
|
if (command.commandName === parsedYArgs._[0]) {
|
|
|
|
command.subject.next(parsedYArgs);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (this.aliasObject[parsedYArgs[0]]) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.parseCompleted.resolve(parsedYArgs);
|
2018-05-03 10:10:39 +00:00
|
|
|
return;
|
2017-04-22 19:03:28 +00:00
|
|
|
}
|
2016-10-14 22:56:02 +00:00
|
|
|
}
|