├── .npmrc ├── .gitignore ├── .gitattributes ├── .travis.yml ├── .editorconfig ├── test.js ├── package.json ├── license ├── index.js └── readme.md /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto 2 | *.js text eol=lf 3 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - '12' 4 | - '10' 5 | - '8' 6 | - '6' 7 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | import m from '.'; 3 | 4 | test('invalid argument', t => { 5 | t.throws(() => m(), 'Expected `question` to be of type `string`, got `undefined`'); 6 | t.throws(() => m(1), 'Expected `question` to be of type `string`, got `number`'); 7 | }); 8 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "listr-input", 3 | "version": "0.2.1", 4 | "description": "Input module for Listr", 5 | "license": "MIT", 6 | "repository": "SamVerschueren/listr-input", 7 | "author": { 8 | "name": "Sam Verschueren", 9 | "email": "sam.verschueren@gmail.com", 10 | "url": "github.com/SamVerschueren" 11 | }, 12 | "engines": { 13 | "node": ">=6" 14 | }, 15 | "scripts": { 16 | "test": "xo && ava" 17 | }, 18 | "files": [ 19 | "index.js" 20 | ], 21 | "keywords": [ 22 | "listr", 23 | "input", 24 | "question", 25 | "message" 26 | ], 27 | "dependencies": { 28 | "inquirer": "^7.0.0", 29 | "inquirer-autosubmit-prompt": "^0.2.0", 30 | "rxjs": "^6.5.3", 31 | "through": "^2.3.8" 32 | }, 33 | "devDependencies": { 34 | "ava": "^0.23.0", 35 | "xo": "^0.24.0" 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Sam Verschueren (github.com/SamVerschueren) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | const through = require('through'); 3 | const inquirer = require('inquirer'); 4 | const {Observable} = require('rxjs'); 5 | const autosubmit = require('inquirer-autosubmit-prompt'); 6 | 7 | module.exports = (question, options) => { 8 | options = Object.assign({ 9 | secret: false, 10 | done: () => { } 11 | }, options); 12 | 13 | if (typeof question !== 'string') { 14 | throw new TypeError(`Expected \`question\` to be of type \`string\`, got \`${typeof question}\``); 15 | } 16 | 17 | const questions = [ 18 | { 19 | type: 'autosubmit', 20 | name: 'result', 21 | message: question, 22 | secret: options.secret, 23 | default: options.default, 24 | validate: options.validate, 25 | autoSubmit: options.autoSubmit 26 | } 27 | ]; 28 | 29 | return new Observable(observer => { 30 | let buffer = ''; 31 | 32 | const outputStream = through(data => { 33 | // eslint-disable-next-line no-control-regex 34 | if (/\u001B\[.*?(D|C)$/.test(data)) { 35 | if (buffer.length > 0) { 36 | observer.next(buffer); 37 | buffer = ''; 38 | } 39 | 40 | return; 41 | } 42 | 43 | buffer += data; 44 | }); 45 | 46 | const prompt = inquirer.createPromptModule({ 47 | output: outputStream 48 | }); 49 | prompt.registerPrompt('autosubmit', autosubmit); 50 | 51 | prompt(questions) 52 | .then(answer => { 53 | // Clear the output 54 | observer.next(); 55 | 56 | return options.done(answer.result); 57 | }) 58 | .then(() => { 59 | observer.complete(); 60 | }) 61 | .catch(error => { 62 | observer.error(error); 63 | }); 64 | 65 | return outputStream; 66 | }); 67 | }; 68 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # listr-input [![Build Status](https://travis-ci.org/SamVerschueren/listr-input.svg?branch=master)](https://travis-ci.org/SamVerschueren/listr-input) 2 | 3 | > Input module for [Listr](https://github.com/SamVerschueren/listr) 4 | 5 | 6 | ## Install 7 | 8 | ``` 9 | $ npm install --save listr-input 10 | ``` 11 | 12 | 13 | ## Usage 14 | 15 | ```js 16 | const Listr = require('listr'); 17 | const input = require('listr-input'); 18 | const got = require('got'); 19 | 20 | const list = new Listr([ 21 | { 22 | title: 'Retrieving data', 23 | task: () => input('Credentials', { 24 | secret: true, 25 | validate: value => value.length > 0, 26 | done: credentials => got('https://myapi.com', { 27 | headers: { 28 | 'Authorization': `Bearer ${credentials}` 29 | } 30 | }) 31 | }) 32 | } 33 | ]); 34 | 35 | list.run(); 36 | ``` 37 | 38 | 39 | ## API 40 | 41 | ### input(question, [options]) 42 | 43 | Returns an Observable which asks for user input. 44 | 45 | #### question 46 | 47 | Type: `string` 48 | 49 | Question to ask. 50 | 51 | #### options 52 | 53 | ##### default 54 | 55 | Type: `string` 56 | 57 | Default value to use if nothing is entered. 58 | 59 | ##### validate 60 | 61 | Type: `function` 62 | 63 | Function which accepts the provided value. Should return `true` if the value is valid, `false` otherwise. 64 | 65 | ##### secret 66 | 67 | Type: `boolean`
68 | Default: `false` 69 | 70 | Mark the input as secret. 71 | 72 | ##### done 73 | 74 | Type: `function` 75 | 76 | Function that will be invoked when the user has answered the question. 77 | 78 | ##### autoSubmit 79 | 80 | Type: `function` 81 | 82 | Function which accepts the provided value. If returns `true` then the value will be submitted automatically. 83 | 84 | 85 | 86 | ## License 87 | 88 | MIT © [Sam Verschueren](https://github.com/SamVerschueren) 89 | --------------------------------------------------------------------------------