yargs-interactive
Advanced tools
Comparing version 2.1.0 to 2.2.0
{ | ||
"name": "yargs-interactive", | ||
"version": "2.1.0", | ||
"version": "2.2.0", | ||
"description": "Build interactive command line tools without worring to parse the arguments (or ask them).", | ||
@@ -10,6 +10,5 @@ "main": "src/index.js", | ||
"scripts": { | ||
"lint": "eslint src test", | ||
"test": "nyc --reporter=text --reporter=html mocha", | ||
"coveralls": "nyc report --reporter=text-lcov | coveralls", | ||
"commitmsg": "validate-commit-msg", | ||
"lint": "eslint src test --fix", | ||
"test": "jest --collectCoverage", | ||
"coveralls": "jest --collectCoverage && cat ./coverage/lcov.info | coveralls", | ||
"semantic-release": "semantic-release" | ||
@@ -37,17 +36,50 @@ }, | ||
"dependencies": { | ||
"inquirer": "^4.0.0", | ||
"inquirer": "^7.0.0", | ||
"yargs": "^10.0.3" | ||
}, | ||
"devDependencies": { | ||
"coveralls": "^3.0.0", | ||
"eslint": "^4.11.0", | ||
"eslint-config-google": "^0.9.1", | ||
"husky": "^0.14.3", | ||
"mocha": "^4.0.1", | ||
"nyc": "^11.3.0", | ||
"proxyquire": "^1.8.0", | ||
"semantic-release": "^15.13.3", | ||
"sinon": "^4.1.2", | ||
"validate-commit-msg": "^2.14.0" | ||
"@commitlint/cli": "^8.1.0", | ||
"@commitlint/config-conventional": "^8.1.0", | ||
"@types/inquirer": "^6.5.0", | ||
"@types/jest": "^24.0.18", | ||
"@types/node": "12.7.2", | ||
"@types/yargs": "^13.0.2", | ||
"babel-eslint": "^10.0.2", | ||
"coveralls": "^3.0.6", | ||
"cz-conventional-changelog": "^3.0.2", | ||
"eslint": "^6.2.1", | ||
"eslint-config-google": "^0.13.0", | ||
"husky": "^3.0.4", | ||
"jest": "^24.9.0", | ||
"jest-matcher-one-of": "^1.0.2", | ||
"lint-staged": "^9.2.3", | ||
"proxyquire": "^2.1.3", | ||
"semantic-release": "^15.13.24" | ||
}, | ||
"engines": { | ||
"node": ">=8", | ||
"npm": ">=6" | ||
}, | ||
"config": { | ||
"commitizen": { | ||
"path": "./node_modules/cz-conventional-changelog" | ||
} | ||
}, | ||
"husky": { | ||
"hooks": { | ||
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS", | ||
"pre-commit": "npx lint-staged" | ||
} | ||
}, | ||
"commitlint": { | ||
"extends": [ | ||
"@commitlint/config-conventional" | ||
] | ||
}, | ||
"lint-staged": { | ||
"*.{js}": [ | ||
"npm run lint", | ||
"git add" | ||
] | ||
} | ||
} |
109
README.md
# Yargs Interactive | ||
[![Build Status](https://travis-ci.org/nanovazquez/yargs-interactive.svg?branch=master)](https://travis-ci.org/nanovazquez/yargs-interactive) [![Coverage Status](https://coveralls.io/repos/github/nanovazquez/yargs-interactive/badge.svg)](https://coveralls.io/github/nanovazquez/yargs-interactive) [![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release) [![npm](https://img.shields.io/npm/v/yargs-interactive.svg?style=flat)](https://www.npmjs.com/package/yargs-interactive) | ||
@@ -24,24 +25,20 @@ [![npm](https://img.shields.io/npm/dw/yargs-interactive.svg)](https://www.npmjs.com/package/yargs-interactive) | ||
const yargsInteractive = require('yargs-interactive'); | ||
const yargsInteractive = require("yargs-interactive"); | ||
const options = { | ||
name: { type: 'input', default: 'A robot', describe: 'Enter your name' }, | ||
likesPizza: { type: 'confirm', default: false, describe: 'Do you like pizza?' }, | ||
name: { type: "input", default: "A robot", describe: "Enter your name" }, | ||
likesPizza: { type: "confirm", default: false, describe: "Do you like pizza?" } | ||
}; | ||
yargsInteractive() | ||
.usage('$0 <command> [args]') | ||
.usage("$0 <command> [args]") | ||
.interactive(options) | ||
.then((result) => { | ||
.then(result => { | ||
// Your business logic goes here. | ||
// Get the arguments from the result | ||
// e.g. myCli(result.name); | ||
console.log( | ||
`\nResult is:\n` | ||
+ `- Name: ${result.name}\n` | ||
+ `- Likes pizza: ${result.likesPizza}\n` | ||
); | ||
console.log(`\nResult is:\n` + `- Name: ${result.name}\n` + `- Likes pizza: ${result.likesPizza}\n`); | ||
}); | ||
``` | ||
Now, by simply wrapping your CLI code with this tool, you'll get all the information you need from the user. For instance, save the previous snipped in a file named *my-cli.js* and run it in your terminal: | ||
Now, by simply wrapping your CLI code with this tool, you'll get all the information you need from the user. For instance, save the previous snipped in a file named _my-cli.js_ and run it in your terminal: | ||
@@ -59,32 +56,32 @@ ``` | ||
It supports the following use cases | ||
* [Prompt questions with default values (full-interactive)](#prompt-questions-with-default-values-full-interactive) | ||
* [Prompt just some questions (mixed mode)](#prompt-just-some-questions-mixed-mode) | ||
* [No prompt at all (ye olde yargs)](#no-prompt-at-all-ye-olde-yargs) | ||
### Prompt questions with default values (full-interactive) | ||
- [Prompt all questions](#prompt-questions-with-default-values-full-interactive) | ||
- [Prompt some questions (mixed mode)](#prompt-just-some-questions-mixed-mode) | ||
- [No prompt at all (ye olde yargs)](#no-prompt-at-all-ye-olde-yargs) | ||
### Prompt questions (full-interactive) | ||
**my-cli.js** | ||
```js | ||
const yargsInteractive = require('yargs-interactive'); | ||
const yargsInteractive = require("yargs-interactive"); | ||
const options = { | ||
name: { | ||
type: 'input', | ||
default: 'nano', | ||
describe: 'Enter your name' | ||
type: "input", | ||
describe: "Enter your name" | ||
}, | ||
likesPizza: { | ||
type: 'confirm', | ||
default: false, | ||
describe: 'Do you like pizza?' | ||
}, | ||
type: "confirm", | ||
describe: "Do you like pizza?" | ||
} | ||
}; | ||
yargsInteractive() | ||
.usage('$0 <command> [args]') | ||
.usage("$0 <command> [args]") | ||
.interactive(options) | ||
.then((result) => { | ||
.then(result => { | ||
// The tool will prompt questions and will output your answers. | ||
// TODO: Do something with the result (e.g result.name) | ||
console.log(result) | ||
console.log(result); | ||
}); | ||
@@ -127,35 +124,38 @@ ``` | ||
| Property | Type | Description | | ||
| ---------- | -------------| ----------------------------- | | ||
| type | string | _(Required)_ The type of the option to prompt (e.g. `input`, `confirm`, etc.). **We provide all prompt types supported by [Inquirer](https://github.com/SBoudrias/Inquirer.js/#prompt-types).**| | ||
| describe | string | _(Required)_ The message to display when prompting the option (e.g. `Do you like pizza?`) | | ||
| default | any | The default value of the option. | | ||
| prompt | string | _(Default is `if-empty`)_ Property to decide whether to prompt the option or not. Possible values: `always`, `never`, `if-no-arg` (prompts if the option was not sent via command line parameters) and `if-empty` (prompts if the value was not sent via command line parameters and it doesn't have a default property). | | ||
| Property | Type | Description | | ||
| -------- | ------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | ||
| type | string | _(Required)_ The type of the option to prompt (e.g. `input`, `confirm`, etc.). **We provide all prompt types supported by [Inquirer](https://github.com/SBoudrias/Inquirer.js/#prompt-types).** | | ||
| describe | string | _(Required)_ The message to display when prompting the option (e.g. `Do you like pizza?`) | | ||
| default | any | The default value of the option. | | ||
| prompt | string | _(Default is `if-empty`)_ Property to decide whether to prompt the option or not. Possible values: `always`, `never`, `if-no-arg` (prompts if the option was not sent via command line parameters) and `if-empty` (prompts if the value was not sent via command line parameters and it doesn't have a default property). | | ||
### Prompt just some questions (mixed mode) | ||
### Prompt some questions (mixed mode) | ||
You can opt-out options from interactive mode by setting the `prompt` property to `never`. By default, its value is `if-empty`, prompting the question to the user if the value was not set via command line parameters or it doesn't have a default property. Setting to `if-no-arg` will only prompt the question if no argument is provided. Lastly, you can use `always` to always prompt the option. | ||
You can opt-out options from interactive mode by setting the `prompt` property to `never`. By default, its value is `if-empty`, prompting the question to the user if the value was not set via command line parameters, or if it doesn't have a default property. Setting it to `if-no-arg` will prompt the question if no argument is provided. Lastly, you can use `always` to _always prompt the option_. | ||
**my-cli.js** | ||
```js | ||
const yargsInteractive = require('yargs-interactive'); | ||
const yargsInteractive = require("yargs-interactive"); | ||
const options = { | ||
name: { | ||
// prompt property if not set defaults to 'if-empty' | ||
type: 'input', | ||
describe: 'Enter your name' | ||
// prompt property, if not set, defaults to 'if-empty' | ||
// In this case, it means the question will be prompted | ||
// if it is not provided by args, as it doesn't have a default value. | ||
type: "input", | ||
describe: "Enter your name" | ||
}, | ||
likesPizza: { | ||
type: 'confirm', | ||
type: "confirm", | ||
default: false, | ||
describe: 'Do you like pizza?', | ||
prompt: 'never' // because everyone likes pizza | ||
}, | ||
describe: "Do you like pizza?", | ||
prompt: "never" // because everyone likes pizza | ||
} | ||
}; | ||
yargsInteractive() | ||
.usage('$0 <command> [args]') | ||
.usage("$0 <command> [args]") | ||
.interactive(options) | ||
.then((result) => { | ||
.then(result => { | ||
// The tool will prompt questions output the answers. | ||
@@ -170,2 +170,3 @@ // You can opt-out options by using `prompt: 'never'`. For these properties, it | ||
**Usage in terminal** | ||
``` | ||
@@ -180,22 +181,23 @@ ➜ node my-cli.js --interactive | ||
**my-cli.js** | ||
```js | ||
const yargsInteractive = require('yargs-interactive'); | ||
const yargsInteractive = require("yargs-interactive"); | ||
const options = { | ||
name: { | ||
type: 'input', | ||
default: 'nano', | ||
describe: 'Enter your name' | ||
type: "input", | ||
default: "nano", | ||
describe: "Enter your name" | ||
}, | ||
likesPizza: { | ||
type: 'confirm', | ||
type: "confirm", | ||
default: false, | ||
describe: 'Do you like pizza?' | ||
}, | ||
describe: "Do you like pizza?" | ||
} | ||
}; | ||
yargsInteractive() | ||
.usage('$0 <command> [args]') | ||
.usage("$0 <command> [args]") | ||
.interactive(options) | ||
.then((result) => { | ||
.then(result => { | ||
// The tool will output the values set via parameters or | ||
@@ -209,4 +211,5 @@ // the default value (if not provided). | ||
**Usage in terminal** | ||
``` | ||
➜ node my-cli.js --name='Johh' --likesPizza | ||
``` |
/** | ||
* Filter keys in an object based on a condition. | ||
* @param {Object} element - The element whose keys will be filtered. | ||
* @param {any} condition - (value, key) => boolean | ||
* @param {object} element The element whose keys will be filtered. | ||
* @param {any} condition (value, key) => boolean | ||
* The condition to execute in each key value (i.e. if condition(value, key) returns true, the key is kept). | ||
* @return {object} - A new object with only the keys that pass the condition. | ||
* @return {object} A new object with only the keys that pass the condition. | ||
*/ | ||
@@ -8,0 +8,0 @@ module.exports = (element = {}, condition = () => true) => { |
const inquirer = require('inquirer'); | ||
/** | ||
* Initiate an interactive prompt to get values from the user. | ||
* @param {object} values The values to configure the prompt | ||
* @return {object} A promise that, when fullfilled, will contain answer of the questions prompted to the user | ||
*/ | ||
module.exports = (values = {}) => { | ||
@@ -11,3 +16,3 @@ const prompt = inquirer.createPromptModule(); | ||
message: value.describe, | ||
default: value.default, | ||
default: value.default | ||
}); | ||
@@ -14,0 +19,0 @@ }); |
/** | ||
* Checks if the argument received is provided in the argument collection | ||
* @param {Object} arg - The arg to check (e.g. `likesPizza`) | ||
* @param {Object} processArgs - The collection of process arguments (e.g. `["--interactive", "--likesPizza=true"]`) | ||
* @return {boolean} - Returns true if the argument is present in the collection of process arguments, false otherwise. | ||
* @param {Object} arg The arg to check (e.g. `likesPizza`) | ||
* @param {Object} processArgs The collection of process arguments (e.g. `["--interactive", "--likesPizza=true"]`) | ||
* @return {boolean} True if the argument is present in the collection of process arguments, false otherwise. | ||
*/ | ||
@@ -7,0 +7,0 @@ module.exports = (arg, processArgs) => { |
@@ -8,3 +8,3 @@ const yargs = require('yargs'); | ||
// Set up yargs options | ||
let yargsInteractive = (processArgs = process.argv.slice(2), cwd) => { | ||
const yargsInteractive = (processArgs = process.argv.slice(2), cwd) => { | ||
const yargsConfig = yargs(processArgs, cwd); | ||
@@ -49,5 +49,3 @@ | ||
// Check if we should get the values from the interactive mode | ||
return argv.interactive | ||
? interactiveMode(interactiveOptions).then((result) => Object.assign({}, argv, result)) | ||
: Promise.resolve(argv); | ||
return argv.interactive ? interactiveMode(interactiveOptions).then((result) => Object.assign({}, argv, result)) : Promise.resolve(argv); | ||
}; | ||
@@ -54,0 +52,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
15466
103
210
17
+ Addedansi-escapes@4.3.2(transitive)
+ Addedansi-regex@5.0.1(transitive)
+ Addedansi-styles@4.3.0(transitive)
+ Addedchalk@4.1.2(transitive)
+ Addedchardet@0.7.0(transitive)
+ Addedcli-cursor@3.1.0(transitive)
+ Addedcli-width@3.0.0(transitive)
+ Addedcolor-convert@2.0.1(transitive)
+ Addedcolor-name@1.1.4(transitive)
+ Addedemoji-regex@8.0.0(transitive)
+ Addedexternal-editor@3.1.0(transitive)
+ Addedfigures@3.2.0(transitive)
+ Addedhas-flag@4.0.0(transitive)
+ Addedinquirer@7.3.3(transitive)
+ Addedis-fullwidth-code-point@3.0.0(transitive)
+ Addedmimic-fn@2.1.0(transitive)
+ Addedmute-stream@0.0.8(transitive)
+ Addedonetime@5.1.2(transitive)
+ Addedrestore-cursor@3.1.0(transitive)
+ Addedrxjs@6.6.7(transitive)
+ Addedstring-width@4.2.3(transitive)
+ Addedstrip-ansi@6.0.1(transitive)
+ Addedsupports-color@7.2.0(transitive)
+ Addedtslib@1.14.1(transitive)
+ Addedtype-fest@0.21.3(transitive)
- Removedansi-escapes@3.2.0(transitive)
- Removedansi-styles@3.2.1(transitive)
- Removedchalk@2.4.2(transitive)
- Removedchardet@0.4.2(transitive)
- Removedcli-cursor@2.1.0(transitive)
- Removedcli-width@2.2.1(transitive)
- Removedcolor-convert@1.9.3(transitive)
- Removedcolor-name@1.1.3(transitive)
- Removedexternal-editor@2.2.0(transitive)
- Removedfigures@2.0.0(transitive)
- Removedhas-flag@3.0.0(transitive)
- Removedinquirer@4.0.2(transitive)
- Removedmute-stream@0.0.7(transitive)
- Removedonetime@2.0.1(transitive)
- Removedrestore-cursor@2.0.0(transitive)
- Removedrx-lite@4.0.8(transitive)
- Removedrx-lite-aggregates@4.0.8(transitive)
- Removedsupports-color@5.5.0(transitive)
Updatedinquirer@^7.0.0