Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(inputs): use enquirer instead of inquirer #52

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
198 changes: 72 additions & 126 deletions package-lock.json

Large diffs are not rendered by default.

9 changes: 4 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,12 @@
"gmtft": "./build/gmtft.js"
},
"dependencies": {
"@babel/runtime": "^7.0.0-rc.1",
"@babel/runtime": "^7.4.5",
"@google/maps": "^0.5.5",
"cli-table3": "^0.5.1",
"commander": "^2.17.1",
"fuzzy": "^0.1.3",
"inquirer": "^6.1.0",
"inquirer-autocomplete-prompt": "^1.0.1",
"commander": "^2.20.0",
"enquirer": "^2.3.0",
"lodash.debounce": "^4.0.8",
"striptags": "^3.1.1"
},
"devDependencies": {
Expand Down
38 changes: 0 additions & 38 deletions src/GoogleMapsService.js

This file was deleted.

47 changes: 0 additions & 47 deletions src/LocationSelector.js

This file was deleted.

27 changes: 27 additions & 0 deletions src/createDirectionsService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import GoogleMapsClient from '@google/maps';

export default function createDirectionsService(apiKey) {
const client = GoogleMapsClient.createClient({
key: apiKey,
Promise,
});

async function getDirections({
destination,
origin,
travelMode,
}) {
const {
json,
} = await client.directions({
destination,
origin,
mode: travelMode,
}).asPromise();
return json;
}

return {
getDirections,
};
}
19 changes: 19 additions & 0 deletions src/createLocationsSearcher.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import GoogleMapsClient from '@google/maps';

export default function createLocationsSearcher(apiKey) {
const client = GoogleMapsClient.createClient({
key: apiKey,
Promise,
});

async function searchLocations(address) {
const {
json,
} = await client.geocode({ address }).asPromise();
return json;
}

return {
searchLocations,
};
}
2 changes: 2 additions & 0 deletions src/gmtft.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/usr/bin/env node

/* eslint no-console: 0 */

import execute from '..';

try {
Expand Down
32 changes: 17 additions & 15 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
/* eslint no-console: 0 */

import createRouteTable from './createRouteTable';
import {
GEOCODE_API_KEY,
DIRECTIONS_API_KEY,
} from './constants';
import GoogleMapsService from './GoogleMapsService';
import LocationSelector from './LocationSelector';
import selectTravelMode from './selectTravelMode';
import translateRoute from './translateRoute';
import createDirectionsService from './createDirectionsService';
import selectLocation from './selectLocation';

const execute = async () => {
const googleMapsService = new GoogleMapsService({
directionsAPIKey: DIRECTIONS_API_KEY,
geocodeAPIKey: GEOCODE_API_KEY,
});
const originSelector = selectLocation({ message: 'Select start location' });
const originResult = await originSelector.run();

const locationSelector = new LocationSelector(googleMapsService);
const origin = await locationSelector.selectLocation('Select start location');
const destination = await locationSelector.selectLocation('Select end location');
const destinationSelector = selectLocation({ message: 'Select end location' });
const destinationResult = await destinationSelector.run();

const travelMode = await selectTravelMode();
const travelModeSelector = selectTravelMode({ message: 'Select travel mode' });
const travelModeResult = await travelModeSelector.run();

const { routes } = await googleMapsService.getDirections({
origin,
destination,
travelMode,
const directionsService = createDirectionsService(DIRECTIONS_API_KEY);
const {
routes,
} = await directionsService.getDirections({
origin: originResult.location,
destination: destinationResult.location,
travelMode: travelModeResult,
});

routes
Expand Down
19 changes: 19 additions & 0 deletions src/searchLocations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
function parseLocations(locations) {
return locations.map((location) => {
const {
geometry,
formatted_address: formattedAddress,
} = location;
const parsedLocation = {
latitude: geometry.location.lat,
longitude: geometry.location.lng,
formattedAddress,
};
return parsedLocation;
});
}

export default async function searchLocations({ input, locationsSearcher }) {
const searchResults = await locationsSearcher.searchLocations(input);
return parseLocations(searchResults.results);
}
45 changes: 45 additions & 0 deletions src/selectLocation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import {
AutoComplete,
} from 'enquirer';
import debounce from 'lodash.debounce';

import {
GEOCODE_API_KEY,
} from './constants';
import createLocationsSearcher from './createLocationsSearcher';
import searchLocations from './searchLocations';

const searcher = createLocationsSearcher(GEOCODE_API_KEY);
const debouncedSearchLocations = debounce(searchLocations, 500, { leading: false, trailing: true });

async function generateSuggestions(input) {
const results = await debouncedSearchLocations({ input, locationsSearcher: searcher });
if (results && results.length > 0) {
return results.map(location => ({
name: location.formattedAddress,
message: location.formattedAddress,
value: location.formattedAddress,
location,
}));
}
return [];
}

export default function selectLocation({ message }) {
return new AutoComplete({
name: 'location',
limit: 5,
choices: [],
message,
suggest: generateSuggestions,
// override the default behavior of using number keys
// to select choices, so the user can enter a value for "address"
number(...args) {
return this.append(...args);
},
// this will return the entire choice object
result(value) {
return this.choices.find(choice => choice.name === value);
},
});
}
41 changes: 15 additions & 26 deletions src/selectTravelMode.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,21 @@
import inquirer from 'inquirer';
import inquirerAutocompletePrompt from 'inquirer-autocomplete-prompt';
import fuzzy from 'fuzzy';
import {
AutoComplete,
} from 'enquirer';

import { TRAVEL_MODE } from './constants';

inquirer.registerPrompt('autocomplete', inquirerAutocompletePrompt);

const getTravelModeKey = mode => `${mode.emoji} (${mode.value})`;

const formattedTravelModesToValues = Object.freeze({
[getTravelModeKey(TRAVEL_MODE.DRIVING)]: TRAVEL_MODE.DRIVING,
[getTravelModeKey(TRAVEL_MODE.WALKING)]: TRAVEL_MODE.WALKING,
[getTravelModeKey(TRAVEL_MODE.BICYCLING)]: TRAVEL_MODE.BICYCLING,
[getTravelModeKey(TRAVEL_MODE.TRANSIT)]: TRAVEL_MODE.TRANSIT,
});

const formattedTravelModes = Object.keys(formattedTravelModesToValues);

const selectTravelMode = async () => {
const { travelMode } = await inquirer.prompt([
{
type: 'autocomplete',
name: 'travelMode',
message: 'Select your travel mode',
source: (_, input) => Promise.resolve(fuzzy.filter(input || '', formattedTravelModes).map(match => match.original)),
},
]);
return formattedTravelModesToValues[travelMode];
};
const choices = Object.values(TRAVEL_MODE).map(mode => ({
message: getTravelModeKey(mode),
name: getTravelModeKey(mode),
value: mode.value,
}));

export default selectTravelMode;
export default function selectTravelMode({ message }) {
return new AutoComplete({
name: 'travelMode',
message,
choices,
});
}