BREAKING CHANGE(core): switch to esm
This commit is contained in:
parent
311232aeea
commit
fa59d2da40
9
.snyk
9
.snyk
@ -1,9 +0,0 @@
|
||||
# Snyk (https://snyk.io) policy file, patches or ignores known vulnerabilities.
|
||||
version: v1.12.0
|
||||
# ignores vulnerabilities until expiry date; change duration by modifying expiry date
|
||||
ignore:
|
||||
'npm:mem:20180117':
|
||||
- yargs > os-locale > mem:
|
||||
reason: None given
|
||||
expires: '2018-09-29T22:12:38.004Z'
|
||||
patch: {}
|
@ -9,7 +9,7 @@
|
||||
"githost": "gitlab.com",
|
||||
"gitscope": "pushrocks",
|
||||
"gitrepo": "smartcli",
|
||||
"shortDescription": "easy observable cli tasks",
|
||||
"description": "easy observable cli tasks",
|
||||
"npmPackagename": "@pushrocks/smartcli",
|
||||
"license": "MIT"
|
||||
}
|
||||
|
20895
package-lock.json
generated
20895
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
18
package.json
18
package.json
@ -5,6 +5,7 @@
|
||||
"description": "easy observable cli tasks",
|
||||
"main": "dist_ts/index.js",
|
||||
"typings": "dist_ts/index.d.ts",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"test": "(tstest test/ --web)",
|
||||
"build": "(tsbuild --web)"
|
||||
@ -26,22 +27,21 @@
|
||||
},
|
||||
"homepage": "https://gitlab.com/pushrocks/smartcli",
|
||||
"dependencies": {
|
||||
"@pushrocks/lik": "^4.0.20",
|
||||
"@pushrocks/smartlog": "^2.0.39",
|
||||
"@pushrocks/lik": "^6.0.0",
|
||||
"@pushrocks/smartlog": "^3.0.1",
|
||||
"@pushrocks/smartparam": "^1.1.6",
|
||||
"@pushrocks/smartpromise": "^3.1.3",
|
||||
"@types/yargs": "^16.0.1",
|
||||
"rxjs": "^6.6.7",
|
||||
"yargs": "^16.2.0"
|
||||
"@pushrocks/smartrx": "^2.0.25",
|
||||
"@types/yargs": "^17.0.10",
|
||||
"@types/yargs-parser": "^21.0.0",
|
||||
"yargs-parser": "^21.0.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@gitzone/tsbuild": "^2.1.25",
|
||||
"@gitzone/tsrun": "^1.2.12",
|
||||
"@gitzone/tstest": "^1.0.52",
|
||||
"@pushrocks/tapbundle": "^3.2.14",
|
||||
"@types/node": "^14.14.37",
|
||||
"tslint": "^6.1.3",
|
||||
"tslint-config-prettier": "^1.18.0"
|
||||
"@pushrocks/tapbundle": "^5.0.4",
|
||||
"@types/node": "^18.6.3"
|
||||
},
|
||||
"files": [
|
||||
"ts/**/*",
|
||||
|
18
test/test.ts
18
test/test.ts
@ -1,43 +1,43 @@
|
||||
import { tap, expect } from '@pushrocks/tapbundle';
|
||||
import { Subject } from 'rxjs';
|
||||
|
||||
import smartcli = require('../ts/index');
|
||||
import * as smartcli from '../ts/index.js';
|
||||
|
||||
let smartCliTestObject: smartcli.Smartcli;
|
||||
|
||||
tap.test('should create a new Smartcli', async () => {
|
||||
smartCliTestObject = new smartcli.Smartcli();
|
||||
expect(smartCliTestObject).to.be.instanceof(smartcli.Smartcli);
|
||||
expect(smartCliTestObject).toBeInstanceOf(smartcli.Smartcli);
|
||||
});
|
||||
|
||||
tap.test('should add an command', async () => {
|
||||
expect(smartCliTestObject.addCommand('awesome')).to.be.instanceOf(Subject);
|
||||
expect(smartCliTestObject.addCommand('awesome')).toBeInstanceOf(Subject);
|
||||
});
|
||||
|
||||
tap.test('should start parsing a standardTask', async () => {
|
||||
expect(smartCliTestObject.standardTask()).to.be.instanceOf(Subject);
|
||||
expect(smartCliTestObject.standardCommand()).toBeInstanceOf(Subject);
|
||||
});
|
||||
|
||||
let hasExecuted: boolean = false;
|
||||
|
||||
tap.test('should accept a command', async () => {
|
||||
smartCliTestObject.addTrigger('triggerme').subscribe(() => {
|
||||
smartCliTestObject.addCommand('triggerme').subscribe(() => {
|
||||
hasExecuted = true;
|
||||
});
|
||||
});
|
||||
|
||||
tap.test('should not have executed yet', async () => {
|
||||
expect(hasExecuted).to.be.false;
|
||||
expect(hasExecuted).toBeFalse();
|
||||
});
|
||||
|
||||
tap.test('should execute when triggered', async () => {
|
||||
smartCliTestObject.trigger('triggerme');
|
||||
expect(hasExecuted).be.true;
|
||||
smartCliTestObject.triggerCommand('triggerme', {});
|
||||
expect(hasExecuted).toBeTrue();
|
||||
});
|
||||
|
||||
tap.test('should start parsing the CLI input', async () => {
|
||||
smartCliTestObject.startParse();
|
||||
expect(smartCliTestObject.parseStarted.promise).to.be.instanceOf(Promise);
|
||||
expect(smartCliTestObject.parseCompleted.promise).toBeInstanceOf(Promise);
|
||||
});
|
||||
|
||||
tap.start();
|
||||
|
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;
|
||||
}
|
||||
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>();
|
||||
|
||||
/**
|
||||
* 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;
|
||||
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
|
||||
}
|
||||
|
10
tsconfig.json
Normal file
10
tsconfig.json
Normal file
@ -0,0 +1,10 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"experimentalDecorators": true,
|
||||
"useDefineForClassFields": false,
|
||||
"target": "ES2022",
|
||||
"module": "ES2022",
|
||||
"moduleResolution": "nodenext",
|
||||
"esModuleInterop": true
|
||||
}
|
||||
}
|
17
tslint.json
17
tslint.json
@ -1,17 +0,0 @@
|
||||
{
|
||||
"extends": ["tslint:latest", "tslint-config-prettier"],
|
||||
"rules": {
|
||||
"semicolon": [true, "always"],
|
||||
"no-console": false,
|
||||
"ordered-imports": false,
|
||||
"object-literal-sort-keys": false,
|
||||
"member-ordering": {
|
||||
"options":{
|
||||
"order": [
|
||||
"static-method"
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
"defaultSeverity": "warning"
|
||||
}
|
Loading…
Reference in New Issue
Block a user