Compare commits

..

32 Commits

Author SHA1 Message Date
e9fc5b98f2 4.0.7 2023-07-12 15:09:53 +02:00
fef954c423 fix(core): update 2023-07-12 15:09:53 +02:00
39408d9832 switch to new org scheme 2023-07-11 00:25:26 +02:00
26d9b3e9cc switch to new org scheme 2023-07-10 02:42:41 +02:00
5f027430bf 4.0.6 2022-08-07 11:40:46 +02:00
d3385782ed fix(core): update 2022-08-07 11:40:45 +02:00
7384b54e09 4.0.5 2022-08-04 12:22:49 +02:00
0eac72e15d fix(core): update 2022-08-04 12:22:49 +02:00
b7957b0ab6 4.0.4 2022-08-03 20:21:54 +02:00
99a0a9ca81 fix(core): update 2022-08-03 20:21:54 +02:00
bd66903419 4.0.3 2022-08-03 18:48:40 +02:00
740d8dac35 fix(core): update 2022-08-03 18:48:40 +02:00
488e7410fe 4.0.2 2022-08-03 18:47:35 +02:00
04deb8960c fix(core): update 2022-08-03 18:47:35 +02:00
19f0a9563f 4.0.1 2022-08-03 17:07:11 +02:00
db1e866fe1 fix(core): update 2022-08-03 17:07:11 +02:00
f7c24a0bd2 4.0.0 2022-08-03 17:00:36 +02:00
fa59d2da40 BREAKING CHANGE(core): switch to esm 2022-08-03 17:00:36 +02:00
311232aeea 3.0.14 2021-04-07 20:28:58 +00:00
4cd0844bc3 fix(core): update 2021-04-07 20:28:57 +00:00
17c1a687c8 3.0.13 2021-04-07 20:25:18 +00:00
1d1264c2b3 fix(core): update 2021-04-07 20:25:17 +00:00
b036e609ce 3.0.12 2020-05-29 17:48:18 +00:00
c2ec0df907 fix(core): update 2020-05-29 17:48:17 +00:00
167b4d29df 3.0.11 2020-04-13 21:53:18 +00:00
02fec216db fix(core): more consistent handling of process.enc.CLI_CALL 2020-04-13 21:53:18 +00:00
4e9d2f3e8c 3.0.10 2020-04-13 21:44:27 +00:00
65d8a8b6f5 fix(core): now works better with tapbundle tests 2020-04-13 21:44:27 +00:00
8e04bd6a62 3.0.9 2020-03-11 22:50:40 +00:00
687a5f7c4e fix(core): update 2020-03-11 22:50:40 +00:00
17983b1da9 3.0.8 2020-03-11 22:49:44 +00:00
5fcdf1ff8f fix(core): update 2020-03-11 22:49:43 +00:00
21 changed files with 5281 additions and 1873 deletions

View File

@ -0,0 +1,66 @@
name: Default (not tags)
on:
push:
tags-ignore:
- '**'
env:
IMAGE: registry.gitlab.com/hosttoday/ht-docker-node:npmci
NPMCI_COMPUTED_REPOURL: https://${{gitea.repository_owner}}:${{secrets.GITEA_TOKEN}}@gitea.lossless.digital/${{gitea.repository}}.git
NPMCI_TOKEN_NPM: ${{secrets.NPMCI_TOKEN_NPM}}
NPMCI_TOKEN_NPM2: ${{secrets.NPMCI_TOKEN_NPM2}}
NPMCI_GIT_GITHUBTOKEN: ${{secrets.NPMCI_GIT_GITHUBTOKEN}}
NPMCI_URL_CLOUDLY: ${{secrets.NPMCI_URL_CLOUDLY}}
jobs:
security:
runs-on: ubuntu-latest
continue-on-error: true
container:
image: ${{ env.IMAGE }}
steps:
- uses: actions/checkout@v3
- name: Install pnpm and npmci
run: |
pnpm install -g pnpm
pnpm install -g @shipzone/npmci
- name: Run npm prepare
run: npmci npm prepare
- name: Audit production dependencies
run: |
npmci command npm config set registry https://registry.npmjs.org
npmci command pnpm audit --audit-level=high --prod
continue-on-error: true
- name: Audit development dependencies
run: |
npmci command npm config set registry https://registry.npmjs.org
npmci command pnpm audit --audit-level=high --dev
continue-on-error: true
test:
if: ${{ always() }}
needs: security
runs-on: ubuntu-latest
container:
image: ${{ env.IMAGE }}
steps:
- uses: actions/checkout@v3
- name: Test stable
run: |
npmci node install stable
npmci npm install
npmci npm test
- name: Test build
run: |
npmci node install stable
npmci npm install
npmci npm build

View File

@ -0,0 +1,124 @@
name: Default (tags)
on:
push:
tags:
- '*'
env:
IMAGE: registry.gitlab.com/hosttoday/ht-docker-node:npmci
NPMCI_COMPUTED_REPOURL: https://${{gitea.repository_owner}}:${{secrets.GITEA_TOKEN}}@gitea.lossless.digital/${{gitea.repository}}.git
NPMCI_TOKEN_NPM: ${{secrets.NPMCI_TOKEN_NPM}}
NPMCI_TOKEN_NPM2: ${{secrets.NPMCI_TOKEN_NPM2}}
NPMCI_GIT_GITHUBTOKEN: ${{secrets.NPMCI_GIT_GITHUBTOKEN}}
NPMCI_URL_CLOUDLY: ${{secrets.NPMCI_URL_CLOUDLY}}
jobs:
security:
runs-on: ubuntu-latest
continue-on-error: true
container:
image: ${{ env.IMAGE }}
steps:
- uses: actions/checkout@v3
- name: Prepare
run: |
pnpm install -g pnpm
pnpm install -g @shipzone/npmci
npmci npm prepare
- name: Audit production dependencies
run: |
npmci command npm config set registry https://registry.npmjs.org
npmci command pnpm audit --audit-level=high --prod
continue-on-error: true
- name: Audit development dependencies
run: |
npmci command npm config set registry https://registry.npmjs.org
npmci command pnpm audit --audit-level=high --dev
continue-on-error: true
test:
if: ${{ always() }}
needs: security
runs-on: ubuntu-latest
container:
image: ${{ env.IMAGE }}
steps:
- uses: actions/checkout@v3
- name: Prepare
run: |
pnpm install -g pnpm
pnpm install -g @shipzone/npmci
npmci npm prepare
- name: Test stable
run: |
npmci node install stable
npmci npm install
npmci npm test
- name: Test build
run: |
npmci node install stable
npmci npm install
npmci npm build
release:
needs: test
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/')
runs-on: ubuntu-latest
container:
image: ${{ env.IMAGE }}
steps:
- uses: actions/checkout@v3
- name: Prepare
run: |
pnpm install -g pnpm
pnpm install -g @shipzone/npmci
npmci npm prepare
- name: Release
run: |
npmci node install stable
npmci npm publish
metadata:
needs: test
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/')
runs-on: ubuntu-latest
container:
image: ${{ env.IMAGE }}
continue-on-error: true
steps:
- uses: actions/checkout@v3
- name: Prepare
run: |
pnpm install -g pnpm
pnpm install -g @shipzone/npmci
npmci npm prepare
- name: Code quality
run: |
npmci command npm install -g typescript
npmci npm install
- name: Trigger
run: npmci trigger
- name: Build docs and upload artifacts
run: |
npmci node install stable
npmci npm install
pnpm install -g @gitzone/tsdoc
npmci command tsdoc
continue-on-error: true

18
.gitignore vendored
View File

@ -1,4 +1,20 @@
node_modules/ .nogit/
# artifacts
coverage/ coverage/
public/ public/
pages/
# installs
node_modules/
# caches
.yarn/ .yarn/
.cache/
.rpt2_cache
# builds
dist/
dist_*/
# custom

View File

@ -1,125 +0,0 @@
# gitzone standard
image: hosttoday/ht-docker-node:npmci
cache:
paths:
- .npmci_cache/
key: "$CI_BUILD_STAGE"
stages:
- security
- test
- release
- metadata
# ====================
# security stage
# ====================
mirror:
stage: security
script:
- npmci git mirror
tags:
- docker
- notpriv
snyk:
stage: security
script:
- npmci npm prepare
- npmci command npm install -g snyk
- npmci command npm install --ignore-scripts
- npmci command snyk test
tags:
- docker
- notpriv
# ====================
# test stage
# ====================
testLTS:
stage: test
script:
- npmci npm prepare
- npmci node install lts
- npmci npm install
- npmci npm test
coverage: /\d+.?\d+?\%\s*coverage/
tags:
- docker
- notpriv
testSTABLE:
stage: test
script:
- npmci npm prepare
- npmci node install stable
- npmci npm install
- npmci npm test
coverage: /\d+.?\d+?\%\s*coverage/
tags:
- docker
- notpriv
release:
stage: release
script:
- npmci node install stable
- npmci npm publish
only:
- tags
tags:
- docker
- notpriv
# ====================
# metadata stage
# ====================
codequality:
stage: metadata
image: docker:stable
allow_failure: true
services:
- docker:stable-dind
script:
- export SP_VERSION=$(echo "$CI_SERVER_VERSION" | sed 's/^\([0-9]*\)\.\([0-9]*\).*/\1-\2-stable/')
- docker run
--env SOURCE_CODE="$PWD"
--volume "$PWD":/code
--volume /var/run/docker.sock:/var/run/docker.sock
"registry.gitlab.com/gitlab-org/security-products/codequality:$SP_VERSION" /code
artifacts:
paths: [codeclimate.json]
tags:
- docker
- priv
trigger:
stage: metadata
script:
- npmci trigger
only:
- tags
tags:
- docker
- notpriv
pages:
image: hosttoday/ht-docker-node:npmci
stage: metadata
script:
- npmci command npm install -g typedoc typescript
- npmci npm prepare
- npmci npm install
- npmci command typedoc --module "commonjs" --target "ES2016" --out public/ ts/
tags:
- docker
- notpriv
only:
- tags
artifacts:
expire_in: 1 week
paths:
- public
allow_failure: true

9
.snyk
View File

@ -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: {}

11
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,11 @@
{
"version": "0.2.0",
"configurations": [
{
"command": "npm test",
"name": "Run npm test",
"request": "launch",
"type": "node-terminal"
}
]
}

26
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,26 @@
{
"json.schemas": [
{
"fileMatch": ["/npmextra.json"],
"schema": {
"type": "object",
"properties": {
"npmci": {
"type": "object",
"description": "settings for npmci"
},
"gitzone": {
"type": "object",
"description": "settings for gitzone",
"properties": {
"projectType": {
"type": "string",
"enum": ["website", "element", "service", "npm", "wcc"]
}
}
}
}
}
}
]
}

View File

@ -1,29 +0,0 @@
# smartcli
nodejs wrapper for CLI related tasks
## Availabililty
[![npm](https://pushrocks.gitlab.io/assets/repo-button-npm.svg)](https://www.npmjs.com/package/smartcli)
[![git](https://pushrocks.gitlab.io/assets/repo-button-git.svg)](https://GitLab.com/pushrocks/smartcli)
[![git](https://pushrocks.gitlab.io/assets/repo-button-mirror.svg)](https://github.com/pushrocks/smartcli)
[![docs](https://pushrocks.gitlab.io/assets/repo-button-docs.svg)](https://pushrocks.gitlab.io/smartcli/)
## Status for master
[![build status](https://GitLab.com/pushrocks/smartcli/badges/master/build.svg)](https://GitLab.com/pushrocks/smartcli/commits/master)
[![coverage report](https://GitLab.com/pushrocks/smartcli/badges/master/coverage.svg)](https://GitLab.com/pushrocks/smartcli/commits/master)
[![npm downloads per month](https://img.shields.io/npm/dm/smartcli.svg)](https://www.npmjs.com/package/smartcli)
[![Dependency Status](https://david-dm.org/pushrocks/smartcli.svg)](https://david-dm.org/pushrocks/smartcli)
[![bitHound Dependencies](https://www.bithound.io/github/pushrocks/smartcli/badges/dependencies.svg)](https://www.bithound.io/github/pushrocks/smartcli/master/dependencies/npm)
[![bitHound Code](https://www.bithound.io/github/pushrocks/smartcli/badges/code.svg)](https://www.bithound.io/github/pushrocks/smartcli)
[![TypeScript](https://img.shields.io/badge/TypeScript-2.x-blue.svg)](https://nodejs.org/dist/latest-v6.x/docs/api/)
[![node](https://img.shields.io/badge/node->=%206.x.x-blue.svg)](https://nodejs.org/dist/latest-v6.x/docs/api/)
[![JavaScript Style Guide](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com/)
For further information read the linked docs at the top of this README.
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)
> | By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy.html)
[![repo-footer](https://pushrocks.gitlab.io/assets/repo-footer.svg)](https://)

View File

@ -23,45 +23,6 @@ nodejs wrapper for CLI related tasks
## Usage ## Usage
this plugin tries to establish some logic in which CLI tools work.
take the following commandline input:
```
mytool command argument1 argument2 --option1 -o2 option2Value
```
- `mytool` obviously is the tool (like git)
- `command` is the main thing the tool shall do (like commit)
- `argument1` and `argument2` are arguments
- `option1` is a longform option you can add (like --message for message)
- `optionValue` is the referenced option value (like a commit message)
When there is no command and no option specified the standardTask applied.
When there is a option specified but no command, standardTask applies,
except when of the options is -v, --version or --help.
```javascript
import { Smartcli } from 'smartcli';
mySmartcli = new Smartcli();
mySmartcli.standardTask().then(argvArg => {
// do something if program is called without an command
});
mySmartcli.addCommand({ commandname: 'install' }).then(argvArg => {
// do something if program is called with command "install"
});
mySmartcli.addVersion('1.0.0'); // -v and --version options will display the specified version in the terminal
mySmartCli.addHelp({
// is triggered by help command and --help option
helpText: 'some help text to print' // the helpText to display
});
mySmartcli.startParse(); // starts the evaluation and fullfills or rejects promises.
```
For further information read the linked docs at the top of this README. For further information read the linked docs at the top of this README.
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh) > MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)

View File

@ -2,5 +2,16 @@
"npmci": { "npmci": {
"npmGlobalTools": [], "npmGlobalTools": [],
"npmAccesslevel": "public" "npmAccesslevel": "public"
},
"gitzone": {
"projectType": "npm",
"module": {
"githost": "gitlab.com",
"gitscope": "push.rocks",
"gitrepo": "smartcli",
"description": "easy observable cli tasks",
"npmPackagename": "@push.rocks/smartcli",
"license": "MIT"
}
} }
} }

1505
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,13 +1,15 @@
{ {
"name": "@pushrocks/smartcli", "name": "@push.rocks/smartcli",
"private": false, "private": false,
"version": "3.0.7", "version": "4.0.7",
"description": "nodejs wrapper for CLI related tasks", "description": "easy observable cli tasks",
"main": "dist/index.js", "main": "dist_ts/index.js",
"typings": "dist/index.d.ts", "typings": "dist_ts/index.d.ts",
"type": "module",
"scripts": { "scripts": {
"test": "(tstest test/)", "test": "(tstest test/ --web)",
"build": "(tsbuild)" "build": "(tsbuild --web)",
"buildDocs": "tsdoc"
}, },
"repository": { "repository": {
"type": "git", "type": "git",
@ -26,21 +28,33 @@
}, },
"homepage": "https://gitlab.com/pushrocks/smartcli", "homepage": "https://gitlab.com/pushrocks/smartcli",
"dependencies": { "dependencies": {
"@pushrocks/lik": "^3.0.4", "@push.rocks/lik": "^6.0.3",
"@pushrocks/smartlog": "^2.0.9", "@push.rocks/smartlog": "^3.0.1",
"@pushrocks/smartparam": "^1.0.4", "@push.rocks/smartparam": "^1.1.10",
"@pushrocks/smartpromise": "^2.0.5", "@push.rocks/smartpromise": "^4.0.3",
"@types/yargs": "^12.0.1", "@push.rocks/smartrx": "^3.0.3",
"rxjs": "^6.3.3", "yargs-parser": "21.1.1"
"yargs": "^12.0.5"
}, },
"devDependencies": { "devDependencies": {
"@gitzone/tsbuild": "^2.1.3", "@gitzone/tsbuild": "^2.1.66",
"@gitzone/tsrun": "^1.1.17", "@gitzone/tsrun": "^1.2.42",
"@gitzone/tstest": "^1.0.18", "@gitzone/tstest": "^1.0.74",
"@pushrocks/tapbundle": "^3.0.7", "@push.rocks/tapbundle": "^5.0.4",
"@types/node": "^10.12.12", "@types/node": "^20.4.1"
"tslint": "^5.11.0", },
"tslint-config-prettier": "^1.17.0" "files": [
} "ts/**/*",
"ts_web/**/*",
"dist/**/*",
"dist_*/**/*",
"dist_ts/**/*",
"dist_ts_web/**/*",
"assets/**/*",
"cli.js",
"npmextra.json",
"readme.md"
],
"browserslist": [
"last 1 chrome versions"
]
} }

4771
pnpm-lock.yaml generated Normal file

File diff suppressed because it is too large Load Diff

74
readme.md Normal file
View File

@ -0,0 +1,74 @@
# @push.rocks/smartcli
easy observable cli tasks
## Availabililty and Links
* [npmjs.org (npm package)](https://www.npmjs.com/package/@push.rocks/smartcli)
* [gitlab.com (source)](https://gitlab.com/push.rocks/smartcli)
* [github.com (source mirror)](https://github.com/push.rocks/smartcli)
* [docs (typedoc)](https://push.rocks.gitlab.io/smartcli/)
## Status for master
Status Category | Status Badge
-- | --
GitLab Pipelines | [![pipeline status](https://gitlab.com/push.rocks/smartcli/badges/master/pipeline.svg)](https://lossless.cloud)
GitLab Pipline Test Coverage | [![coverage report](https://gitlab.com/push.rocks/smartcli/badges/master/coverage.svg)](https://lossless.cloud)
npm | [![npm downloads per month](https://badgen.net/npm/dy/@push.rocks/smartcli)](https://lossless.cloud)
Snyk | [![Known Vulnerabilities](https://badgen.net/snyk/push.rocks/smartcli)](https://lossless.cloud)
TypeScript Support | [![TypeScript](https://badgen.net/badge/TypeScript/>=%203.x/blue?icon=typescript)](https://lossless.cloud)
node Support | [![node](https://img.shields.io/badge/node->=%2010.x.x-blue.svg)](https://nodejs.org/dist/latest-v10.x/docs/api/)
Code Style | [![Code Style](https://badgen.net/badge/style/prettier/purple)](https://lossless.cloud)
PackagePhobia (total standalone install weight) | [![PackagePhobia](https://badgen.net/packagephobia/install/@push.rocks/smartcli)](https://lossless.cloud)
PackagePhobia (package size on registry) | [![PackagePhobia](https://badgen.net/packagephobia/publish/@push.rocks/smartcli)](https://lossless.cloud)
BundlePhobia (total size when bundled) | [![BundlePhobia](https://badgen.net/bundlephobia/minzip/@push.rocks/smartcli)](https://lossless.cloud)
## Usage
this plugin tries to establish some logic in which CLI tools work.
take the following commandline input:
```
mytool command argument1 argument2 --option1 -o2 option2Value
```
- `mytool` obviously is the tool (like git)
- `command` is the main thing the tool shall do (like commit)
- `argument1` and `argument2` are arguments
- `option1` is a longform option you can add (like --message for message)
- `optionValue` is the referenced option value (like a commit message)
When there is no command and no option specified the standardTask applied.
When there is a option specified but no command, standardTask applies,
except when of the options is -v, --version or --help.
```javascript
import { Smartcli } from 'smartcli';
mySmartcli = new Smartcli();
mySmartcli.standardTask().then((argvArg) => {
// do something if program is called without an command
});
mySmartcli.addCommand({ commandname: 'install' }).then((argvArg) => {
// do something if program is called with command "install"
});
mySmartcli.addVersion('1.0.0'); // -v and --version options will display the specified version in the terminal
mySmartCli.addHelp({
// is triggered by help command and --help option
helpText: 'some help text to print', // the helpText to display
});
mySmartcli.startParse(); // starts the evaluation and fullfills or rejects promises.
```
## Contribution
We are always happy for code contributions. If you are not the code contributing type that is ok. Still, maintaining Open Source repositories takes considerable time and thought. If you like the quality of what we do and our modules are useful to you we would appreciate a little monthly contribution: You can [contribute one time](https://lossless.link/contribute-onetime) or [contribute monthly](https://lossless.link/contribute). :)
For further information read the linked docs at the top of this readme.
## Legal
> MIT licensed | **©** [Task Venture Capital GmbH](https://task.vc)
| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy)

View File

@ -1,43 +1,42 @@
import { tap, expect } from '@pushrocks/tapbundle'; import { tap, expect } from '@push.rocks/tapbundle';
import { Subject } from 'rxjs'; import * as smartrx from '@push.rocks/smartrx';
import smartcli = require('../ts/index'); import * as smartcli from '../ts/index.js';
let smartCliTestObject: smartcli.Smartcli;
tap.test('should create a new Smartcli', async () => { tap.test('should create a new Smartcli', async () => {
smartCliTestObject = new smartcli.Smartcli(); const 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 (toolsArg) => {
expect(smartCliTestObject.addCommand('awesome')).to.be.instanceOf(Subject); const done = toolsArg.defer();
const smartCliTestObject = new smartcli.Smartcli();
const awesomeCommandSubject = smartCliTestObject.addCommand('awesome');
expect(awesomeCommandSubject).toBeInstanceOf(smartrx.rxjs.Subject);
awesomeCommandSubject.subscribe(() => {
done.resolve();
});
console.log(process.argv);
process.argv.splice(2, 0, 'awesome');
console.log(process.argv);
smartCliTestObject.startParse();
await done.promise;
}); });
tap.test('should start parsing a standardTask', async () => { tap.test('should start parsing a standardTask', async () => {
expect(smartCliTestObject.standardTask()).to.be.instanceOf(Subject); const smartCliTestObject = new smartcli.Smartcli();
expect(smartCliTestObject.standardCommand()).toBeInstanceOf(smartrx.rxjs.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(() => { const smartCliTestObject = new smartcli.Smartcli();
smartCliTestObject.addCommand('triggerme').subscribe(() => {
hasExecuted = true; hasExecuted = true;
}); });
}); smartCliTestObject.triggerCommand('triggerme', {});
expect(hasExecuted).toBeTrue();
tap.test('should not have executed yet', async () => {
expect(hasExecuted).to.be.false;
});
tap.test('should execute when triggered', async () => {
smartCliTestObject.trigger('triggerme');
expect(hasExecuted).be.true;
});
tap.test('should start parsing the CLI input', async () => {
smartCliTestObject.startParse();
expect(smartCliTestObject.parseStarted.promise).to.be.instanceOf(Promise);
}); });
tap.start(); tap.start();

8
ts/00_commitinfo_data.ts Normal file
View File

@ -0,0 +1,8 @@
/**
* autocreated commitinfo by @pushrocks/commitinfo
*/
export const commitinfo = {
name: '@push.rocks/smartcli',
version: '4.0.7',
description: 'easy observable cli tasks'
}

View File

@ -1 +1 @@
export { Smartcli } from './smartcli.classes.smartcli'; export { Smartcli } from './smartcli.classes.smartcli.js';

View File

@ -1,107 +1,77 @@
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 { const logger = new plugins.smartlog.ConsoleLog();
triggerName: string;
subject: Subject<any>;
}
/** /**
* 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 onlyOnProcessEnvCliCall = false;
/** /**
* 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)
*/
onlyTriggerOnProcessEnvCliCall() {
this.onlyOnProcessEnvCliCall = true;
}
/** /**
* 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: string, aliasArg: string): 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> {
let 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) { });
let 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) {
let 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) {
@ -114,52 +84,77 @@ 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) => {
plugins.smartlog.defaultLogger.log('info', optionsArg.helpText); logger.log('info', optionsArg.helpText);
}); });
} }
/** /**
* 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.parseStarted.promise.then(() => {
if (this.argv.v) {
console.log(this.version);
}
});
} }
/** /**
* 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> {
let standardSubject = this.addTrigger('standardTask'); const standardSubject = this.addCommand('standardCommand');
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');
}
}
});
return standardSubject; return standardSubject;
} }
/** /**
* start the process of evaluating commands * start the process of evaluating commands
*/ */
startParse(): void { public startParse(): void {
this.argv = this.argv.argv; const parsedYArgs = plugins.yargsParser(process.argv);
this.parseStarted.resolve();
// lets handle commands
let counter = 0;
let foundCommand = false;
parsedYArgs._ = parsedYArgs._.filter((commandPartArg) => {
counter++;
if (typeof commandPartArg === 'number') {
return true;
}
if (counter <= 2 && !foundCommand) {
const isPath = commandPartArg.startsWith('/');
foundCommand = !isPath;
return foundCommand;
} else {
return true;
}
});
const wantedCommand = parsedYArgs._[0];
// lets handle some standards
if (!wantedCommand && (parsedYArgs.v || parsedYArgs.version)) {
console.log(this.version || 'unknown version');
return;
}
console.log(`Wanted command: ${wantedCommand}`);
for (const command of this.commandObservableMap.getArray()) {
if (!wantedCommand) {
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;
} }
} }

View File

@ -1,7 +1,14 @@
import * as yargs from 'yargs'; // @pushrocks scope
import * as smartlog from '@pushrocks/smartlog'; import * as smartlog from '@push.rocks/smartlog';
import * as lik from '@pushrocks/lik'; import * as lik from '@push.rocks/lik';
import * as path from 'path'; import * as path from 'path';
import * as smartparam from '@pushrocks/smartparam'; import * as smartparam from '@push.rocks/smartparam';
import * as smartpromise from '@push.rocks/smartpromise';
import * as smartrx from '@push.rocks/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
View File

@ -0,0 +1,10 @@
{
"compilerOptions": {
"experimentalDecorators": true,
"useDefineForClassFields": false,
"target": "ES2022",
"module": "ES2022",
"moduleResolution": "nodenext",
"esModuleInterop": true
}
}

View File

@ -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"
}