Compare commits
14 Commits
Author | SHA1 | Date | |
---|---|---|---|
69592933aa | |||
10074b5888 | |||
fe9564e37c | |||
fa5f2d0267 | |||
a4cd5175b3 | |||
97c3ccb329 | |||
cfdd54eff1 | |||
741365d325 | |||
b36e95af1a | |||
ac9ef4915e | |||
6ca2e427ff | |||
39f9bb11fe | |||
8925f29962 | |||
2e862d6840 |
@ -25,34 +25,7 @@ smart cli interaction
|
|||||||
|
|
||||||
Use TypeScript for best in class instellisense.
|
Use TypeScript for best in class instellisense.
|
||||||
|
|
||||||
```javascript
|
|
||||||
import { SmartInteract } from 'smartinteract'
|
|
||||||
|
|
||||||
let myInteract = new SmartInteract([{ // note: its an array. You can specify multiple questions
|
|
||||||
name: 'question1',
|
|
||||||
type: 'input',
|
|
||||||
message: 'Who are you?',
|
|
||||||
default: 'Somebody',
|
|
||||||
validate: (inputString) => { return true } // implement your own validation
|
|
||||||
}])
|
|
||||||
myInteract.addQuestions([ ... ]) // add more questions
|
|
||||||
myInteract.runQueue()
|
|
||||||
.then(answerBucket => { // the bucket has all the answers of the completed queue
|
|
||||||
let answerQuestion1 = answerBucket.getAnswerFor('question1')
|
|
||||||
// do something with the answers
|
|
||||||
})
|
|
||||||
|
|
||||||
// alternatively use .askQuestion() for more direct control
|
|
||||||
myInteract.askQuestion{ // note: its an array. You can specify multiple questions
|
|
||||||
name: 'question2',
|
|
||||||
type: 'confirm',
|
|
||||||
message: 'Do you speak English?',
|
|
||||||
default: true,
|
|
||||||
validate: (inputString) => { return true } // implement your own validation
|
|
||||||
}().then(answerObject => {
|
|
||||||
// answerObject looks like { name: 'question2', value: true }
|
|
||||||
})
|
|
||||||
```
|
|
||||||
|
|
||||||
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.
|
||||||
|
|
||||||
|
@ -1,8 +1,6 @@
|
|||||||
{
|
{
|
||||||
"npmci": {
|
"npmci": {
|
||||||
"globalNpmTools": [
|
"npmGlobalTools": ["npmts"],
|
||||||
"npmts"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"npmAccessLevel": "public"
|
"npmAccessLevel": "public"
|
||||||
|
}
|
||||||
}
|
}
|
1366
package-lock.json
generated
1366
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
17
package.json
17
package.json
@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/smartinteract",
|
"name": "@pushrocks/smartinteract",
|
||||||
"private": false,
|
"private": false,
|
||||||
"version": "2.0.0",
|
"version": "2.0.7",
|
||||||
"description": "smart cli interaction",
|
"description": "smart cli interaction",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"typings": "dist/index.d.ts",
|
"typings": "dist/index.d.ts",
|
||||||
@ -25,13 +25,16 @@
|
|||||||
},
|
},
|
||||||
"homepage": "https://gitlab.com/pushrocks/smartinteract#README",
|
"homepage": "https://gitlab.com/pushrocks/smartinteract#README",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@pushrocks/lik": "^3.0.1",
|
"@pushrocks/lik": "^3.0.13",
|
||||||
"@pushrocks/smartpromise": "^2.0.5",
|
"@pushrocks/smartparam": "^1.1.6",
|
||||||
"@types/inquirer": "0.0.42",
|
"@pushrocks/smartpromise": "^3.0.6",
|
||||||
"inquirer": "^6.0.0"
|
"@types/inquirer": "^6.5.0",
|
||||||
|
"inquirer": "^7.0.4"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@gitzone/tsrun": "^1.1.9",
|
"@gitzone/tsrun": "^1.2.8",
|
||||||
"@pushrocks/tapbundle": "^3.0.1"
|
"@pushrocks/tapbundle": "^3.2.0",
|
||||||
|
"tslint": "^6.0.0",
|
||||||
|
"tslint-config-prettier": "^1.18.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -25,7 +25,36 @@ smart cli interaction
|
|||||||
|
|
||||||
Use TypeScript for best in class instellisense.
|
Use TypeScript for best in class instellisense.
|
||||||
|
|
||||||
For further information read the linked docs at the top of this README.
|
```javascript
|
||||||
|
import { SmartInteract } from 'smartinteract'
|
||||||
|
|
||||||
|
let myInteract = new SmartInteract([{ // note: its an array. You can specify multiple questions
|
||||||
|
name: 'question1',
|
||||||
|
type: 'input',
|
||||||
|
message: 'Who are you?',
|
||||||
|
default: 'Somebody',
|
||||||
|
validate: (inputString) => { return true } // implement your own validation
|
||||||
|
}])
|
||||||
|
myInteract.addQuestions([ ... ]) // add more questions
|
||||||
|
myInteract.runQueue()
|
||||||
|
.then(answerBucket => { // the bucket has all the answers of the completed queue
|
||||||
|
let answerQuestion1 = answerBucket.getAnswerFor('question1')
|
||||||
|
// do something with the answers
|
||||||
|
})
|
||||||
|
|
||||||
|
// alternatively use .askQuestion() for more direct control
|
||||||
|
myInteract.askQuestion{ // note: its an array. You can specify multiple questions
|
||||||
|
name: 'question2',
|
||||||
|
type: 'confirm',
|
||||||
|
message: 'Do you speak English?',
|
||||||
|
default: true,
|
||||||
|
validate: (inputString) => { return true } // implement your own validation
|
||||||
|
}().then(answerObject => {
|
||||||
|
// answerObject looks like { name: 'question2', value: true }
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
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)
|
||||||
> | By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy.html)
|
> | By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy.html)
|
@ -30,14 +30,14 @@ tap.test('should add question to SmartInteract instance', async () => {
|
|||||||
{
|
{
|
||||||
name: 'some.dotted.name',
|
name: 'some.dotted.name',
|
||||||
type: 'input',
|
type: 'input',
|
||||||
message: 'what is your second favourite color? Answer is red',
|
message: 'what is your second favourite color? Answer is aValidAnswer',
|
||||||
default: 'aValidAnswer'
|
default: 'aValidAnswer'
|
||||||
}
|
}
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
|
|
||||||
tap.test('should use default value when not in CI', async () => {
|
tap.test('should use default value when not in CI', async () => {
|
||||||
let answerBucket = await testInteract.runQueue();
|
const answerBucket = await testInteract.runQueue();
|
||||||
expect(answerBucket.getAnswerFor('testQuestion1')).to.equal('blue');
|
expect(answerBucket.getAnswerFor('testQuestion1')).to.equal('blue');
|
||||||
expect(answerBucket.getAnswerFor('testQuestion2')).to.equal('red');
|
expect(answerBucket.getAnswerFor('testQuestion2')).to.equal('red');
|
||||||
expect(answerBucket.getAnswerFor('some.dotted.name')).to.equal('aValidAnswer');
|
expect(answerBucket.getAnswerFor('some.dotted.name')).to.equal('aValidAnswer');
|
||||||
|
@ -1 +1,2 @@
|
|||||||
export * from './smartinteract.classes.smartinteract';
|
export * from './smartinteract.classes.smartinteract';
|
||||||
|
export * from './smartinteract.classes.answerbucket';
|
33
ts/smartinteract.classes.answerbucket.ts
Normal file
33
ts/smartinteract.classes.answerbucket.ts
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
import * as plugins from './smartinteract.plugins';
|
||||||
|
import { IAnswerObject } from './smartinteract.classes.smartinteract';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* class AnswerBucket holds answers
|
||||||
|
*/
|
||||||
|
export class AnswerBucket {
|
||||||
|
private answerMap = new plugins.lik.Objectmap<IAnswerObject>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* add an answer to the bucket
|
||||||
|
*/
|
||||||
|
public addAnswer(answerArg: IAnswerObject) {
|
||||||
|
this.answerMap.add(answerArg);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* gets an answer for a specific name
|
||||||
|
*/
|
||||||
|
public getAnswerFor(nameArg: string) {
|
||||||
|
const answer = this.answerMap.find(answerArg => {
|
||||||
|
return answerArg.name === nameArg;
|
||||||
|
});
|
||||||
|
return answer ? answer.value : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* gets all answers as array
|
||||||
|
*/
|
||||||
|
public getAllAnswers() {
|
||||||
|
return this.answerMap.getArray();
|
||||||
|
}
|
||||||
|
}
|
@ -1,6 +1,6 @@
|
|||||||
import * as plugins from './smartinteract.plugins';
|
import * as plugins from './smartinteract.plugins';
|
||||||
import * as smartq from 'smartq';
|
import * as smartpromise from '@pushrocks/smartpromise';
|
||||||
import { Objectmap } from '@pushrocks/lik';
|
import { AnswerBucket } from './smartinteract.classes.answerbucket';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* the availeable question types
|
* the availeable question types
|
||||||
@ -15,6 +15,9 @@ export type questionType =
|
|||||||
| 'password'
|
| 'password'
|
||||||
| 'editor';
|
| 'editor';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* a choice
|
||||||
|
*/
|
||||||
export interface IChoiceObject {
|
export interface IChoiceObject {
|
||||||
name: string;
|
name: string;
|
||||||
value: any;
|
value: any;
|
||||||
@ -45,7 +48,7 @@ export class SmartInteract {
|
|||||||
/**
|
/**
|
||||||
* holds the qestion queue, that is emptied once you call
|
* holds the qestion queue, that is emptied once you call
|
||||||
*/
|
*/
|
||||||
private questionMap = new Objectmap<IQuestionObject>();
|
private questionMap = new plugins.lik.Objectmap<IQuestionObject>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* constructor of class SmartInteract
|
* constructor of class SmartInteract
|
||||||
@ -60,7 +63,7 @@ export class SmartInteract {
|
|||||||
* skips the queue
|
* skips the queue
|
||||||
*/
|
*/
|
||||||
askQuestion(optionsArg: IQuestionObject): Promise<IAnswerObject> {
|
askQuestion(optionsArg: IQuestionObject): Promise<IAnswerObject> {
|
||||||
let done = smartq.defer<IAnswerObject>();
|
let done = smartpromise.defer<IAnswerObject>();
|
||||||
if (this.isValidEnv()) {
|
if (this.isValidEnv()) {
|
||||||
plugins.inquirer
|
plugins.inquirer
|
||||||
.prompt([
|
.prompt([
|
||||||
@ -73,13 +76,9 @@ export class SmartInteract {
|
|||||||
validate: optionsArg.validate
|
validate: optionsArg.validate
|
||||||
}
|
}
|
||||||
])
|
])
|
||||||
.then((answers: IAnswerObject[]) => {
|
.then((answers) => {
|
||||||
// adjust to the fact that now dots define paths for inquirer
|
// adjust to the fact that now dots define paths for inquirer
|
||||||
let answerValue: any = answers;
|
let answerValue = plugins.smartparam.smartGet(answers, optionsArg.name);
|
||||||
let nameArray = optionsArg.name.split('.');
|
|
||||||
for (let name of nameArray) {
|
|
||||||
answerValue = answerValue[name];
|
|
||||||
}
|
|
||||||
done.resolve({
|
done.resolve({
|
||||||
name: optionsArg.name,
|
name: optionsArg.name,
|
||||||
value: answerValue
|
value: answerValue
|
||||||
@ -110,7 +109,7 @@ export class SmartInteract {
|
|||||||
* run the question queue
|
* run the question queue
|
||||||
*/
|
*/
|
||||||
runQueue() {
|
runQueue() {
|
||||||
let done = smartq.defer<AnswerBucket>();
|
let done = smartpromise.defer<AnswerBucket>();
|
||||||
let answerBucket = new AnswerBucket();
|
let answerBucket = new AnswerBucket();
|
||||||
let handleQuestion = async () => {
|
let handleQuestion = async () => {
|
||||||
if (!this.questionMap.isEmpty()) {
|
if (!this.questionMap.isEmpty()) {
|
||||||
@ -137,34 +136,3 @@ export class SmartInteract {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* class AnswerBucket holds answers
|
|
||||||
*/
|
|
||||||
export class AnswerBucket {
|
|
||||||
answerMap = new Objectmap<IAnswerObject>();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* add an answer to the bucket
|
|
||||||
*/
|
|
||||||
addAnswer(answerArg: IAnswerObject) {
|
|
||||||
this.answerMap.add(answerArg);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* gets an answer for a specific name
|
|
||||||
*/
|
|
||||||
getAnswerFor(nameArg: string) {
|
|
||||||
let answer = this.answerMap.find(answerArg => {
|
|
||||||
return answerArg.name === nameArg;
|
|
||||||
});
|
|
||||||
return answer.value;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* gets all answers as array
|
|
||||||
*/
|
|
||||||
getAllAnswers() {
|
|
||||||
return this.answerMap.getArray();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -1,3 +1,14 @@
|
|||||||
|
// pushrocks scope
|
||||||
|
import * as lik from '@pushrocks/lik';
|
||||||
|
import * as smartparam from '@pushrocks/smartparam';
|
||||||
|
|
||||||
|
export {
|
||||||
|
lik,
|
||||||
|
smartparam
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// third party scope
|
||||||
import * as inquirer from 'inquirer';
|
import * as inquirer from 'inquirer';
|
||||||
|
|
||||||
export { inquirer };
|
export { inquirer };
|
||||||
|
@ -1,3 +1,7 @@
|
|||||||
{
|
{
|
||||||
"extends": "tslint-config-standard"
|
"extends": ["tslint:latest", "tslint-config-prettier"],
|
||||||
}
|
"rules": {
|
||||||
|
"semicolon": [true, "always"]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Reference in New Issue
Block a user