BREAKING CHANGE(core): switch to esm
This commit is contained in:
8
ts/00_commitinfo_data.ts
Normal file
8
ts/00_commitinfo_data.ts
Normal file
@ -0,0 +1,8 @@
|
||||
/**
|
||||
* autocreated commitinfo by @pushrocks/commitinfo
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@pushrocks/smartcli',
|
||||
version: '4.0.0',
|
||||
description: 'easy observable cli tasks'
|
||||
}
|
@ -1 +1 @@
|
||||
export { Smartcli } from './smartcli.classes.smartcli';
|
||||
export { Smartcli } from './smartcli.classes.smartcli.js';
|
||||
|
@ -1,20 +1,9 @@
|
||||
import * as smartpromise from '@pushrocks/smartpromise';
|
||||
import { Subject } from 'rxjs';
|
||||
|
||||
import * as plugins from './smartcli.plugins';
|
||||
|
||||
// import classes
|
||||
import { ObjectMap } from '@pushrocks/lik';
|
||||
import * as plugins from './smartcli.plugins.js';
|
||||
|
||||
// interfaces
|
||||
export interface ICommandPromiseObject {
|
||||
export interface ICommandObservableObject {
|
||||
commandName: string;
|
||||
promise: Promise<void>;
|
||||
}
|
||||
|
||||
export interface ITriggerObservableObject {
|
||||
triggerName: string;
|
||||
subject: Subject<any>;
|
||||
subject: plugins.smartrx.rxjs.Subject<any>;
|
||||
}
|
||||
|
||||
const logger = new plugins.smartlog.ConsoleLog();
|
||||
@ -23,27 +12,29 @@ const logger = new plugins.smartlog.ConsoleLog();
|
||||
* class to create a new instance of Smartcli. Handles parsing of command line arguments.
|
||||
*/
|
||||
export class Smartcli {
|
||||
argv: any;
|
||||
questionsDone;
|
||||
parseStarted: smartpromise.Deferred<any>;
|
||||
commands;
|
||||
questions;
|
||||
version: string;
|
||||
/**
|
||||
* this Deferred contains the parsed result in the end
|
||||
*/
|
||||
public parseCompleted = plugins.smartpromise.defer<any>();
|
||||
|
||||
|
||||
public version: string;
|
||||
private checkForEnvCliCall = true;
|
||||
|
||||
/**
|
||||
* map of all Trigger/Observable objects to keep track
|
||||
*/
|
||||
allTriggerObservablesMap = new ObjectMap<ITriggerObservableObject>();
|
||||
private commandObservableMap = new plugins.lik.ObjectMap<ICommandObservableObject>();
|
||||
|
||||
/**
|
||||
* maps alias
|
||||
*/
|
||||
public aliasObject: {[key: string]: string[]} = {};
|
||||
|
||||
/**
|
||||
* The constructor of Smartcli
|
||||
*/
|
||||
constructor() {
|
||||
this.argv = plugins.yargs;
|
||||
this.questionsDone = smartpromise.defer();
|
||||
this.parseStarted = smartpromise.defer();
|
||||
}
|
||||
constructor() {}
|
||||
|
||||
/**
|
||||
* halts any execution of commands if (process.env.CLI_CALL === false)
|
||||
@ -55,55 +46,41 @@ export class Smartcli {
|
||||
/**
|
||||
* 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;
|
||||
public addCommandAlias(originalArg, aliasArg): void {
|
||||
this.aliasObject[originalArg] = this.aliasObject[originalArg] || [];
|
||||
this.aliasObject[originalArg].push(aliasArg);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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> {
|
||||
const triggerSubject = this.addTrigger(commandNameArg);
|
||||
this.parseStarted.promise.then(() => {
|
||||
if (this.argv._.indexOf(commandNameArg) === 0) {
|
||||
this.trigger(commandNameArg);
|
||||
}
|
||||
});
|
||||
return triggerSubject;
|
||||
}
|
||||
|
||||
/**
|
||||
* adds a Trigger. Like addCommand(), but returns an subscribable observable
|
||||
*/
|
||||
addTrigger(triggerNameArg: string) {
|
||||
const 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;
|
||||
public addCommand(commandNameArg: string): plugins.smartrx.rxjs.Subject<any> {
|
||||
let commandSubject: plugins.smartrx.rxjs.Subject<any>;
|
||||
const existingCommandSubject = this.getCommandSubject(commandNameArg);
|
||||
commandSubject = existingCommandSubject || new plugins.smartrx.rxjs.Subject<any>();
|
||||
|
||||
this.commandObservableMap.add({
|
||||
commandName: commandNameArg,
|
||||
subject: commandSubject,
|
||||
})
|
||||
return commandSubject;
|
||||
}
|
||||
|
||||
/**
|
||||
* execute trigger by name
|
||||
* @param commandNameArg - the name of the command to trigger
|
||||
*/
|
||||
trigger(triggerName: string) {
|
||||
const triggerSubject = this.getTriggerSubject(triggerName);
|
||||
triggerSubject.next(this.argv);
|
||||
public triggerCommand(commandNameArg: string, argvObject: any) {
|
||||
const triggerSubject = this.getCommandSubject(commandNameArg);
|
||||
triggerSubject.next(argvObject);
|
||||
return triggerSubject;
|
||||
}
|
||||
|
||||
getTriggerSubject(triggerName: string) {
|
||||
const triggerObservableObject = this.allTriggerObservablesMap.find(
|
||||
public getCommandSubject(commandNameArg: string) {
|
||||
const triggerObservableObject = this.commandObservableMap.findSync(
|
||||
(triggerObservableObjectArg) => {
|
||||
return triggerObservableObjectArg.triggerName === triggerName;
|
||||
return triggerObservableObjectArg.commandName === commandNameArg;
|
||||
}
|
||||
);
|
||||
if (triggerObservableObject) {
|
||||
@ -116,7 +93,7 @@ export class Smartcli {
|
||||
/**
|
||||
* allows to specify help text to be printed above the rest of the help text
|
||||
*/
|
||||
addHelp(optionsArg: { helpText: string }) {
|
||||
public addHelp(optionsArg: { helpText: string }) {
|
||||
this.addCommand('help').subscribe((argvArg) => {
|
||||
logger.log('info', optionsArg.helpText);
|
||||
});
|
||||
@ -125,11 +102,11 @@ export class Smartcli {
|
||||
/**
|
||||
* specify version to be printed for -v --version
|
||||
*/
|
||||
addVersion(versionArg: string) {
|
||||
public addVersion(versionArg: string) {
|
||||
this.version = versionArg;
|
||||
this.addCommandAlias('v', 'version');
|
||||
this.parseStarted.promise.then(() => {
|
||||
if (this.argv.v) {
|
||||
this.parseCompleted.promise.then(argv => {
|
||||
if (argv.v) {
|
||||
console.log(this.version);
|
||||
}
|
||||
});
|
||||
@ -138,32 +115,61 @@ export class Smartcli {
|
||||
/**
|
||||
* adds a trigger that is called when no command is specified
|
||||
*/
|
||||
standardTask(): Subject<any> {
|
||||
const standardSubject = this.addTrigger('standardTask');
|
||||
this.parseStarted.promise.then(() => {
|
||||
if (
|
||||
(this.argv._.length === 0 ||
|
||||
(this.argv._.length === 1 && this.argv._[0].startsWith('test/'))) &&
|
||||
!this.argv.v
|
||||
) {
|
||||
this.trigger('standardTask');
|
||||
}
|
||||
});
|
||||
public standardCommand(): plugins.smartrx.rxjs.Subject<any> {
|
||||
const standardSubject = this.addCommand('standardCommand');
|
||||
return standardSubject;
|
||||
}
|
||||
|
||||
/**
|
||||
* start the process of evaluating commands
|
||||
*/
|
||||
startParse(): void {
|
||||
public startParse(): void {
|
||||
// if we check for cli env calls, we might want to abort here.
|
||||
if (!process.env.CLI_CALL && this.checkForEnvCliCall) {
|
||||
console.log(
|
||||
`note: @pushrocks/smartcli: You called .startParse() on a SmartCli instance. However process.env.CLI_CALL being absent prevented parsing.`
|
||||
);
|
||||
return;
|
||||
}
|
||||
this.argv = this.argv.argv;
|
||||
this.parseStarted.resolve();
|
||||
const parsedYArgs = plugins.yargsParser(process.argv);
|
||||
|
||||
// lets handle commands
|
||||
let counter = 0;
|
||||
let foundCommand = false;
|
||||
parsedYArgs._.map((commandPartArg) => {
|
||||
counter ++;
|
||||
if (typeof commandPartArg === 'number') {
|
||||
return true;
|
||||
}
|
||||
if (counter <= 2 && !foundCommand) {
|
||||
const isPath = commandPartArg.startsWith('/');
|
||||
foundCommand = !isPath;
|
||||
return foundCommand;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
})
|
||||
for (const command of this.commandObservableMap.getArray()) {
|
||||
if (!parsedYArgs._[0]) {
|
||||
const standardCommand = this.commandObservableMap.findSync(commandArg => {
|
||||
return commandArg.commandName === "standardCommand";
|
||||
});
|
||||
if (standardCommand) {
|
||||
standardCommand.subject.next(parsedYArgs);
|
||||
} else {
|
||||
console.log('no smartcli standard task was created or assigned.');
|
||||
};
|
||||
break;
|
||||
}
|
||||
if (command.commandName === parsedYArgs._[0]) {
|
||||
command.subject.next(parsedYArgs);
|
||||
break;
|
||||
}
|
||||
if (this.aliasObject[parsedYArgs[0]]) {
|
||||
|
||||
}
|
||||
}
|
||||
this.parseCompleted.resolve(parsedYArgs);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,16 @@
|
||||
import * as yargs from 'yargs';
|
||||
// @pushrocks scope
|
||||
import * as smartlog from '@pushrocks/smartlog';
|
||||
import * as lik from '@pushrocks/lik';
|
||||
import * as path from 'path';
|
||||
import * as smartparam from '@pushrocks/smartparam';
|
||||
import * as smartpromise from '@pushrocks/smartpromise';
|
||||
import * as smartrx from '@pushrocks/smartrx';
|
||||
|
||||
export { yargs, smartlog, lik, path, smartparam };
|
||||
export { smartlog, lik, path, smartparam, smartpromise, smartrx };
|
||||
|
||||
// thirdparty scope
|
||||
import yargsParser from 'yargs-parser';
|
||||
|
||||
export {
|
||||
yargsParser
|
||||
}
|
||||
|
Reference in New Issue
Block a user