fix: workaround inquirer validation not working properly

This commit is contained in:
Sérgio Salgado 2022-01-24 18:25:27 +00:00
parent 4b910ff87b
commit fc5a13fcf3

View file

@ -125,6 +125,7 @@ function getBlockchainSyncStatus (cryptoList) {
return wallet.checkBlockchainStatus(settings, value.cryptoCode)
.then(res => _.includes(value.cryptoCode, _.keys(installedCryptos) ? ({ ...acc, [value.cryptoCode]: res }) : ({ ...acc })))
.catch(() => {
console.log('processStatus', processStatus)
if (processStatus === 'RUNNING') {
installedButNotConfigured.push(value.cryptoCode)
return { ...acc, [value.cryptoCode]: 'syncing' }
@ -157,29 +158,37 @@ function run () {
const questions = []
questions.push({
type: 'checkbox',
name: 'crypto',
message: 'Which cryptocurrencies would you like to install?\nTo prevent server resource overloading, only TWO coins should be syncing simultaneously.\nMore coins can be installed after this process is over.',
choices,
validate: (answers) => {
if (_.size(answers) > 2) return `Please insert a maximum of two coins to install.`
const validateAnswers = async (answers) => {
if (_.size(answers) > 2) return { message: `Please insert a maximum of two coins to install.`, isValid: false }
return getBlockchainSyncStatus(cryptos)
.then(({ blockchainStatuses, installedButNotConfigured }) => {
if (!_.isEmpty(installedButNotConfigured)) {
return `Detected ${_.join(' and ', installedButNotConfigured)} installed on this machine, but couldn't establish connection. ${_.size(installedButNotConfigured) === 1 ? `Is this plugin` : `Are these plugins`} configured via admin?`
return { message: `Detected ${_.join(' and ', installedButNotConfigured)} installed on this machine, but couldn't establish connection. ${_.size(installedButNotConfigured) === 1 ? `Is this plugin` : `Are these plugins`} configured via admin?`, isValid: false}
}
const result = _.reduce((acc, value) => ({ ...acc, [value]: _.isNil(acc[value]) ? 1 : acc[value] + 1 }), {}, _.values(blockchainStatuses))
if (result.syncing > 2) {
return `There are currently more than 2 blockchains in their initial synchronization. Please try again later.`
return { message: `There are currently more than 2 blockchains in their initial synchronization. Please try again later.`, isValid: false }
}
return true
return { message: null, isValid: true }
})
}
questions.push({
type: 'checkbox',
name: 'crypto',
message: 'Which cryptocurrencies would you like to install?\nTo prevent server resource overloading, only TWO coins should be syncing simultaneously.\nMore coins can be installed after this process is over.',
choices
})
inquirer.prompt(questions)
.then(answers => processCryptos(answers.crypto))
.then(answers => Promise.all([validateAnswers(answers), answers]))
.then(([res, answers]) => {
console.log('res', res)
if (res.isValid) {
return processCryptos(answers.crypto)
}
console.log(res.message)
})
}