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",
|
"githost": "gitlab.com",
|
||||||
"gitscope": "pushrocks",
|
"gitscope": "pushrocks",
|
||||||
"gitrepo": "smartcli",
|
"gitrepo": "smartcli",
|
||||||
"shortDescription": "easy observable cli tasks",
|
"description": "easy observable cli tasks",
|
||||||
"npmPackagename": "@pushrocks/smartcli",
|
"npmPackagename": "@pushrocks/smartcli",
|
||||||
"license": "MIT"
|
"license": "MIT"
|
||||||
}
|
}
|
||||||
|
20859
package-lock.json
generated
20859
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",
|
"description": "easy observable cli tasks",
|
||||||
"main": "dist_ts/index.js",
|
"main": "dist_ts/index.js",
|
||||||
"typings": "dist_ts/index.d.ts",
|
"typings": "dist_ts/index.d.ts",
|
||||||
|
"type": "module",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "(tstest test/ --web)",
|
"test": "(tstest test/ --web)",
|
||||||
"build": "(tsbuild --web)"
|
"build": "(tsbuild --web)"
|
||||||
@ -26,22 +27,21 @@
|
|||||||
},
|
},
|
||||||
"homepage": "https://gitlab.com/pushrocks/smartcli",
|
"homepage": "https://gitlab.com/pushrocks/smartcli",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@pushrocks/lik": "^4.0.20",
|
"@pushrocks/lik": "^6.0.0",
|
||||||
"@pushrocks/smartlog": "^2.0.39",
|
"@pushrocks/smartlog": "^3.0.1",
|
||||||
"@pushrocks/smartparam": "^1.1.6",
|
"@pushrocks/smartparam": "^1.1.6",
|
||||||
"@pushrocks/smartpromise": "^3.1.3",
|
"@pushrocks/smartpromise": "^3.1.3",
|
||||||
"@types/yargs": "^16.0.1",
|
"@pushrocks/smartrx": "^2.0.25",
|
||||||
"rxjs": "^6.6.7",
|
"@types/yargs": "^17.0.10",
|
||||||
"yargs": "^16.2.0"
|
"@types/yargs-parser": "^21.0.0",
|
||||||
|
"yargs-parser": "^21.0.1"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@gitzone/tsbuild": "^2.1.25",
|
"@gitzone/tsbuild": "^2.1.25",
|
||||||
"@gitzone/tsrun": "^1.2.12",
|
"@gitzone/tsrun": "^1.2.12",
|
||||||
"@gitzone/tstest": "^1.0.52",
|
"@gitzone/tstest": "^1.0.52",
|
||||||
"@pushrocks/tapbundle": "^3.2.14",
|
"@pushrocks/tapbundle": "^5.0.4",
|
||||||
"@types/node": "^14.14.37",
|
"@types/node": "^18.6.3"
|
||||||
"tslint": "^6.1.3",
|
|
||||||
"tslint-config-prettier": "^1.18.0"
|
|
||||||
},
|
},
|
||||||
"files": [
|
"files": [
|
||||||
"ts/**/*",
|
"ts/**/*",
|
||||||
|
18
test/test.ts
18
test/test.ts
@ -1,43 +1,43 @@
|
|||||||
import { tap, expect } from '@pushrocks/tapbundle';
|
import { tap, expect } from '@pushrocks/tapbundle';
|
||||||
import { Subject } from 'rxjs';
|
import { Subject } from 'rxjs';
|
||||||
|
|
||||||
import smartcli = require('../ts/index');
|
import * as smartcli from '../ts/index.js';
|
||||||
|
|
||||||
let smartCliTestObject: smartcli.Smartcli;
|
let smartCliTestObject: smartcli.Smartcli;
|
||||||
|
|
||||||
tap.test('should create a new Smartcli', async () => {
|
tap.test('should create a new Smartcli', async () => {
|
||||||
smartCliTestObject = new smartcli.Smartcli();
|
smartCliTestObject = new smartcli.Smartcli();
|
||||||
expect(smartCliTestObject).to.be.instanceof(smartcli.Smartcli);
|
expect(smartCliTestObject).toBeInstanceOf(smartcli.Smartcli);
|
||||||
});
|
});
|
||||||
|
|
||||||
tap.test('should add an command', async () => {
|
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 () => {
|
tap.test('should start parsing a standardTask', async () => {
|
||||||
expect(smartCliTestObject.standardTask()).to.be.instanceOf(Subject);
|
expect(smartCliTestObject.standardCommand()).toBeInstanceOf(Subject);
|
||||||
});
|
});
|
||||||
|
|
||||||
let hasExecuted: boolean = false;
|
let hasExecuted: boolean = false;
|
||||||
|
|
||||||
tap.test('should accept a command', async () => {
|
tap.test('should accept a command', async () => {
|
||||||
smartCliTestObject.addTrigger('triggerme').subscribe(() => {
|
smartCliTestObject.addCommand('triggerme').subscribe(() => {
|
||||||
hasExecuted = true;
|
hasExecuted = true;
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
tap.test('should not have executed yet', async () => {
|
tap.test('should not have executed yet', async () => {
|
||||||
expect(hasExecuted).to.be.false;
|
expect(hasExecuted).toBeFalse();
|
||||||
});
|
});
|
||||||
|
|
||||||
tap.test('should execute when triggered', async () => {
|
tap.test('should execute when triggered', async () => {
|
||||||
smartCliTestObject.trigger('triggerme');
|
smartCliTestObject.triggerCommand('triggerme', {});
|
||||||
expect(hasExecuted).be.true;
|
expect(hasExecuted).toBeTrue();
|
||||||
});
|
});
|
||||||
|
|
||||||
tap.test('should start parsing the CLI input', async () => {
|
tap.test('should start parsing the CLI input', async () => {
|
||||||
smartCliTestObject.startParse();
|
smartCliTestObject.startParse();
|
||||||
expect(smartCliTestObject.parseStarted.promise).to.be.instanceOf(Promise);
|
expect(smartCliTestObject.parseCompleted.promise).toBeInstanceOf(Promise);
|
||||||
});
|
});
|
||||||
|
|
||||||
tap.start();
|
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 * as plugins from './smartcli.plugins.js';
|
||||||
import { Subject } from 'rxjs';
|
|
||||||
|
|
||||||
import * as plugins from './smartcli.plugins';
|
|
||||||
|
|
||||||
// import classes
|
|
||||||
import { ObjectMap } from '@pushrocks/lik';
|
|
||||||
|
|
||||||
// interfaces
|
// interfaces
|
||||||
export interface ICommandPromiseObject {
|
export interface ICommandObservableObject {
|
||||||
commandName: string;
|
commandName: string;
|
||||||
promise: Promise<void>;
|
subject: plugins.smartrx.rxjs.Subject<any>;
|
||||||
}
|
|
||||||
|
|
||||||
export interface ITriggerObservableObject {
|
|
||||||
triggerName: string;
|
|
||||||
subject: Subject<any>;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const logger = new plugins.smartlog.ConsoleLog();
|
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.
|
* class to create a new instance of Smartcli. Handles parsing of command line arguments.
|
||||||
*/
|
*/
|
||||||
export class Smartcli {
|
export class Smartcli {
|
||||||
argv: any;
|
/**
|
||||||
questionsDone;
|
* this Deferred contains the parsed result in the end
|
||||||
parseStarted: smartpromise.Deferred<any>;
|
*/
|
||||||
commands;
|
public parseCompleted = plugins.smartpromise.defer<any>();
|
||||||
questions;
|
|
||||||
version: string;
|
|
||||||
|
public version: string;
|
||||||
private checkForEnvCliCall = true;
|
private checkForEnvCliCall = true;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* map of all Trigger/Observable objects to keep track
|
* 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
|
* The constructor of Smartcli
|
||||||
*/
|
*/
|
||||||
constructor() {
|
constructor() {}
|
||||||
this.argv = plugins.yargs;
|
|
||||||
this.questionsDone = smartpromise.defer();
|
|
||||||
this.parseStarted = smartpromise.defer();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* halts any execution of commands if (process.env.CLI_CALL === false)
|
* 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.
|
* adds an alias, meaning one equals the other in terms of command execution.
|
||||||
*/
|
*/
|
||||||
addCommandAlias(keyArg, aliasArg): void {
|
public addCommandAlias(originalArg, aliasArg): void {
|
||||||
this.argv = this.argv.alias(keyArg, aliasArg);
|
this.aliasObject[originalArg] = this.aliasObject[originalArg] || [];
|
||||||
return;
|
this.aliasObject[originalArg].push(aliasArg);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* adds a Command by returning a Promise that reacts to the specific commandString given.
|
* 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.
|
* Note: in e.g. "npm install something" the "install" is considered the command.
|
||||||
*/
|
*/
|
||||||
addCommand(commandNameArg: string): Subject<any> {
|
public addCommand(commandNameArg: string): plugins.smartrx.rxjs.Subject<any> {
|
||||||
const triggerSubject = this.addTrigger(commandNameArg);
|
let commandSubject: plugins.smartrx.rxjs.Subject<any>;
|
||||||
this.parseStarted.promise.then(() => {
|
const existingCommandSubject = this.getCommandSubject(commandNameArg);
|
||||||
if (this.argv._.indexOf(commandNameArg) === 0) {
|
commandSubject = existingCommandSubject || new plugins.smartrx.rxjs.Subject<any>();
|
||||||
this.trigger(commandNameArg);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return triggerSubject;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
this.commandObservableMap.add({
|
||||||
* adds a Trigger. Like addCommand(), but returns an subscribable observable
|
commandName: commandNameArg,
|
||||||
*/
|
subject: commandSubject,
|
||||||
addTrigger(triggerNameArg: string) {
|
})
|
||||||
const triggerSubject = new Subject<any>();
|
return commandSubject;
|
||||||
if (!this.getTriggerSubject(triggerNameArg)) {
|
|
||||||
this.allTriggerObservablesMap.add({
|
|
||||||
triggerName: triggerNameArg,
|
|
||||||
subject: triggerSubject,
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
throw new Error(`you can't add a trigger twice`);
|
|
||||||
}
|
|
||||||
return triggerSubject;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* execute trigger by name
|
* execute trigger by name
|
||||||
* @param commandNameArg - the name of the command to trigger
|
* @param commandNameArg - the name of the command to trigger
|
||||||
*/
|
*/
|
||||||
trigger(triggerName: string) {
|
public triggerCommand(commandNameArg: string, argvObject: any) {
|
||||||
const triggerSubject = this.getTriggerSubject(triggerName);
|
const triggerSubject = this.getCommandSubject(commandNameArg);
|
||||||
triggerSubject.next(this.argv);
|
triggerSubject.next(argvObject);
|
||||||
return triggerSubject;
|
return triggerSubject;
|
||||||
}
|
}
|
||||||
|
|
||||||
getTriggerSubject(triggerName: string) {
|
public getCommandSubject(commandNameArg: string) {
|
||||||
const triggerObservableObject = this.allTriggerObservablesMap.find(
|
const triggerObservableObject = this.commandObservableMap.findSync(
|
||||||
(triggerObservableObjectArg) => {
|
(triggerObservableObjectArg) => {
|
||||||
return triggerObservableObjectArg.triggerName === triggerName;
|
return triggerObservableObjectArg.commandName === commandNameArg;
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
if (triggerObservableObject) {
|
if (triggerObservableObject) {
|
||||||
@ -116,7 +93,7 @@ export class Smartcli {
|
|||||||
/**
|
/**
|
||||||
* allows to specify help text to be printed above the rest of the help text
|
* 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) => {
|
this.addCommand('help').subscribe((argvArg) => {
|
||||||
logger.log('info', optionsArg.helpText);
|
logger.log('info', optionsArg.helpText);
|
||||||
});
|
});
|
||||||
@ -125,11 +102,11 @@ export class Smartcli {
|
|||||||
/**
|
/**
|
||||||
* specify version to be printed for -v --version
|
* specify version to be printed for -v --version
|
||||||
*/
|
*/
|
||||||
addVersion(versionArg: string) {
|
public addVersion(versionArg: string) {
|
||||||
this.version = versionArg;
|
this.version = versionArg;
|
||||||
this.addCommandAlias('v', 'version');
|
this.addCommandAlias('v', 'version');
|
||||||
this.parseStarted.promise.then(() => {
|
this.parseCompleted.promise.then(argv => {
|
||||||
if (this.argv.v) {
|
if (argv.v) {
|
||||||
console.log(this.version);
|
console.log(this.version);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -138,32 +115,61 @@ export class Smartcli {
|
|||||||
/**
|
/**
|
||||||
* adds a trigger that is called when no command is specified
|
* adds a trigger that is called when no command is specified
|
||||||
*/
|
*/
|
||||||
standardTask(): Subject<any> {
|
public standardCommand(): plugins.smartrx.rxjs.Subject<any> {
|
||||||
const standardSubject = this.addTrigger('standardTask');
|
const standardSubject = this.addCommand('standardCommand');
|
||||||
this.parseStarted.promise.then(() => {
|
|
||||||
if (
|
|
||||||
(this.argv._.length === 0 ||
|
|
||||||
(this.argv._.length === 1 && this.argv._[0].startsWith('test/'))) &&
|
|
||||||
!this.argv.v
|
|
||||||
) {
|
|
||||||
this.trigger('standardTask');
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return standardSubject;
|
return standardSubject;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* start the process of evaluating commands
|
* 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) {
|
if (!process.env.CLI_CALL && this.checkForEnvCliCall) {
|
||||||
console.log(
|
console.log(
|
||||||
`note: @pushrocks/smartcli: You called .startParse() on a SmartCli instance. However process.env.CLI_CALL being absent prevented parsing.`
|
`note: @pushrocks/smartcli: You called .startParse() on a SmartCli instance. However process.env.CLI_CALL being absent prevented parsing.`
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.argv = this.argv.argv;
|
const parsedYArgs = plugins.yargsParser(process.argv);
|
||||||
this.parseStarted.resolve();
|
|
||||||
|
// 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;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,16 @@
|
|||||||
import * as yargs from 'yargs';
|
// @pushrocks scope
|
||||||
import * as smartlog from '@pushrocks/smartlog';
|
import * as smartlog from '@pushrocks/smartlog';
|
||||||
import * as lik from '@pushrocks/lik';
|
import * as lik from '@pushrocks/lik';
|
||||||
import * as path from 'path';
|
import * as path from 'path';
|
||||||
import * as smartparam from '@pushrocks/smartparam';
|
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