Compare commits
110 Commits
Author | SHA1 | Date | |
---|---|---|---|
adfda70522 | |||
c701e3e04c | |||
7b1de5b31d | |||
7908fd8cfd | |||
21bd0c9279 | |||
9d1108e40d | |||
390e0cb491 | |||
032fd0c2fd | |||
440881c3d8 | |||
f208121e2c | |||
7c4ae84871 | |||
668f6c3e16 | |||
b1e08aad1f | |||
f1ab614cdf | |||
995c808512 | |||
28acb867a0 | |||
3148a50d43 | |||
41c99de4d8 | |||
a91f56dacf | |||
f60f17f91e | |||
d154cf0d0f | |||
a6e0fa65e0 | |||
c7e940f597 | |||
45d3ce8ffc | |||
ce121b8b7f | |||
ce65b8d7c9 | |||
9acdfca460 | |||
59bcd8dadf | |||
b6375fd8fa | |||
8183417c90 | |||
5e66d35125 | |||
3ff4c3ff2f | |||
6508b29bfc | |||
66fd7138ab | |||
f3ce1c1408 | |||
d2b84acc55 | |||
ce008da9ad | |||
f0f1f9b86f | |||
089787454a | |||
f8a122b777 | |||
c6db092062 | |||
857d31dcb2 | |||
e257a38688 | |||
19a5082381 | |||
00f5539e6b | |||
cacb0221f1 | |||
b98b90163d | |||
daa6312aea | |||
7f2dab091f | |||
dd293875c4 | |||
120eca42ac | |||
fc289616f6 | |||
e7c1c1c45b | |||
f33c759fa8 | |||
1185df362b | |||
36de8e11f0 | |||
74ffb3aa87 | |||
96a6d01720 | |||
7833bd0be8 | |||
7ca18c4a46 | |||
b98e2a1a62 | |||
e102203422 | |||
92a37cf29b | |||
8408d1f3b5 | |||
d88e05466b | |||
3a789d052c | |||
d76d829e46 | |||
f8157ab8aa | |||
5c67015779 | |||
b67e3c0de1 | |||
c7a647d4ef | |||
9e5fb7a13a | |||
c115d222c0 | |||
643514c64a | |||
19498f7b11 | |||
3afadd9045 | |||
0889423a24 | |||
86b4cf4cc3 | |||
4cb4eabef4 | |||
bad0971f1c | |||
016f03bd8a | |||
63a9da11be | |||
ab18c75b28 | |||
088d4ef11b | |||
5ee866d9c6 | |||
d65061aaaa | |||
506bb0cdaf | |||
834a6c2953 | |||
e1c2de8a40 | |||
9e76f2afb1 | |||
cf78839e12 | |||
8733a601b4 | |||
fdd1761333 | |||
542c77069f | |||
96d82c3a20 | |||
ab8ce876a3 | |||
de4d3bcf3a | |||
75ab5552e1 | |||
eb3d9d722f | |||
c32f41cc23 | |||
cf03c6681b | |||
204c7105e8 | |||
ac0681507a | |||
d17053532b | |||
65d50ab238 | |||
7eed737120 | |||
8f72b3f2af | |||
fea330b8ff | |||
b51bbb00a4 | |||
5bd32f03c5 |
8
.gitignore
vendored
8
.gitignore
vendored
@ -1,6 +1,4 @@
|
||||
node_modules/
|
||||
.settings/
|
||||
.idea/
|
||||
|
||||
ts/*.js
|
||||
ts/*.js.map
|
||||
coverage/
|
||||
public/
|
||||
.yarn/
|
||||
|
141
.gitlab-ci.yml
Normal file
141
.gitlab-ci.yml
Normal file
@ -0,0 +1,141 @@
|
||||
# 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 command npm install -g snyk
|
||||
- npmci command npm install --ignore-scripts
|
||||
- npmci command snyk test
|
||||
tags:
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
# ====================
|
||||
# test stage
|
||||
# ====================
|
||||
testLEGACY:
|
||||
stage: test
|
||||
script:
|
||||
- npmci node install legacy
|
||||
- npmci npm install
|
||||
- npmci npm test
|
||||
coverage: /\d+.?\d+?\%\s*coverage/
|
||||
tags:
|
||||
- docker
|
||||
- notpriv
|
||||
allow_failure: true
|
||||
|
||||
testLTS:
|
||||
stage: test
|
||||
script:
|
||||
- npmci node install lts
|
||||
- npmci npm install
|
||||
- npmci npm test
|
||||
coverage: /\d+.?\d+?\%\s*coverage/
|
||||
tags:
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
testSTABLE:
|
||||
stage: test
|
||||
script:
|
||||
- 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 npmpage
|
||||
- npmci command npmpage
|
||||
tags:
|
||||
- docker
|
||||
- notpriv
|
||||
only:
|
||||
- tags
|
||||
artifacts:
|
||||
expire_in: 1 week
|
||||
paths:
|
||||
- public
|
||||
allow_failure: true
|
||||
|
||||
windowsCompatibility:
|
||||
image: stefanscherer/node-windows:10-build-tools
|
||||
stage: metadata
|
||||
script:
|
||||
- npm install & npm test
|
||||
coverage: /\d+.?\d+?\%\s*coverage/
|
||||
tags:
|
||||
- windows
|
||||
allow_failure: true
|
6
.idea/vcs.xml
generated
6
.idea/vcs.xml
generated
@ -1,6 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
20
.travis.yml
20
.travis.yml
@ -1,20 +0,0 @@
|
||||
language: node_js
|
||||
before_install:
|
||||
- nvm install stable
|
||||
- node -v
|
||||
- npm -v
|
||||
- npm install -g gulp
|
||||
- npm install gulp
|
||||
- npm install gulp-typescript
|
||||
deploy:
|
||||
provider: npm
|
||||
email: npm@smart-coordination.com
|
||||
api_key:
|
||||
secure: XAtA2XHAr96ILKpm1VnB/Nff9hiQv3FUa/NDfGZgvJKimuF/TwU2+kXyAag/LlEQGckZ9/IDx0PTIe8x4wOvg2VyKceYktGt6G5/MHeAXwzkkM253k2CKweVtBgnpZ2doTZXeeV/h7hHuEWZbWRXIVCgM6/inaaNe4cMRGCWqwb1xmw6D8VkLpNo6PiB37gR4GfMokasgVHZEVMg2evWKk0moREGW9XKqoE6PS9TPHPHerdqFqfbmeOq3nNgHrJZkNsAJ3PPESWCe/NGEBwtYDKrPI3QlouvXmxScI9/p2VCQmdFoUxKMDsVzBdRMrCx+XxoS8x81HxubZSpnvzUlzGtznZi7e6OyAlI7xXELIO551AH4V/pNWWUPATX+7vXVDGh7aAZYme/fejisjO5Tk9aqRvohh2jCDml3ED6alHHfI0oX5UvBpOE+XwGTIJ8qpf9RrLP5DC27JWE3fqY/WoqIZX7phoKeKYLl+R7Iln/dfvepbMBgcesnBf4PuMh3Qm2l2G1bElJ/hniV1dgggwjcg50nA0Ee9sCnORPxxjdDIMwJw//NJSe+Aq5Fm1ure9oNiBYOeF0bLVQyfGIC8J8coy1xdqv2klKnGHjMMN44bjNlmDp00SaRpbdDuavyocosQVpzfA+hY2QtGV5TXFv6TLgcMYwf8gNyVNkSGE=
|
||||
on:
|
||||
tags: true
|
||||
repo: pushrocks/smartcli
|
||||
notifications:
|
||||
slack:
|
||||
secure: f5Uss0z9RPl/QcA/DroB8loyE93aOYI6bqCkrsiUscmZtlv/TVQtT4dxqGA6uvcG6iTQDBi3Ul88dQxWkRm4IqbhY35/iMaV2dHW4FVYMAh8GQMbsfL2sALCcufxD9blw47awv3iFcwhV1EeyesscjgL0JIjduk96v/7G/6QIO2838M1lzlgtj+kRUkim8qkaEs1je3gRrhMUIjLuAdscMXyUKYFMjWo9ACSjVUl30R/ZNemb18itIja6i92GotreBgcfEMczvy58ovDC7xdJUsY8LjMI01DwY+WPRnI0tAhsuI8moBwwcdM4e3bAjKjucQRjO33O5bMWRZ6QCiYd0DnCEFyCPQLJ4GSy/tkD00n8ijLHAOSV3AH1zNbdK1EAdSPQXDvlI36KJn/2hyQLoitGHVUPr76ujJWP82ypO2tgIp3XQU0dJVCxDuHnwJO2+hjdI+gCPqxNTpjeujHx3UdkTGNRjuuf9dlZ/D08fApjYxy2fxItTqo3QjP/nrqvBXUOPP8yPHpjIT4H2t5Pr4SJjBGI6X4qhKyFj6s9rA/Xu1rL+45zu1C3uC3z+u3T9UwrbzJ/cZM6r6UQvQmUvIfBNaMlg4I/diQCDIPL+Rhop2nylY3IcHmJnk2itn7kOqj1tohCpFEml5pRuSZy4udWywkdtyBAsHWFLF7oiQ=
|
||||
email: false
|
68
README.md
68
README.md
@ -1,57 +1,29 @@
|
||||
# smartcli
|
||||
nodejs wrapper for CLI related tasks
|
||||
[](https://github.com/pushrocks/smartcli/commits/dev)
|
||||
|
||||
## Buildstatus/Dependencies
|
||||
[](https://travis-ci.org/pushrocks/smartcli)
|
||||
[](https://david-dm.org/pushrocks/smartcli#info=devDependencies)
|
||||
nodejs wrapper for CLI related tasks
|
||||
|
||||
## Install the package
|
||||
npm install smartcli
|
||||
## Availabililty
|
||||
|
||||
## Usage
|
||||
[](https://www.npmjs.com/package/smartcli)
|
||||
[](https://GitLab.com/pushrocks/smartcli)
|
||||
[](https://github.com/pushrocks/smartcli)
|
||||
[](https://pushrocks.gitlab.io/smartcli/)
|
||||
|
||||
this plugin tries to establish some logic in which CLI tools work.
|
||||
## Status for master
|
||||
|
||||
take the following commandline input:
|
||||
[](https://GitLab.com/pushrocks/smartcli/commits/master)
|
||||
[](https://GitLab.com/pushrocks/smartcli/commits/master)
|
||||
[](https://www.npmjs.com/package/smartcli)
|
||||
[](https://david-dm.org/pushrocks/smartcli)
|
||||
[](https://www.bithound.io/github/pushrocks/smartcli/master/dependencies/npm)
|
||||
[](https://www.bithound.io/github/pushrocks/smartcli)
|
||||
[](https://nodejs.org/dist/latest-v6.x/docs/api/)
|
||||
[](https://nodejs.org/dist/latest-v6.x/docs/api/)
|
||||
[](http://standardjs.com/)
|
||||
|
||||
```
|
||||
mytool function argument1 argument2 --option1 option1Value --option2 option2Value
|
||||
```
|
||||
For further information read the linked docs at the top of this README.
|
||||
|
||||
* 'mytool' obviously is the tool (like git)
|
||||
* function is the main thing the tool shall do (like commit)
|
||||
* option is an option you can add (like -m for message)
|
||||
* optionValue is the referenced option value (like a commit message)
|
||||
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)
|
||||
> | By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy.html)
|
||||
|
||||
|
||||
### The inner organization of smartcli
|
||||
**smartcli** exposes three major groups of functions:
|
||||
|
||||
* check functions
|
||||
* are grouped in **smartcli.checks** object
|
||||
* get functions
|
||||
* are grouped in **smartcli.get** object
|
||||
* async interaction functions
|
||||
* are grouped in **smartcli.interaction** object
|
||||
|
||||
```js
|
||||
var smartcli = require("smartcli");
|
||||
|
||||
/* -------------- Check Functions -------------------*/
|
||||
//returns true for terminal command "node myjs.js jazz"
|
||||
smartcli.check.command('jazz');
|
||||
|
||||
/**
|
||||
* returns an object for terminal command "node myjs.js --myoption something" like so
|
||||
* {
|
||||
* name: 'myoption',
|
||||
* specified: true,
|
||||
* value: 'something'
|
||||
* }
|
||||
*/
|
||||
smartcli.get.option('myoption');
|
||||
```
|
||||
|
||||
Cheers
|
||||
Phil from Lossless Digital
|
||||
[](https://)
|
||||
|
1
dist/index.d.ts
vendored
Normal file
1
dist/index.d.ts
vendored
Normal file
@ -0,0 +1 @@
|
||||
export { Smartcli } from './smartcli.classes.smartcli';
|
5
dist/index.js
vendored
Normal file
5
dist/index.js
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
var smartcli_classes_smartcli_1 = require("./smartcli.classes.smartcli");
|
||||
exports.Smartcli = smartcli_classes_smartcli_1.Smartcli;
|
||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOztBQUFBLHlFQUF1RDtBQUE5QywrQ0FBQSxRQUFRLENBQUEifQ==
|
72
dist/smartcli.classes.smartcli.d.ts
vendored
Normal file
72
dist/smartcli.classes.smartcli.d.ts
vendored
Normal file
@ -0,0 +1,72 @@
|
||||
import * as smartq from 'smartq';
|
||||
import { Subject } from 'rxjs';
|
||||
import * as plugins from './smartcli.plugins';
|
||||
export interface ICommandPromiseObject {
|
||||
commandName: string;
|
||||
promise: Promise<void>;
|
||||
}
|
||||
export interface ITriggerObservableObject {
|
||||
triggerName: string;
|
||||
subject: Subject<any>;
|
||||
}
|
||||
/**
|
||||
* class to create a new instance of Smartcli. Handles parsing of command line arguments.
|
||||
*/
|
||||
export declare class Smartcli {
|
||||
argv: any;
|
||||
questionsDone: any;
|
||||
parseStarted: smartq.Deferred<any>;
|
||||
commands: any;
|
||||
questions: any;
|
||||
version: string;
|
||||
private onlyOnProcessEnvCliCall;
|
||||
/**
|
||||
* map of all Trigger/Observable objects to keep track
|
||||
*/
|
||||
allTriggerObservablesMap: plugins.lik.Objectmap<ITriggerObservableObject>;
|
||||
/**
|
||||
* The constructor of Smartcli
|
||||
*/
|
||||
constructor();
|
||||
/**
|
||||
* halts any execution of commands if (process.env.CLI_CALL === false)
|
||||
*/
|
||||
onlyTriggerOnProcessEnvCliCall(): void;
|
||||
/**
|
||||
* adds an alias, meaning one equals the other in terms of command execution.
|
||||
*/
|
||||
addCommandAlias(keyArg: any, aliasArg: any): void;
|
||||
/**
|
||||
* 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>;
|
||||
/**
|
||||
* adds a Trigger. Like addCommand(), but returns an subscribable observable
|
||||
*/
|
||||
addTrigger(triggerNameArg: string): Subject<any>;
|
||||
/**
|
||||
* execute trigger by name
|
||||
* @param commandNameArg - the name of the command to trigger
|
||||
*/
|
||||
trigger(triggerName: string): Subject<any>;
|
||||
getTriggerSubject(triggerName: string): Subject<any>;
|
||||
/**
|
||||
* allows to specify help text to be printed above the rest of the help text
|
||||
*/
|
||||
addHelp(optionsArg: {
|
||||
helpText: string;
|
||||
}): void;
|
||||
/**
|
||||
* specify version to be printed for -v --version
|
||||
*/
|
||||
addVersion(versionArg: string): void;
|
||||
/**
|
||||
* adds a trigger that is called when no command is specified
|
||||
*/
|
||||
standardTask(): Subject<any>;
|
||||
/**
|
||||
* start the process of evaluating commands
|
||||
*/
|
||||
startParse(): void;
|
||||
}
|
139
dist/smartcli.classes.smartcli.js
vendored
Normal file
139
dist/smartcli.classes.smartcli.js
vendored
Normal file
@ -0,0 +1,139 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const smartq = require("smartq");
|
||||
const rxjs_1 = require("rxjs");
|
||||
const plugins = require("./smartcli.plugins");
|
||||
// import classes
|
||||
const lik_1 = require("lik");
|
||||
/**
|
||||
* class to create a new instance of Smartcli. Handles parsing of command line arguments.
|
||||
*/
|
||||
class Smartcli {
|
||||
/**
|
||||
* The constructor of Smartcli
|
||||
*/
|
||||
constructor() {
|
||||
this.onlyOnProcessEnvCliCall = false;
|
||||
/**
|
||||
* map of all Trigger/Observable objects to keep track
|
||||
*/
|
||||
this.allTriggerObservablesMap = new lik_1.Objectmap();
|
||||
this.argv = plugins.yargs;
|
||||
this.questionsDone = smartq.defer();
|
||||
this.parseStarted = smartq.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.
|
||||
*/
|
||||
addCommandAlias(keyArg, aliasArg) {
|
||||
this.argv = this.argv.alias(keyArg, aliasArg);
|
||||
return;
|
||||
}
|
||||
/**
|
||||
* 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) {
|
||||
let 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) {
|
||||
let triggerSubject = new rxjs_1.Subject();
|
||||
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
|
||||
* @param commandNameArg - the name of the command to trigger
|
||||
*/
|
||||
trigger(triggerName) {
|
||||
let triggerSubject = this.getTriggerSubject(triggerName);
|
||||
triggerSubject.next(this.argv);
|
||||
return triggerSubject;
|
||||
}
|
||||
getTriggerSubject(triggerName) {
|
||||
const triggerObservableObject = this.allTriggerObservablesMap.find(triggerObservableObjectArg => {
|
||||
return triggerObservableObjectArg.triggerName === triggerName;
|
||||
});
|
||||
if (triggerObservableObject) {
|
||||
return triggerObservableObject.subject;
|
||||
}
|
||||
else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* allows to specify help text to be printed above the rest of the help text
|
||||
*/
|
||||
addHelp(optionsArg) {
|
||||
this.addCommand('help').subscribe(argvArg => {
|
||||
plugins.beautylog.log(optionsArg.helpText);
|
||||
});
|
||||
}
|
||||
/**
|
||||
* specify version to be printed for -v --version
|
||||
*/
|
||||
addVersion(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
|
||||
*/
|
||||
standardTask() {
|
||||
let standardSubject = this.addTrigger('standardTask');
|
||||
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;
|
||||
}
|
||||
/**
|
||||
* start the process of evaluating commands
|
||||
*/
|
||||
startParse() {
|
||||
this.argv = this.argv.argv;
|
||||
this.parseStarted.resolve();
|
||||
return;
|
||||
}
|
||||
}
|
||||
exports.Smartcli = Smartcli;
|
||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRjbGkuY2xhc3Nlcy5zbWFydGNsaS5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL3NtYXJ0Y2xpLmNsYXNzZXMuc21hcnRjbGkudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7QUFBQSxpQ0FBaUM7QUFDakMsK0JBQStCO0FBRS9CLDhDQUE4QztBQUU5QyxpQkFBaUI7QUFDakIsNkJBQWdDO0FBYWhDOztHQUVHO0FBQ0g7SUFjRTs7T0FFRztJQUNIO1FBVlEsNEJBQXVCLEdBQUcsS0FBSyxDQUFDO1FBRXhDOztXQUVHO1FBQ0gsNkJBQXdCLEdBQUcsSUFBSSxlQUFTLEVBQTRCLENBQUM7UUFNbkUsSUFBSSxDQUFDLElBQUksR0FBRyxPQUFPLENBQUMsS0FBSyxDQUFDO1FBQzFCLElBQUksQ0FBQyxhQUFhLEdBQUcsTUFBTSxDQUFDLEtBQUssRUFBRSxDQUFDO1FBQ3BDLElBQUksQ0FBQyxZQUFZLEdBQUcsTUFBTSxDQUFDLEtBQUssRUFBRSxDQUFDO0lBQ3JDLENBQUM7SUFFRDs7T0FFRztJQUNILDhCQUE4QjtRQUM1QixJQUFJLENBQUMsdUJBQXVCLEdBQUcsSUFBSSxDQUFDO0lBQ3RDLENBQUM7SUFFRDs7T0FFRztJQUNILGVBQWUsQ0FBQyxNQUFNLEVBQUUsUUFBUTtRQUM5QixJQUFJLENBQUMsSUFBSSxHQUFHLElBQUksQ0FBQyxJQUFJLENBQUMsS0FBSyxDQUFDLE1BQU0sRUFBRSxRQUFRLENBQUMsQ0FBQztRQUM5QyxPQUFPO0lBQ1QsQ0FBQztJQUVEOzs7T0FHRztJQUNILFVBQVUsQ0FBQyxjQUFzQjtRQUMvQixJQUFJLGNBQWMsR0FBRyxJQUFJLENBQUMsVUFBVSxDQUFDLGNBQWMsQ0FBQyxDQUFDO1FBQ3JELElBQUksQ0FBQyxZQUFZLENBQUMsT0FBTyxDQUFDLElBQUksQ0FBQyxHQUFHLEVBQUU7WUFDbEMsSUFBSSxJQUFJLENBQUMsSUFBSSxDQUFDLENBQUMsQ0FBQyxPQUFPLENBQUMsY0FBYyxDQUFDLEtBQUssQ0FBQyxFQUFFO2dCQUM3QyxJQUFJLENBQUMsT0FBTyxDQUFDLGNBQWMsQ0FBQyxDQUFDO2FBQzlCO1FBQ0gsQ0FBQyxDQUFDLENBQUM7UUFDSCxPQUFPLGNBQWMsQ0FBQztJQUN4QixDQUFDO0lBRUQ7O09BRUc7SUFDSCxVQUFVLENBQUMsY0FBc0I7UUFDL0IsSUFBSSxjQUFjLEdBQUcsSUFBSSxjQUFPLEVBQU8sQ0FBQztRQUN4QyxJQUFJLENBQUMsSUFBSSxDQUFDLGlCQUFpQixDQUFDLGNBQWMsQ0FBQyxFQUFFO1lBQzNDLElBQUksQ0FBQyx3QkFBd0IsQ0FBQyxHQUFHLENBQUM7Z0JBQ2hDLFdBQVcsRUFBRSxjQUFjO2dCQUMzQixPQUFPLEVBQUUsY0FBYzthQUN4QixDQUFDLENBQUM7U0FDSjthQUFNO1lBQ0wsTUFBTSxJQUFJLEtBQUssQ0FBQywrQkFBK0IsQ0FBQyxDQUFDO1NBQ2xEO1FBQ0QsT0FBTyxjQUFjLENBQUM7SUFDeEIsQ0FBQztJQUVEOzs7T0FHRztJQUNILE9BQU8sQ0FBQyxXQUFtQjtRQUN6QixJQUFJLGNBQWMsR0FBRyxJQUFJLENBQUMsaUJBQWlCLENBQUMsV0FBVyxDQUFDLENBQUM7UUFDekQsY0FBYyxDQUFDLElBQUksQ0FBQyxJQUFJLENBQUMsSUFBSSxDQUFDLENBQUM7UUFDL0IsT0FBTyxjQUFjLENBQUM7SUFDeEIsQ0FBQztJQUVELGlCQUFpQixDQUFDLFdBQW1CO1FBQ25DLE1BQU0sdUJBQXVCLEdBQUcsSUFBSSxDQUFDLHdCQUF3QixDQUFDLElBQUksQ0FDaEUsMEJBQTBCLENBQUMsRUFBRTtZQUMzQixPQUFPLDBCQUEwQixDQUFDLFdBQVcsS0FBSyxXQUFXLENBQUM7UUFDaEUsQ0FBQyxDQUNGLENBQUM7UUFDRixJQUFJLHVCQUF1QixFQUFFO1lBQzNCLE9BQU8sdUJBQXVCLENBQUMsT0FBTyxDQUFDO1NBQ3hDO2FBQU07WUFDTCxPQUFPLElBQUksQ0FBQztTQUNiO0lBQ0gsQ0FBQztJQUVEOztPQUVHO0lBQ0gsT0FBTyxDQUFDLFVBQWdDO1FBQ3RDLElBQUksQ0FBQyxVQUFVLENBQUMsTUFBTSxDQUFDLENBQUMsU0FBUyxDQUFDLE9BQU8sQ0FBQyxFQUFFO1lBQzFDLE9BQU8sQ0FBQyxTQUFTLENBQUMsR0FBRyxDQUFDLFVBQVUsQ0FBQyxRQUFRLENBQUMsQ0FBQztRQUM3QyxDQUFDLENBQUMsQ0FBQztJQUNMLENBQUM7SUFFRDs7T0FFRztJQUNILFVBQVUsQ0FBQyxVQUFrQjtRQUMzQixJQUFJLENBQUMsT0FBTyxHQUFHLFVBQVUsQ0FBQztRQUMxQixJQUFJLENBQUMsZUFBZSxDQUFDLEdBQUcsRUFBRSxTQUFTLENBQUMsQ0FBQztRQUNyQyxJQUFJLENBQUMsWUFBWSxDQUFDLE9BQU8sQ0FBQyxJQUFJLENBQUMsR0FBRyxFQUFFO1lBQ2xDLElBQUksSUFBSSxDQUFDLElBQUksQ0FBQyxDQUFDLEVBQUU7Z0JBQ2YsT0FBTyxDQUFDLEdBQUcsQ0FBQyxJQUFJLENBQUMsT0FBTyxDQUFDLENBQUM7YUFDM0I7UUFDSCxDQUFDLENBQUMsQ0FBQztJQUNMLENBQUM7SUFFRDs7T0FFRztJQUNILFlBQVk7UUFDVixJQUFJLGVBQWUsR0FBRyxJQUFJLENBQUMsVUFBVSxDQUFDLGNBQWMsQ0FBQyxDQUFDO1FBQ3RELElBQUksQ0FBQyxZQUFZLENBQUMsT0FBTyxDQUFDLElBQUksQ0FBQyxHQUFHLEVBQUU7WUFDbEMsSUFBSSxJQUFJLENBQUMsSUFBSSxDQUFDLENBQUMsQ0FBQyxNQUFNLEtBQUssQ0FBQyxJQUFJLENBQUMsSUFBSSxDQUFDLElBQUksQ0FBQyxDQUFDLEVBQUU7Z0JBQzVDLElBQUksSUFBSSxDQUFDLHVCQUF1QixFQUFFO29CQUNoQyxJQUFJLE9BQU8sQ0FBQyxHQUFHLENBQUMsUUFBUSxLQUFLLE1BQU0sRUFBRTt3QkFDbkMsSUFBSSxDQUFDLE9BQU8sQ0FBQyxjQUFjLENBQUMsQ0FBQztxQkFDOUI7eUJBQU07d0JBQ0wsT0FBTztxQkFDUjtpQkFDRjtxQkFBTTtvQkFDTCxJQUFJLENBQUMsT0FBTyxDQUFDLGNBQWMsQ0FBQyxDQUFDO2lCQUM5QjthQUNGO1FBQ0gsQ0FBQyxDQUFDLENBQUM7UUFDSCxPQUFPLGVBQWUsQ0FBQztJQUN6QixDQUFDO0lBRUQ7O09BRUc7SUFDSCxVQUFVO1FBQ1IsSUFBSSxDQUFDLElBQUksR0FBRyxJQUFJLENBQUMsSUFBSSxDQUFDLElBQUksQ0FBQztRQUMzQixJQUFJLENBQUMsWUFBWSxDQUFDLE9BQU8sRUFBRSxDQUFDO1FBQzVCLE9BQU87SUFDVCxDQUFDO0NBQ0Y7QUE5SUQsNEJBOElDIn0=
|
6
dist/smartcli.plugins.d.ts
vendored
Normal file
6
dist/smartcli.plugins.d.ts
vendored
Normal file
@ -0,0 +1,6 @@
|
||||
import * as yargs from 'yargs';
|
||||
import * as beautylog from 'beautylog';
|
||||
import * as lik from 'lik';
|
||||
import * as path from 'path';
|
||||
import * as smartparam from 'smartparam';
|
||||
export { yargs, beautylog, lik, path, smartparam };
|
13
dist/smartcli.plugins.js
vendored
Normal file
13
dist/smartcli.plugins.js
vendored
Normal file
@ -0,0 +1,13 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const yargs = require("yargs");
|
||||
exports.yargs = yargs;
|
||||
const beautylog = require("beautylog");
|
||||
exports.beautylog = beautylog;
|
||||
const lik = require("lik");
|
||||
exports.lik = lik;
|
||||
const path = require("path");
|
||||
exports.path = path;
|
||||
const smartparam = require("smartparam");
|
||||
exports.smartparam = smartparam;
|
||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRjbGkucGx1Z2lucy5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL3NtYXJ0Y2xpLnBsdWdpbnMudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7QUFBQSwrQkFBK0I7QUFNdEIsc0JBQUs7QUFMZCx1Q0FBdUM7QUFLdkIsOEJBQVM7QUFKekIsMkJBQTJCO0FBSUEsa0JBQUc7QUFIOUIsNkJBQTZCO0FBR0csb0JBQUk7QUFGcEMseUNBQXlDO0FBRUgsZ0NBQVUifQ==
|
70
docs/index.md
Normal file
70
docs/index.md
Normal file
@ -0,0 +1,70 @@
|
||||
# smartcli
|
||||
|
||||
nodejs wrapper for CLI related tasks
|
||||
|
||||
## Availabililty
|
||||
|
||||
[](https://www.npmjs.com/package/smartcli)
|
||||
[](https://GitLab.com/pushrocks/smartcli)
|
||||
[](https://github.com/pushrocks/smartcli)
|
||||
[](https://pushrocks.gitlab.io/smartcli/)
|
||||
|
||||
## Status for master
|
||||
|
||||
[](https://GitLab.com/pushrocks/smartcli/commits/master)
|
||||
[](https://GitLab.com/pushrocks/smartcli/commits/master)
|
||||
[](https://www.npmjs.com/package/smartcli)
|
||||
[](https://david-dm.org/pushrocks/smartcli)
|
||||
[](https://www.bithound.io/github/pushrocks/smartcli/master/dependencies/npm)
|
||||
[](https://www.bithound.io/github/pushrocks/smartcli)
|
||||
[](https://nodejs.org/dist/latest-v6.x/docs/api/)
|
||||
[](https://nodejs.org/dist/latest-v6.x/docs/api/)
|
||||
[](http://standardjs.com/)
|
||||
|
||||
## 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.
|
||||
|
||||
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)
|
||||
> | By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy.html)
|
||||
|
||||
[](https://push.rocks)
|
263
index.js
263
index.js
@ -1,263 +0,0 @@
|
||||
/// <reference path="./index.ts" />
|
||||
var smartcliPlugins;
|
||||
(function (smartcliPlugins) {
|
||||
var plugins = {};
|
||||
function init() {
|
||||
plugins.path = require("path");
|
||||
plugins.beautylog = require("beautylog")("os");
|
||||
plugins.cliff = require("cliff");
|
||||
plugins.inquirer = require("inquirer");
|
||||
plugins.smartparam = require("smartparam");
|
||||
plugins.argv = require('yargs').argv;
|
||||
return plugins;
|
||||
}
|
||||
smartcliPlugins.init = init;
|
||||
})(smartcliPlugins || (smartcliPlugins = {}));
|
||||
/// <reference path="index.ts" />
|
||||
/// <reference path="./index.ts" />
|
||||
var SmartcliChecks;
|
||||
(function (SmartcliChecks) {
|
||||
function init() {
|
||||
/**
|
||||
* all functions in smartcli.check return a boolean
|
||||
* @type {{}}
|
||||
*/
|
||||
smartcli.check = {};
|
||||
/**
|
||||
* checks for a special command string and returns true if found.
|
||||
* @param commandString
|
||||
* @returns {boolean}
|
||||
*/
|
||||
smartcli.check.command = function (commandString) {
|
||||
if (plugins.argv._.indexOf(commandString) == 0) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
/**
|
||||
* checks if a command is present, returns true if yes, false if no.
|
||||
* @returns {boolean}
|
||||
*/
|
||||
smartcli.check.commandPresence = function () {
|
||||
if (plugins.argv._.length > 0) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
/**
|
||||
* checks for an special command argument at a certain position, returns true if matches, returns false if not
|
||||
* @param level
|
||||
* @returns {boolean}
|
||||
*/
|
||||
smartcli.check.commandArgument = function (commandArgumentString, level) {
|
||||
if (level === void 0) { level = 1; }
|
||||
if (smartcli.check.commandArgumentPresence(level) && (plugins.argv._[level] == commandArgumentString)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
smartcli.check.commandArgumentPresence = function (level) {
|
||||
if (level === void 0) { level = 1; }
|
||||
if (plugins.argv._.length >= (level + 1)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
/**
|
||||
* checks for a specific option string, returns true if yes, returns false if no
|
||||
* @returns {boolean}
|
||||
*/
|
||||
smartcli.check.option = function (optionString) {
|
||||
if (plugins.smartparam.exists(plugins.argv, optionString)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
smartcli.check.optionPresence = function () {
|
||||
if (plugins.argv.indexOf() != -1) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
}
|
||||
SmartcliChecks.init = init;
|
||||
})(SmartcliChecks || (SmartcliChecks = {}));
|
||||
/// <reference path="./index.ts" />
|
||||
var SmartcliGetters;
|
||||
(function (SmartcliGetters) {
|
||||
function init() {
|
||||
smartcli.get = {};
|
||||
/**
|
||||
*
|
||||
* @param commandString
|
||||
* @returns {{specified: boolean, name: any, arguments: CliCommandArgument}}
|
||||
*/
|
||||
smartcli.get.command = function () {
|
||||
var cliCommand = {
|
||||
specified: smartcli.check.commandPresence(),
|
||||
name: plugins.argv._[0],
|
||||
arguments: smartcli.get.commandArgs()
|
||||
};
|
||||
return cliCommand;
|
||||
};
|
||||
/**
|
||||
* gets the second or higher value of plugins.argv._ if specified and returns it as commandArgument
|
||||
* @param argumentLevel
|
||||
* @returns {{specified: (boolean|boolean), name}}
|
||||
*/
|
||||
smartcli.get.commandArgument = function (argumentLevel) {
|
||||
var commandArgument = {
|
||||
specified: false,
|
||||
name: "undefined",
|
||||
level: argumentLevel
|
||||
};
|
||||
if (argumentLevel < 1) {
|
||||
plugins.beautylog.error("smartcli.get.argument cannot be invoked with an argumentLevel smaller than 1");
|
||||
return commandArgument;
|
||||
}
|
||||
if (smartcli.check.commandArgumentPresence(argumentLevel)) {
|
||||
commandArgument.specified = true;
|
||||
commandArgument.name = plugins.argv._[argumentLevel];
|
||||
}
|
||||
return commandArgument;
|
||||
};
|
||||
/**
|
||||
* returns array with commandArgs
|
||||
* @returns {CliCommandArgument[]}
|
||||
*/
|
||||
smartcli.get.commandArgs = function () {
|
||||
var commandArgs = [];
|
||||
var argsArray = smartcli.get.commandArray().slice(0);
|
||||
argsArray.shift();
|
||||
for (var item in argsArray) {
|
||||
var commandArgument = {
|
||||
specified: true,
|
||||
name: argsArray[item],
|
||||
level: item + 1
|
||||
};
|
||||
commandArgs.push(commandArgument);
|
||||
}
|
||||
return commandArgs;
|
||||
};
|
||||
/**
|
||||
* returns complete command array
|
||||
* @returns {any}
|
||||
*/
|
||||
smartcli.get.commandArray = function () {
|
||||
var commandArray = plugins.argv._;
|
||||
return commandArray;
|
||||
};
|
||||
/**
|
||||
* returns a cli option
|
||||
* @param optionName
|
||||
* @returns {CliOption}
|
||||
*/
|
||||
smartcli.get.option = function (optionName) {
|
||||
var cliOption = {
|
||||
name: optionName,
|
||||
specified: false,
|
||||
value: false
|
||||
};
|
||||
if (plugins.smartparam.exists(plugins.argv, optionName)) {
|
||||
cliOption.name = optionName;
|
||||
cliOption.specified = true;
|
||||
cliOption.value = plugins.argv[optionName]; //we already know from the "if" above that the value is available.
|
||||
}
|
||||
return cliOption;
|
||||
};
|
||||
smartcli.get.options = function () {
|
||||
var options = {};
|
||||
for (var key in plugins.argv) {
|
||||
if (key != "_") {
|
||||
options['key'] = plugins.argv['key'];
|
||||
}
|
||||
}
|
||||
return options;
|
||||
};
|
||||
/**
|
||||
* returns Directory of cwd
|
||||
* @returns {{path: string}}
|
||||
*/
|
||||
smartcli.get.cwd = function () {
|
||||
return {
|
||||
path: process.cwd()
|
||||
};
|
||||
};
|
||||
}
|
||||
SmartcliGetters.init = init;
|
||||
})(SmartcliGetters || (SmartcliGetters = {}));
|
||||
/// <reference path="./index.ts" />
|
||||
var SmartcliInteraction;
|
||||
(function (SmartcliInteraction) {
|
||||
function init() {
|
||||
smartcli.interaction = {};
|
||||
/**
|
||||
* executes callback with answer to question as argument
|
||||
* @param questionString the question you want to ask the user
|
||||
* @param cb the function to execute with answer as param
|
||||
* @returns {null}
|
||||
*/
|
||||
smartcli.interaction.getAnswer = function (questionString, cb) {
|
||||
if (typeof questionString != 'string') {
|
||||
plugins.beautylog.error('no question specified');
|
||||
return null;
|
||||
}
|
||||
//make inquirer compatible question object
|
||||
var question = {
|
||||
type: "input",
|
||||
name: "userFeedback",
|
||||
message: questionString,
|
||||
validate: function (value) {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
plugins.inquirer.prompt([question], function (answers) {
|
||||
var answer = answers.userFeedback;
|
||||
cb(answer);
|
||||
});
|
||||
};
|
||||
/**
|
||||
*
|
||||
* @param questionString
|
||||
* @param choiceOptions
|
||||
* @param cb
|
||||
* @returns {null}
|
||||
*/
|
||||
smartcli.interaction.getChoice = function (questionString, choiceOptions, cb) {
|
||||
if (!Array.isArray(choiceOptions)) {
|
||||
return null;
|
||||
}
|
||||
//make inquirer compatible question object
|
||||
var question = {
|
||||
type: "list",
|
||||
name: "userFeedback",
|
||||
message: questionString,
|
||||
choices: choiceOptions,
|
||||
filter: function (val) { return val.toLowerCase(); }
|
||||
};
|
||||
plugins.inquirer.prompt(question, function (answers) {
|
||||
var answer = answers.userFeedback;
|
||||
cb(answer);
|
||||
});
|
||||
};
|
||||
}
|
||||
SmartcliInteraction.init = init;
|
||||
})(SmartcliInteraction || (SmartcliInteraction = {}));
|
||||
/// <reference path="typings/tsd.d.ts" />
|
||||
/// <reference path="./smartcli.plugins.ts" />
|
||||
/// <reference path="./smartcli.interfaces.ts" />
|
||||
/// <reference path="./smartcli.checks.ts" />
|
||||
/// <reference path="./smartcli.getters.ts" />
|
||||
/// <reference path="./smartcli.interaction.ts" />
|
||||
var plugins = smartcliPlugins.init(); //get all the required npm modules under plugins
|
||||
//define the smartcli object
|
||||
var smartcli = {};
|
||||
//add plugins from above for direct use
|
||||
smartcli.inquirer = plugins.inquirer; //inquirer is for asking questions
|
||||
smartcli.cliff = plugins.cliff; // formats cli output
|
||||
smartcli.argv = plugins.argv; //argv gets initial cli commands and options.
|
||||
//init checks. Checks return boolean. That means they can be used as question with an answer of yes or no.
|
||||
SmartcliChecks.init(); // is defined in smartcli.checks.ts
|
||||
SmartcliGetters.init(); // is defined in smartcli.getters.ts
|
||||
SmartcliInteraction.init(); // is defined in smartcli.interaction.ts
|
||||
module.exports = smartcli; // expose smartcli to outside world
|
9
npmextra.json
Normal file
9
npmextra.json
Normal file
@ -0,0 +1,9 @@
|
||||
{
|
||||
"npmci": {
|
||||
"npmGlobalTools": [
|
||||
"@gitzone/npmts",
|
||||
"ts-node"
|
||||
],
|
||||
"npmAccesslevel": "public"
|
||||
}
|
||||
}
|
1038
package-lock.json
generated
Normal file
1038
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
54
package.json
54
package.json
@ -1,40 +1,48 @@
|
||||
{
|
||||
"name": "smartcli",
|
||||
"version": "0.0.10",
|
||||
"name": "@pushrocks/smartcli",
|
||||
"private": false,
|
||||
"version": "3.0.2",
|
||||
"description": "nodejs wrapper for CLI related tasks",
|
||||
"main": "index.js",
|
||||
"main": "dist/index.js",
|
||||
"typings": "dist/index.d.ts",
|
||||
"scripts": {
|
||||
"test": "(cd ts/compile && gulp) && (node test.js jazz jam --awesome --silent)",
|
||||
"mtest": "(cd ts/compile && gulp) && (node test.js jazz jam --awesome)",
|
||||
"devTest": "(npm test) && (node test.js --test true)",
|
||||
"reinstall": "(rm -r node_modules && npm install)",
|
||||
"release": "(git pull origin master && npm version patch && git push origin master && git checkout release && git merge master && git push origin release && git checkout master)",
|
||||
"startdev": "(git checkout master && git pull origin master)"
|
||||
"test": "(tsrun test/test.ts)",
|
||||
"build": "(npmts)"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/pushrocks/smartcli.git"
|
||||
"url": "https://gitlab.com/pushrocks/smartcli.git"
|
||||
},
|
||||
"keywords": [
|
||||
"json",
|
||||
"jade",
|
||||
"template"
|
||||
"cli",
|
||||
"promise",
|
||||
"task",
|
||||
"push.rocks"
|
||||
],
|
||||
"author": "Smart Coordination GmbH <office@push.rocks> (https://push.rocks)",
|
||||
"author": "Lossless GmbH <office@lossless.com> (https://lossless.com)",
|
||||
"license": "MIT",
|
||||
"bugs": {
|
||||
"url": "https://github.com/pushrocks/smartcli/issues"
|
||||
"url": "https://gitlab.com/pushrocks/smartcli/issues"
|
||||
},
|
||||
"homepage": "https://github.com/pushrocks/smartcli",
|
||||
"homepage": "https://gitlab.com/pushrocks/smartcli",
|
||||
"dependencies": {
|
||||
"beautylog": "1.0.2",
|
||||
"cliff": "^0.1.10",
|
||||
"inquirer": "^0.11.0",
|
||||
"smartparam": "0.0.7",
|
||||
"yargs": "^3.29.0"
|
||||
"@types/yargs": "^11.0.0",
|
||||
"beautylog": "^6.1.10",
|
||||
"lik": "^2.0.5",
|
||||
"rxjs": "^6.2.1",
|
||||
"smartparam": "1.0.2",
|
||||
"smartq": "^1.1.8",
|
||||
"yargs": "^11.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"gulp": "3.9.0",
|
||||
"gulp-typescript": "2.9.2"
|
||||
"@gitzone/tsrun": "^1.0.7",
|
||||
"@types/node": "^10.5.0",
|
||||
"cz-conventional-changelog": "^2.1.0",
|
||||
"tapbundle": "^2.0.2"
|
||||
},
|
||||
"config": {
|
||||
"commitizen": {
|
||||
"path": "./node_modules/cz-conventional-changelog"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
173
test.js
173
test.js
@ -1,173 +0,0 @@
|
||||
/// <reference path="typings/tsd.d.ts" />
|
||||
var smartcli = require("./index.js");
|
||||
var bl = require("beautylog")("os");
|
||||
bl.log('now starting Test');
|
||||
bl.log('starting with initial CLI commands and options');
|
||||
var commandsString = 'You specified the following commands:';
|
||||
var commands = smartcli.get.commandArray();
|
||||
for (var key in commands) {
|
||||
commandsString = commandsString + ' ' + commands[key];
|
||||
}
|
||||
bl.log(commandsString);
|
||||
/* ------------------------------------------------------------------ *
|
||||
* ------------------- CHECKS TESTS --------------------------------- *
|
||||
* ------------------------------------------------------------------ */
|
||||
/**
|
||||
*
|
||||
*/
|
||||
var checkCommandTest = function () {
|
||||
if (smartcli.check.command('jazz')) {
|
||||
bl.success('One of your commands is jazz. It is supposed to be there. Perfect!');
|
||||
}
|
||||
else {
|
||||
bl.error('None of your commands is jazz. You need to check this');
|
||||
process.exit(1);
|
||||
}
|
||||
if (!smartcli.check.command('punk')) {
|
||||
bl.success('None of your commands is punk. It is not supposed to be there. Perfect!');
|
||||
}
|
||||
else {
|
||||
bl.error('One of your commands seems to be punk. Something is wrong here');
|
||||
process.exit(1);
|
||||
}
|
||||
};
|
||||
checkCommandTest();
|
||||
var checkCommandPresenceTest = function () {
|
||||
if (smartcli.check.commandPresence()) {
|
||||
bl.success('There are commands present, like supposed to. Perfect!');
|
||||
}
|
||||
else {
|
||||
bl.error('There do not seem to be any commands present... This is wrong');
|
||||
process.exit(1);
|
||||
}
|
||||
};
|
||||
checkCommandPresenceTest();
|
||||
var checkCommandArgumentTest = function () {
|
||||
if (smartcli.check.commandArgument("jam", 1)) {
|
||||
bl.success('There is a level 1 argument! Perfect!');
|
||||
}
|
||||
else {
|
||||
bl.error('There seems to be no level 1 argument... This is wrong');
|
||||
process.exit(1);
|
||||
}
|
||||
if (!smartcli.check.commandArgument("session", 2)) {
|
||||
bl.success('There is no level 2 argument with the name of "session"! Perfect!');
|
||||
}
|
||||
else {
|
||||
bl.error('There seems to be a level 2 argument with the name of "session"! This is wrong');
|
||||
process.exit(1);
|
||||
}
|
||||
};
|
||||
checkCommandArgumentTest();
|
||||
var checkCommandArgumentPresenceTest = function () {
|
||||
if (smartcli.check.commandArgumentPresence(1)) {
|
||||
bl.success('There is a level 1 argument! Perfect!');
|
||||
}
|
||||
else {
|
||||
bl.error('There seems to be no level 1 argument... This is wrong');
|
||||
process.exit(1);
|
||||
}
|
||||
if (!smartcli.check.commandArgumentPresence(2)) {
|
||||
bl.success('There is no level 2 argument! Perfect!');
|
||||
}
|
||||
else {
|
||||
bl.error('There seems to be a level 2 argument... This is wrong');
|
||||
process.exit(1);
|
||||
}
|
||||
};
|
||||
checkCommandArgumentPresenceTest();
|
||||
/* ------------------------------------------------------------------ *
|
||||
* ------------------- GETTERS TESTS -------------------------------- *
|
||||
* ------------------------------------------------------------------ */
|
||||
var getCommandTest = function () {
|
||||
var cliCommand = smartcli.get.command();
|
||||
if (cliCommand.name == "jazz") {
|
||||
bl.success('The specified command name is "jazz". Perfect!');
|
||||
}
|
||||
else {
|
||||
bl.error('The specified command name is not "jazz". Something is wrong!');
|
||||
process.exit(1);
|
||||
}
|
||||
};
|
||||
getCommandTest();
|
||||
var getCommandArgumentTest = function () {
|
||||
var cliArgument = smartcli.get.commandArgument(1);
|
||||
var cliArgument2 = smartcli.get.commandArgument(2);
|
||||
if (cliArgument.name == "jam") {
|
||||
bl.success('The first specified command argument name is "jam". Perfect!');
|
||||
}
|
||||
else {
|
||||
bl.error('The first specified command argument name is not "jam". Something is wrong!');
|
||||
process.exit(1);
|
||||
}
|
||||
if (cliArgument2.name == "undefined") {
|
||||
bl.success('The second specified command argument name is "undefined". Perfect!');
|
||||
}
|
||||
else {
|
||||
bl.error('The second specified command argument name is not "undefined". Something is wrong!');
|
||||
process.exit(1);
|
||||
}
|
||||
};
|
||||
getCommandArgumentTest();
|
||||
var getCommandArgsTest = function () {
|
||||
var commandArgs = smartcli.get.commandArgs();
|
||||
if (commandArgs[0].name == "jam") {
|
||||
bl.success("first command argument is 'jam'. Perfect!");
|
||||
}
|
||||
else {
|
||||
bl.error("first command argument is not 'jam'. Something is wromg!");
|
||||
console.log(commandArgs[0].name);
|
||||
process.exit(1);
|
||||
}
|
||||
};
|
||||
getCommandArgsTest();
|
||||
var getOptionTest = function () {
|
||||
var cliOption = smartcli.get.option("awesome");
|
||||
var cliOption2 = smartcli.get.option("terrific");
|
||||
if (cliOption.specified) {
|
||||
bl.success("awesome is specified. Perfect!");
|
||||
}
|
||||
else {
|
||||
bl.error("awesome is not specified. Somehthing is wrong");
|
||||
process.exit(1);
|
||||
}
|
||||
if (!cliOption2.specified) {
|
||||
bl.success("terrific is not specified. Perfect!");
|
||||
}
|
||||
else {
|
||||
bl.error("terrific is specified. Somehthing is wrong");
|
||||
process.exit(1);
|
||||
}
|
||||
};
|
||||
getOptionTest();
|
||||
var getCwdTest = function () {
|
||||
bl.info('The current directory is: ' + smartcli.get.cwd().path);
|
||||
};
|
||||
getCwdTest();
|
||||
/* ------------------------------------------------------------------ *
|
||||
* ------------------- INTERACTION TESTS ---------------------------- *
|
||||
* ------------------------------------------------------------------ */
|
||||
var interactionGetAnswerTest = function () {
|
||||
smartcli.interaction.getAnswer('How do you feel?', function (answer) {
|
||||
console.log('The answer is: ' + answer);
|
||||
interactionGetChoiceTest();
|
||||
});
|
||||
};
|
||||
var interactionGetChoiceTest = function () {
|
||||
smartcli.interaction.getChoice('What music do you like to hear?', ['Jazz', 'Blues', 'Classical'], function (answer) {
|
||||
console.log('The answer is: ' + answer);
|
||||
endTests();
|
||||
});
|
||||
};
|
||||
var endTests = function () {
|
||||
bl.ok("No more tests!");
|
||||
bl.success("Tests completed successfully!");
|
||||
};
|
||||
if (!smartcli.check.option("silent")) {
|
||||
interactionGetAnswerTest();
|
||||
}
|
||||
else {
|
||||
bl.info("--silent option is specified, thus we are not running interaction tests.");
|
||||
endTests();
|
||||
}
|
||||
;
|
43
test/test.ts
Normal file
43
test/test.ts
Normal file
@ -0,0 +1,43 @@
|
||||
import { tap, expect } from 'tapbundle';
|
||||
import { Subject } from 'rxjs';
|
||||
|
||||
import smartcli = require('../ts/index');
|
||||
|
||||
let smartCliTestObject: smartcli.Smartcli;
|
||||
|
||||
tap.test('should create a new Smartcli', async () => {
|
||||
smartCliTestObject = new smartcli.Smartcli();
|
||||
expect(smartCliTestObject).to.be.instanceof(smartcli.Smartcli);
|
||||
});
|
||||
|
||||
tap.test('should add an command', async () => {
|
||||
expect(smartCliTestObject.addCommand('awesome')).to.be.instanceOf(Subject);
|
||||
});
|
||||
|
||||
tap.test('should start parsing a standardTask', async () => {
|
||||
expect(smartCliTestObject.standardTask()).to.be.instanceOf(Subject);
|
||||
});
|
||||
|
||||
let hasExecuted: boolean = false;
|
||||
|
||||
tap.test('should accept a command', async () => {
|
||||
smartCliTestObject.addTrigger('triggerme').subscribe(() => {
|
||||
hasExecuted = true;
|
||||
});
|
||||
});
|
||||
|
||||
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();
|
@ -1,25 +0,0 @@
|
||||
// import gulp
|
||||
var gulp = require("gulp"),
|
||||
gulpTypescript = require("gulp-typescript");
|
||||
|
||||
gulp.task('compileTS', function() {
|
||||
var stream = gulp.src('../index.ts')
|
||||
.pipe(gulpTypescript({
|
||||
out: "index.js"
|
||||
}))
|
||||
.pipe(gulp.dest("../../"));
|
||||
return stream;
|
||||
});
|
||||
|
||||
gulp.task('compileTestTS', function() {
|
||||
var stream = gulp.src('../test.ts')
|
||||
.pipe(gulpTypescript({
|
||||
out: "test.js"
|
||||
}))
|
||||
.pipe(gulp.dest("../../"));
|
||||
return stream;
|
||||
});
|
||||
|
||||
gulp.task('default',['compileTS','compileTestTS'], function() {
|
||||
console.log('Typescript compiled');
|
||||
});
|
@ -1,2 +0,0 @@
|
||||
# How to compile.
|
||||
Make sure gulp and gulp-taypescript from npm are available. Then run the gulpfile in this directory.
|
26
ts/index.ts
26
ts/index.ts
@ -1,25 +1 @@
|
||||
/// <reference path="typings/tsd.d.ts" />
|
||||
/// <reference path="./smartcli.plugins.ts" />
|
||||
/// <reference path="./smartcli.interfaces.ts" />
|
||||
/// <reference path="./smartcli.checks.ts" />
|
||||
/// <reference path="./smartcli.getters.ts" />
|
||||
/// <reference path="./smartcli.interaction.ts" />
|
||||
|
||||
var plugins = smartcliPlugins.init(); //get all the required npm modules under plugins
|
||||
|
||||
//define the smartcli object
|
||||
var smartcli:any = {};
|
||||
|
||||
//add plugins from above for direct use
|
||||
smartcli.inquirer = plugins.inquirer; //inquirer is for asking questions
|
||||
smartcli.cliff = plugins.cliff; // formats cli output
|
||||
smartcli.argv = plugins.argv; //argv gets initial cli commands and options.
|
||||
|
||||
//init checks. Checks return boolean. That means they can be used as question with an answer of yes or no.
|
||||
|
||||
SmartcliChecks.init(); // is defined in smartcli.checks.ts
|
||||
SmartcliGetters.init(); // is defined in smartcli.getters.ts
|
||||
SmartcliInteraction.init(); // is defined in smartcli.interaction.ts
|
||||
|
||||
|
||||
module.exports = smartcli; // expose smartcli to outside world
|
||||
export { Smartcli } from './smartcli.classes.smartcli';
|
||||
|
@ -1,70 +0,0 @@
|
||||
/// <reference path="./index.ts" />
|
||||
module SmartcliChecks {
|
||||
export function init() {
|
||||
/**
|
||||
* all functions in smartcli.check return a boolean
|
||||
* @type {{}}
|
||||
*/
|
||||
smartcli.check = {};
|
||||
|
||||
/**
|
||||
* checks for a special command string and returns true if found.
|
||||
* @param commandString
|
||||
* @returns {boolean}
|
||||
*/
|
||||
smartcli.check.command = function(commandString:string):boolean {
|
||||
if (plugins.argv._.indexOf(commandString) == 0) {
|
||||
return true
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
/**
|
||||
* checks if a command is present, returns true if yes, false if no.
|
||||
* @returns {boolean}
|
||||
*/
|
||||
smartcli.check.commandPresence = function():boolean {
|
||||
if(plugins.argv._.length > 0){
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
/**
|
||||
* checks for an special command argument at a certain position, returns true if matches, returns false if not
|
||||
* @param level
|
||||
* @returns {boolean}
|
||||
*/
|
||||
smartcli.check.commandArgument = function(commandArgumentString:string,level:number = 1):boolean {
|
||||
if(smartcli.check.commandArgumentPresence(level) && (plugins.argv._[level] == commandArgumentString )) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
smartcli.check.commandArgumentPresence = function(level:number = 1) {
|
||||
if(plugins.argv._.length >= (level + 1)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
/**
|
||||
* checks for a specific option string, returns true if yes, returns false if no
|
||||
* @returns {boolean}
|
||||
*/
|
||||
smartcli.check.option = function(optionString):boolean {
|
||||
if(plugins.smartparam.exists(plugins.argv, optionString)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
smartcli.check.optionPresence = function():boolean {
|
||||
if (plugins.argv.indexOf() != -1) {
|
||||
return true
|
||||
}
|
||||
return false;
|
||||
};
|
||||
}
|
||||
}
|
165
ts/smartcli.classes.smartcli.ts
Normal file
165
ts/smartcli.classes.smartcli.ts
Normal file
@ -0,0 +1,165 @@
|
||||
import * as smartq from 'smartq';
|
||||
import { Subject } from 'rxjs';
|
||||
|
||||
import * as plugins from './smartcli.plugins';
|
||||
|
||||
// import classes
|
||||
import { Objectmap } from 'lik';
|
||||
|
||||
// interfaces
|
||||
export interface ICommandPromiseObject {
|
||||
commandName: string;
|
||||
promise: Promise<void>;
|
||||
}
|
||||
|
||||
export interface ITriggerObservableObject {
|
||||
triggerName: string;
|
||||
subject: Subject<any>;
|
||||
}
|
||||
|
||||
/**
|
||||
* class to create a new instance of Smartcli. Handles parsing of command line arguments.
|
||||
*/
|
||||
export class Smartcli {
|
||||
argv: any;
|
||||
questionsDone;
|
||||
parseStarted: smartq.Deferred<any>;
|
||||
commands;
|
||||
questions;
|
||||
version: string;
|
||||
private onlyOnProcessEnvCliCall = false;
|
||||
|
||||
/**
|
||||
* map of all Trigger/Observable objects to keep track
|
||||
*/
|
||||
allTriggerObservablesMap = new Objectmap<ITriggerObservableObject>();
|
||||
|
||||
/**
|
||||
* The constructor of Smartcli
|
||||
*/
|
||||
constructor() {
|
||||
this.argv = plugins.yargs;
|
||||
this.questionsDone = smartq.defer();
|
||||
this.parseStarted = smartq.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.
|
||||
*/
|
||||
addCommandAlias(keyArg, aliasArg): void {
|
||||
this.argv = this.argv.alias(keyArg, aliasArg);
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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> {
|
||||
let 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) {
|
||||
let 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* execute trigger by name
|
||||
* @param commandNameArg - the name of the command to trigger
|
||||
*/
|
||||
trigger(triggerName: string) {
|
||||
let triggerSubject = this.getTriggerSubject(triggerName);
|
||||
triggerSubject.next(this.argv);
|
||||
return triggerSubject;
|
||||
}
|
||||
|
||||
getTriggerSubject(triggerName: string) {
|
||||
const triggerObservableObject = this.allTriggerObservablesMap.find(
|
||||
triggerObservableObjectArg => {
|
||||
return triggerObservableObjectArg.triggerName === triggerName;
|
||||
}
|
||||
);
|
||||
if (triggerObservableObject) {
|
||||
return triggerObservableObject.subject;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* allows to specify help text to be printed above the rest of the help text
|
||||
*/
|
||||
addHelp(optionsArg: { helpText: string }) {
|
||||
this.addCommand('help').subscribe(argvArg => {
|
||||
plugins.beautylog.log(optionsArg.helpText);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* specify version to be printed for -v --version
|
||||
*/
|
||||
addVersion(versionArg: string) {
|
||||
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
|
||||
*/
|
||||
standardTask(): Subject<any> {
|
||||
let standardSubject = this.addTrigger('standardTask');
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* start the process of evaluating commands
|
||||
*/
|
||||
startParse(): void {
|
||||
this.argv = this.argv.argv;
|
||||
this.parseStarted.resolve();
|
||||
return;
|
||||
}
|
||||
}
|
@ -1,110 +0,0 @@
|
||||
/// <reference path="./index.ts" />
|
||||
module SmartcliGetters {
|
||||
export function init() {
|
||||
smartcli.get = {};
|
||||
/**
|
||||
*
|
||||
* @param commandString
|
||||
* @returns {{specified: boolean, name: any, arguments: CliCommandArgument}}
|
||||
*/
|
||||
smartcli.get.command = function():CliCommand {
|
||||
var cliCommand = {
|
||||
specified: smartcli.check.commandPresence(),
|
||||
name: plugins.argv._[0],
|
||||
arguments: smartcli.get.commandArgs()
|
||||
}
|
||||
return cliCommand;
|
||||
};
|
||||
|
||||
/**
|
||||
* gets the second or higher value of plugins.argv._ if specified and returns it as commandArgument
|
||||
* @param argumentLevel
|
||||
* @returns {{specified: (boolean|boolean), name}}
|
||||
*/
|
||||
smartcli.get.commandArgument = function(argumentLevel):CliCommandArgument {
|
||||
var commandArgument:CliCommandArgument = {
|
||||
specified: false,
|
||||
name: "undefined",
|
||||
level:argumentLevel
|
||||
};
|
||||
if(argumentLevel < 1) {
|
||||
plugins.beautylog.error("smartcli.get.argument cannot be invoked with an argumentLevel smaller than 1");
|
||||
return commandArgument;
|
||||
}
|
||||
if(smartcli.check.commandArgumentPresence(argumentLevel)) {
|
||||
commandArgument.specified = true;
|
||||
commandArgument.name = plugins.argv._[argumentLevel];
|
||||
}
|
||||
return commandArgument;
|
||||
};
|
||||
|
||||
/**
|
||||
* returns array with commandArgs
|
||||
* @returns {CliCommandArgument[]}
|
||||
*/
|
||||
smartcli.get.commandArgs = function():CliCommandArgument[] {
|
||||
var commandArgs:CliCommandArgument[] = [];
|
||||
var argsArray = smartcli.get.commandArray().slice(0);
|
||||
argsArray.shift();
|
||||
for (var item in argsArray){
|
||||
var commandArgument:CliCommandArgument = {
|
||||
specified:true,
|
||||
name:argsArray[item],
|
||||
level: item + 1
|
||||
}
|
||||
commandArgs.push(commandArgument);
|
||||
}
|
||||
return commandArgs;
|
||||
};
|
||||
|
||||
/**
|
||||
* returns complete command array
|
||||
* @returns {any}
|
||||
*/
|
||||
smartcli.get.commandArray = function ():string[] {
|
||||
var commandArray = plugins.argv._;
|
||||
return commandArray;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* returns a cli option
|
||||
* @param optionName
|
||||
* @returns {CliOption}
|
||||
*/
|
||||
smartcli.get.option = function(optionName:string):CliOption {
|
||||
var cliOption:CliOption = {
|
||||
name:optionName,
|
||||
specified: false,
|
||||
value: false
|
||||
};
|
||||
if (plugins.smartparam.exists(plugins.argv,optionName)) {
|
||||
cliOption.name = optionName;
|
||||
cliOption.specified = true;
|
||||
cliOption.value = plugins.argv[optionName] //we already know from the "if" above that the value is available.
|
||||
}
|
||||
return cliOption;
|
||||
};
|
||||
|
||||
|
||||
smartcli.get.options = function() {
|
||||
var options = {};
|
||||
for (var key in plugins.argv) {
|
||||
if (key != "_") {
|
||||
options['key'] = plugins.argv['key'];
|
||||
}
|
||||
}
|
||||
return options;
|
||||
};
|
||||
|
||||
/**
|
||||
* returns Directory of cwd
|
||||
* @returns {{path: string}}
|
||||
*/
|
||||
smartcli.get.cwd = function():Directory {
|
||||
return {
|
||||
path: process.cwd()
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
@ -1,61 +0,0 @@
|
||||
/// <reference path="./index.ts" />
|
||||
module SmartcliInteraction {
|
||||
export function init(){
|
||||
|
||||
smartcli.interaction = {};
|
||||
/**
|
||||
* executes callback with answer to question as argument
|
||||
* @param questionString the question you want to ask the user
|
||||
* @param cb the function to execute with answer as param
|
||||
* @returns {null}
|
||||
*/
|
||||
smartcli.interaction.getAnswer = function(questionString:string, cb) {
|
||||
if (typeof questionString != 'string') {
|
||||
plugins.beautylog.error('no question specified');
|
||||
return null;
|
||||
}
|
||||
//make inquirer compatible question object
|
||||
var question = {
|
||||
type: "input",
|
||||
name: "userFeedback",
|
||||
message: questionString,
|
||||
validate: function( value ) {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
plugins.inquirer.prompt([question],function(answers){
|
||||
var answer = answers.userFeedback;
|
||||
cb(answer);
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
*
|
||||
* @param questionString
|
||||
* @param choiceOptions
|
||||
* @param cb
|
||||
* @returns {null}
|
||||
*/
|
||||
smartcli.interaction.getChoice = function(questionString:string, choiceOptions:string[], cb) {
|
||||
if(!Array.isArray(choiceOptions)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
//make inquirer compatible question object
|
||||
var question = {
|
||||
type: "list",
|
||||
name: "userFeedback",
|
||||
message: questionString,
|
||||
choices: choiceOptions,
|
||||
filter: function( val ) { return val.toLowerCase(); }
|
||||
};
|
||||
|
||||
plugins.inquirer.prompt(question,function(answers){
|
||||
var answer = answers.userFeedback;
|
||||
cb(answer);
|
||||
});
|
||||
|
||||
};
|
||||
}
|
||||
}
|
@ -1,23 +0,0 @@
|
||||
/// <reference path="index.ts" />
|
||||
interface CliOption {
|
||||
name: string;
|
||||
specified:boolean;
|
||||
value: any;
|
||||
}
|
||||
|
||||
interface Directory {
|
||||
path: string;
|
||||
}
|
||||
|
||||
interface CliCommand {
|
||||
specified: boolean;
|
||||
name: string;
|
||||
arguments:string[];
|
||||
}
|
||||
|
||||
interface CliCommandArgument {
|
||||
specified:boolean;
|
||||
name:string;
|
||||
level:number;
|
||||
}
|
||||
|
@ -1,14 +1,7 @@
|
||||
/// <reference path="./index.ts" />
|
||||
import * as yargs from 'yargs';
|
||||
import * as beautylog from 'beautylog';
|
||||
import * as lik from 'lik';
|
||||
import * as path from 'path';
|
||||
import * as smartparam from 'smartparam';
|
||||
|
||||
module smartcliPlugins {
|
||||
var plugins:any = {};
|
||||
export function init() {
|
||||
plugins.path = require("path");
|
||||
plugins.beautylog = require("beautylog")("os");
|
||||
plugins.cliff = require("cliff");
|
||||
plugins.inquirer = require("inquirer");
|
||||
plugins.smartparam = require("smartparam");
|
||||
plugins.argv = require('yargs').argv;
|
||||
return plugins;
|
||||
}
|
||||
}
|
||||
export { yargs, beautylog, lik, path, smartparam };
|
||||
|
188
ts/test.ts
188
ts/test.ts
@ -1,188 +0,0 @@
|
||||
/// <reference path="typings/tsd.d.ts" />
|
||||
var smartcli = require("./index.js");
|
||||
var bl = require("beautylog")("os");
|
||||
|
||||
bl.log('now starting Test');
|
||||
bl.log('starting with initial CLI commands and options');
|
||||
|
||||
var commandsString:string = 'You specified the following commands:';
|
||||
var commands = smartcli.get.commandArray();
|
||||
for (var key in commands) {
|
||||
commandsString = commandsString + ' ' + commands[key];
|
||||
}
|
||||
bl.log(commandsString);
|
||||
|
||||
|
||||
/* ------------------------------------------------------------------ *
|
||||
* ------------------- CHECKS TESTS --------------------------------- *
|
||||
* ------------------------------------------------------------------ */
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
var checkCommandTest = function() {
|
||||
if (smartcli.check.command('jazz')) {
|
||||
bl.success('One of your commands is jazz. It is supposed to be there. Perfect!');
|
||||
} else {
|
||||
bl.error('None of your commands is jazz. You need to check this');
|
||||
process.exit(1);
|
||||
}
|
||||
if (!smartcli.check.command('punk')) {
|
||||
bl.success('None of your commands is punk. It is not supposed to be there. Perfect!');
|
||||
} else {
|
||||
bl.error('One of your commands seems to be punk. Something is wrong here');
|
||||
process.exit(1);
|
||||
}
|
||||
};
|
||||
checkCommandTest();
|
||||
|
||||
|
||||
var checkCommandPresenceTest = function() {
|
||||
if (smartcli.check.commandPresence()) {
|
||||
bl.success('There are commands present, like supposed to. Perfect!');
|
||||
} else {
|
||||
bl.error('There do not seem to be any commands present... This is wrong');
|
||||
process.exit(1);
|
||||
}
|
||||
};
|
||||
checkCommandPresenceTest();
|
||||
|
||||
|
||||
var checkCommandArgumentTest = function() {
|
||||
if (smartcli.check.commandArgument("jam",1)) {
|
||||
bl.success('There is a level 1 argument! Perfect!');
|
||||
} else {
|
||||
bl.error('There seems to be no level 1 argument... This is wrong');
|
||||
process.exit(1);
|
||||
}
|
||||
if (!smartcli.check.commandArgument("session",2)) {
|
||||
bl.success('There is no level 2 argument with the name of "session"! Perfect!');
|
||||
} else {
|
||||
bl.error('There seems to be a level 2 argument with the name of "session"! This is wrong');
|
||||
process.exit(1);
|
||||
}
|
||||
};
|
||||
checkCommandArgumentTest();
|
||||
|
||||
var checkCommandArgumentPresenceTest = function() {
|
||||
if (smartcli.check.commandArgumentPresence(1)) {
|
||||
bl.success('There is a level 1 argument! Perfect!');
|
||||
} else {
|
||||
bl.error('There seems to be no level 1 argument... This is wrong');
|
||||
process.exit(1);
|
||||
}
|
||||
if (!smartcli.check.commandArgumentPresence(2)) {
|
||||
bl.success('There is no level 2 argument! Perfect!');
|
||||
} else {
|
||||
bl.error('There seems to be a level 2 argument... This is wrong');
|
||||
process.exit(1);
|
||||
}
|
||||
};
|
||||
checkCommandArgumentPresenceTest();
|
||||
|
||||
/* ------------------------------------------------------------------ *
|
||||
* ------------------- GETTERS TESTS -------------------------------- *
|
||||
* ------------------------------------------------------------------ */
|
||||
var getCommandTest = function(){
|
||||
var cliCommand = smartcli.get.command();
|
||||
if(cliCommand.name == "jazz") {
|
||||
bl.success('The specified command name is "jazz". Perfect!');
|
||||
} else {
|
||||
bl.error('The specified command name is not "jazz". Something is wrong!');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
};
|
||||
getCommandTest();
|
||||
|
||||
var getCommandArgumentTest = function() {
|
||||
var cliArgument = smartcli.get.commandArgument(1);
|
||||
var cliArgument2 = smartcli.get.commandArgument(2);
|
||||
if(cliArgument.name == "jam") {
|
||||
bl.success('The first specified command argument name is "jam". Perfect!');
|
||||
} else {
|
||||
bl.error('The first specified command argument name is not "jam". Something is wrong!');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
if(cliArgument2.name == "undefined") {
|
||||
bl.success('The second specified command argument name is "undefined". Perfect!');
|
||||
} else {
|
||||
bl.error('The second specified command argument name is not "undefined". Something is wrong!');
|
||||
process.exit(1);
|
||||
}
|
||||
};
|
||||
getCommandArgumentTest();
|
||||
|
||||
var getCommandArgsTest = function() {
|
||||
var commandArgs = smartcli.get.commandArgs();
|
||||
if(commandArgs[0].name == "jam") {
|
||||
bl.success("first command argument is 'jam'. Perfect!");
|
||||
} else {
|
||||
bl.error("first command argument is not 'jam'. Something is wromg!");
|
||||
console.log(commandArgs[0].name);
|
||||
process.exit(1);
|
||||
}
|
||||
};
|
||||
getCommandArgsTest();
|
||||
|
||||
var getOptionTest = function() {
|
||||
var cliOption = smartcli.get.option("awesome");
|
||||
var cliOption2 = smartcli.get.option("terrific");
|
||||
if(cliOption.specified){
|
||||
bl.success("awesome is specified. Perfect!")
|
||||
} else {
|
||||
bl.error("awesome is not specified. Somehthing is wrong");
|
||||
process.exit(1);
|
||||
}
|
||||
if(!cliOption2.specified){
|
||||
bl.success("terrific is not specified. Perfect!")
|
||||
} else {
|
||||
bl.error("terrific is specified. Somehthing is wrong");
|
||||
process.exit(1);
|
||||
}
|
||||
};
|
||||
getOptionTest();
|
||||
|
||||
var getCwdTest = function(){
|
||||
bl.info('The current directory is: ' + smartcli.get.cwd().path);
|
||||
};
|
||||
getCwdTest();
|
||||
|
||||
|
||||
/* ------------------------------------------------------------------ *
|
||||
* ------------------- INTERACTION TESTS ---------------------------- *
|
||||
* ------------------------------------------------------------------ */
|
||||
|
||||
|
||||
var interactionGetAnswerTest = function() {
|
||||
smartcli.interaction.getAnswer('How do you feel?',function(answer){
|
||||
console.log('The answer is: ' + answer);
|
||||
interactionGetChoiceTest();
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
||||
var interactionGetChoiceTest = function() {
|
||||
smartcli.interaction.getChoice('What music do you like to hear?',['Jazz','Blues','Classical'],function(answer){
|
||||
console.log('The answer is: ' + answer);
|
||||
endTests();
|
||||
});
|
||||
};
|
||||
|
||||
var endTests = function() {
|
||||
bl.ok("No more tests!");
|
||||
bl.success("Tests completed successfully!");
|
||||
};
|
||||
|
||||
if(!smartcli.check.option("silent")){
|
||||
interactionGetAnswerTest();
|
||||
} else {
|
||||
bl.info("--silent option is specified, thus we are not running interaction tests.");
|
||||
endTests();
|
||||
};
|
||||
|
||||
|
||||
|
12
ts/tsd.json
12
ts/tsd.json
@ -1,12 +0,0 @@
|
||||
{
|
||||
"version": "v4",
|
||||
"repo": "borisyankov/DefinitelyTyped",
|
||||
"ref": "master",
|
||||
"path": "typings",
|
||||
"bundle": "typings/tsd.d.ts",
|
||||
"installed": {
|
||||
"node/node.d.ts": {
|
||||
"commit": "efa0c1196d7280640e624ac1e7fa604502e7bd63"
|
||||
}
|
||||
}
|
||||
}
|
2079
ts/typings/node/node.d.ts
vendored
2079
ts/typings/node/node.d.ts
vendored
File diff suppressed because it is too large
Load Diff
1
ts/typings/tsd.d.ts
vendored
1
ts/typings/tsd.d.ts
vendored
@ -1 +0,0 @@
|
||||
/// <reference path="node/node.d.ts" />
|
3
tslint.json
Normal file
3
tslint.json
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"extends": "tslint-config-standard"
|
||||
}
|
Reference in New Issue
Block a user