import { Answer } from '../models/choice' import inquirer from 'inquirer' import inquirerPrompt from 'inquirer-autocomplete-prompt' import { getCountryFromTimezone } from '../templates/location/detect_node_location' import { getCountriesList } from '../utils/countries' export async function confirmCountryGuessQuestion(): Promise { const country = getCountryFromTimezone() return inquirer.prompt([ { name: 'confirmCountryGuess', type: 'input', message: `Is this '${country}' correct country of node? (yes|no)`, validate(input: string): boolean | Promise { input = input.toLowerCase() return input == 'yes' || input == 'no' || input == 'y' || input == 'n' }, }, ]) } export async function countryQuestion(): Promise { inquirer.registerPrompt('autocomplete', inquirerPrompt) return inquirer.prompt([ { type: 'autocomplete', name: 'nodeCountry', default: '', message: 'Select country of node:', source: searchCountry, }, ]) } function searchCountry(answers: any, uInput = '') { const countryList = getCountryArray() return new Promise((resolve) => { const results: any[] = countryList .filter((input) => { return input.name.toLowerCase().includes(uInput.toLowerCase()) }) .map((el) => el.name) results.push(new inquirer.Separator()) resolve(results) }) } interface Country { name: string code: string } function getCountryArray(): Country[] { const countryList = getCountriesList() return Object.keys(countryList).map((key) => { return { name: countryList[key], code: key, } }) }