-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·51 lines (39 loc) · 1.68 KB
/
Copy pathcli.js
File metadata and controls
executable file
·51 lines (39 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env node
'use strict';
const meow = require('meow');
const cheapestAirfareAmx = require('cheapest-airfare-amx');
const currencyFormatter = require('currency-formatter');
const getValidatedOptions = require('./lib/get-validated-options');
const hasNotificationsEnabled = require('./lib/has-notifications-enabled')();
const cli = meow(`
Usage
$ amx-tracker <options>
Options
--from <airport> Origin airport code
--to <airport> Destination airport code
--departure <date> Departure date in YYYY-MM-DD
[--return <date>] Return date in YYYY-MM-DD (optional, leave out if one-way)
[--deal-price <date>] Desired total price in Mexican Pesos (optional, leave out to get current cheapest total)
[--interval <minutes>] Number of minutes until next run (optional, 30 by default)
`);
const options = getValidatedOptions(cli.flags);
const execute = () => {
cheapestAirfareAmx(options)
.then(result => {
if (!options.dealPrice) {
console.log(`Cheapest total: ${currencyFormatter.format(result.total, {code: 'MXN'})}. Check it out here: ${result.source}`);
return;
}
if (result.total <= options.dealPrice) {
const notificationMessage = `Deal alert! New total: ${currencyFormatter.format(result.total, {code: 'MXN'})}. Check it out here: ${result.source}`;
if (options.dealPrice && hasNotificationsEnabled) {
const sendNotification = require('./lib/send-notification');
sendNotification(notificationMessage);
}
console.log(notificationMessage);
}
setTimeout(execute, options.interval * 60 * 1000);
})
.catch(console.error);
};
execute();