UNPKG

@ahurein/travel-explorer

Version:

CLI utility to easily browse attractions in every country/city

62 lines (61 loc) 3.42 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import ora from 'ora'; import { customTable } from './helper.js'; import { getAttractionsByCity, getAttractionsByContinent, getAttractionsByCountry } from '../attractionsApi.js'; import chalk from "chalk"; import { confirm } from "@inquirer/prompts"; const spinner = ora('Loading unicorns'); const log = console.log; const table = customTable(["No", "Title", "Location", "Total Reviews", "Price (USD)", "Cancellation", "Overall Rating", "Image", "Reviews", "Overview", "URL"]); //locations are country, continent export const displayLocationAttractions = (location, type, city) => __awaiter(void 0, void 0, void 0, function* () { let count = 0, page = 1, attractions; while (true) { spinner.start(); switch (type) { case "continent": attractions = yield getAttractionsByContinent(location, page); break; case "country": attractions = yield getAttractionsByCountry(location, page); break; case "city": attractions = yield getAttractionsByCity(location, city, page); break; default: attractions = yield getAttractionsByCountry(location, page); } spinner.stop(); if (attractions.message) return log(chalk.red(attractions.message)); if (!(attractions === null || attractions === void 0 ? void 0 : attractions.total)) return log(chalk.red(`Sorry, We do not have any attractions on ${location}`)); log(`Attractions found: ${chalk.bold.green(attractions === null || attractions === void 0 ? void 0 : attractions.total)}`); attractions.attractions.forEach(({ attraction }) => { const overview = `${attraction.overview}`.substring(0, 60) + "..."; let review = ""; count += 1; if (Number(attraction.total_reviews) && attraction.reviews[0].body) { review = `${attraction.reviews[0].body}`.substring(0, 60) + "..."; } const { title, city, total_reviews, price, cancellation, overall_rating, main_image, url, country } = attraction; const row = [count, title, `${city},\n${country}`, total_reviews, price, cancellation, overall_rating, { content: chalk.yellow("View image"), href: main_image }, review, overview, { content: chalk.yellow("Book attraction"), href: url }]; table.push(row); }); log(table.toString()); if (!attractions.hasMore) return log(chalk.green("No more data!")); const fetchMore = yield confirm({ message: "Do you want to fetch more attractions: " }); if (!fetchMore) return; page += 1; } });