我想向用户提问,但又不想立即列出所有问题。
该文档提到了 rxjs,但我觉得文档中关于如何在执行提示时正确添加问题的问题存在差距,或者至少它对我来说不太有效。
https://www.npmjs.com/package/inquirer#reactive-interface
在内部,Inquirer 使用 JS 反应式扩展来处理事件和异步流。
这意味着您可以利用此功能来提供更高级的流程。例如,您可以动态添加要询问的问题:
var prompts = new Rx.Subject();
inquirer.prompt(prompts);
// At some point in the future, push new questions
prompts.next({
/* question... */
});
prompts.next({
/* question... */
});
// When you're done
prompts.complete();
Run Code Online (Sandbox Code Playgroud)
使用返回值处理属性,您可以访问更细粒度的回调:
inquirer.prompt(prompts).ui.process.subscribe(onEachAnswer, onError, onComplete);
Run Code Online (Sandbox Code Playgroud) 在创作自耕农生成器时,我想在我的一个提示中使用查询器的“自动完成”插件(https://github.com/mokkabonna/inquirer-autocomplete-prompt)。我似乎无法在文档中找到任何说明如何从自耕农生成器中向查询器注册此插件的内容。这可能吗?
我想知道如何为npm包Inquirer.js编写单元测试,这是使CLI包更容易使用的工具。我已经阅读了这篇文章,但无法使其正常工作。
这是我的代码,需要进行测试:
const questions = [
{
type: 'input',
name: 'email',
message: "What's your email ?",
},
{
type: 'password',
name: 'password',
message: 'Enter your password (it will not be saved neither communicate for other purpose than archiving)'
}
];
inquirer.prompt(questions).then(answers => {
const user = create_user(answers.email, answers.password);
let guessing = guess_unix_login(user);
guessing.then(function (user) {
resolve(user);
}).catch(function (message) {
reject(message);
});
} );
Run Code Online (Sandbox Code Playgroud)
...这是用Mocha编写的测试:
describe('#create_from_stdin', function () {
this.timeout(10000);
check_env(['TEST_EXPECTED_UNIX_LOGIN']);
it('should find the unix_login user and …Run Code Online (Sandbox Code Playgroud) stdin unit-testing command-line-interface node.js inquirerjs
const inquirer = require("inquirer")
var questions = [
{
type: "number",
name: "name",
message: "Please the number of players",
validate: function (name) {
var valid = Number.isInteger(name)
return valid || `Please enter a valid whole number`
},
},
]
function promptUser() {
inquirer
.prompt(questions)
.then((answers) => {
console.log(`You entered ${answers["name"]}!`)
})
.catch((error) => console.log(`Please enter a number`))
}
promptUser()
Run Code Online (Sandbox Code Playgroud)
考虑到上面的代码,我在类似这样的旧视频中注意到,如果您包含验证并且失败,输入将被清除。然而,就我而言,我得到的 NaN 不会自动清除。假设我启动应用程序并输入“abcdefg”:
? Please the number of players NaN
>> Please enter a valid whole number
Run Code Online (Sandbox Code Playgroud)
如果我输入任何内容,它只会添加到 NaN …
默认情况下,询问者 js 问题前面有一个“?”。是否可以将其更改为其他内容?我尝试探索但没有发现任何地方提到它。
我正在使用Inquirer.js与用户进行交互.在其中一个对话框中,用户可以指定以逗号分隔的"项目"列表.以下验证函数检查API,"item"是否可用.如果不是,则提示用户对其先前的选择进行校正.
当前代码:
validate: async str => {
const items = str.split(',');
for(let item of items) {
try {
await axios.get(`https://api.example.com/items/${item}`);
} catch(e) {
return `The item '${item}' could not be found`;
}
}
// data exists, but is irrelevant
return true;
}
Run Code Online (Sandbox Code Playgroud)
虽然这一切都按预期工作,但迭代一系列包并调用axios.get()每个项目似乎非常低效.有没有办法并行运行这些并在其中一个请求失败后中断?
同样,我对项目的实际数据不感兴趣,只是它是否存在(等于true)(返回错误消息).
inquirerjs ×6
node.js ×4
javascript ×3
inquirer ×2
axios ×1
rxjs ×1
stdin ×1
unit-testing ×1
yeoman ×1