javascript - 如何将参数传递给询问者问题?
问题描述
如何将参数传递给询问者问题,以便我可以根据先前问题的值或提示之外的代码设置问题对象的值?
如果基于上一个问题的答案,我能看到实现此目的的唯一方法是嵌套查询器提示调用
const inquirer = require('inquirer');
function getPath(){
return {
'system1':`system1path`,
'system2':`system2path`,
'system3':`system3path`
}
}
inquirer.prompt([
{
type: 'list',
name: 'testSystem',
message: 'Which system do you want to run?',
choices: ['system1', 'system2', 'system3']
},
{
type: 'fuzzypath',
name: 'searchTestSuite',
excludePath: nodePath => nodePath.startsWith('node_modules'),
itemType: 'any',
rootPath: getPath()['< answer from question(testSystem) >'],
message: 'Select a target directory :',
default: `system1path`,
suggestOnly: false,
depthLimit: 6,
},
]).then(answers => {
console.log(answers);
});
预期结果:如果您选择 testSystem = system2 您应该得到 rootPath = system2Path ,而不嵌套查询器提示或使用when
函数(因为when
似乎正在处理布尔值)
解决方案
您可以通过嵌套来解决它,但将 from 更改Promise.then
为async-await
使其更具可读性。Inquirer.js 使用 Promise,因此您可以使用 await 来捕获提示答案,并通过发出多个提示,您可以保存提示之间的状态。请参阅下面的代码。
PS:我已经default: ...,
从fuzzypath 中删除了该参数,因为它产生了默认值,尽管它在根路径之外。
const inquirer = require('inquirer');
inquirer.registerPrompt('fuzzypath', require('inquirer-fuzzy-path'))
const system1path = ...;
const system2path = ...;
const system3path = ...;
function getPath(){
return {
'system1': `${system1path}`,
'system2': `${system2path}`,
'system3': `${system3path}`
};
}
(async function () {
const {testSystem} = await inquirer.prompt({
type: 'list',
name: 'testSystem',
message: 'Which system do you want to run?',
choices: ['system1', 'system2', 'system3']
});
const {searchTestSuite} = await inquirer.prompt({
type: 'fuzzypath',
name: 'searchTestSuite',
excludePath: nodePath => nodePath.startsWith('node_modules'),
itemType: 'any',
rootPath: getPath()[testSystem],
message: 'Select a target directory :',
suggestOnly: false,
depthLimit: 6,
});
console.log({testSystem, searchTestSuite});
})();
推荐阅读
- laravel - 如何在 Laravel 验证中添加错误消息?
- swift - 如何获取放置在表格视图单元格上的标签的值
- java - 在java中使用aws sdk版本2的合理好方法,请求apigateway
- python - 我正在尝试为 SQL 加载 python 扩展,但是当我尝试时我得到了这个
- mysql - 如何从mysql中的表中跳过行
- c++ - C++ 模板 - 在类外实现
- tensorflow - 使用 TPUClusterResolver 时为 TPU 转储 HLO IR
- android - React Native - 构建成功但没有 APK 输出
- javascript - 如何检查一个人是否通过邀请链接吸引了更多人订阅我的电报频道?
- android - 创建 Xamarin 应用程序并覆盖其他应用程序