我正在尝试创建一个小型构建脚本,如果在默认路径中找不到它们,它将向用户询问 mysql 标头的位置。现在我inquirer
用来提示用户输入效果很好,但我遇到了以下问题:
'use strict'
const inquirer = require('inquirer')
const fs = require('fs')
const MYSQL_INCLUDE_DIR = '/usr/include/mysql'
let questions = [
{
type: 'input',
name: 'MYSQL_INCLUDE_DIR',
message: 'Enter path to mysql headers',
default: MYSQL_INCLUDE_DIR,
when: (answers) => {
return !fs.existsSync(MYSQL_INCLUDE_DIR)
},
validate: (path) => {
return fs.existsSync(path)
}
}
]
inquirer.prompt(questions)
.then((answers) => {
// Problem is that answers.MYSQL_INCLUDE_DIR might be undefined at this point.
})
如果找到 mysql 标头的默认路径,则不会显示问题,因此不会设置答案。如何在不实际向用户显示的情况下为问题设置默认值?
解决上述问题也可以做到这一点,而不是使用全局变量:
let questions = [
{
type: 'input',
name: 'MYSQL_INCLUDE_DIR',
message: 'Enter path to mysql headers',
default: MYSQL_INCLUDE_DIR,
when: (answers) => {
return !fs.existsSync(answers.MYSQL_INCLUDE_DIR)
},
validate: (path) => {
return fs.existsSync(path)
}
}
]