|
3 | 3 | // Makes the script crash on unhandled rejections instead of silently |
4 | 4 | // ignoring them. In the future, promise rejections that are not handled will |
5 | 5 | // terminate the Node.js process with a non-zero exit code. |
6 | | -process.on('unhandledRejection', err => { |
| 6 | +process.on("unhandledRejection", err => { |
7 | 7 | handleError(`UNHANDLED ERROR`, err); |
8 | 8 | }); |
9 | 9 |
|
10 | | -const ora = require('ora'); |
11 | | -const spinner = ora({ text: '' }); |
12 | | -const Table = require('cli-table3'); |
13 | | -const cli = require('./utils/cli.js'); |
14 | | -const init = require('./utils/init.js'); |
15 | | -const getAll = require('./utils/getAll.js'); |
16 | | -const theEnd = require('./utils/theEnd.js'); |
17 | | -const handleError = require('cli-handle-error'); |
18 | | -const getCountry = require('./utils/getCountry.js'); |
19 | | -const getStates = require('./utils/getStates.js'); |
20 | | -const getWorldwide = require('./utils/getWorldwide.js'); |
21 | | -const { single, colored, singleStates, coloredStates, style } = require('./utils/table.js'); |
| 10 | +const ora = require("ora"); |
| 11 | +const spinner = ora({ text: "" }); |
| 12 | +const Table = require("cli-table3"); |
| 13 | +const cli = require("./utils/cli.js"); |
| 14 | +const init = require("./utils/init.js"); |
| 15 | +const getAll = require("./utils/getAll.js"); |
| 16 | +const showHelp = require("./utils/showHelp.js"); |
| 17 | +const theEnd = require("./utils/theEnd.js"); |
| 18 | +const handleError = require("cli-handle-error"); |
| 19 | +const getCountry = require("./utils/getCountry.js"); |
| 20 | +const getStates = require("./utils/getStates.js"); |
| 21 | +const getWorldwide = require("./utils/getWorldwide.js"); |
| 22 | +const { |
| 23 | + single, |
| 24 | + colored, |
| 25 | + singleStates, |
| 26 | + coloredStates, |
| 27 | + style |
| 28 | +} = require("./utils/table.js"); |
22 | 29 | const xcolor = cli.flags.xcolor; |
23 | 30 | const sortBy = cli.flags.sort; |
24 | | -let isState = false; |
25 | 31 |
|
26 | 32 | (async () => { |
27 | 33 | // Init. |
28 | 34 | init(); |
29 | | - const [country] = cli.input; |
30 | | - if (country === 'help') { |
31 | | - cli.showHelp(0); |
32 | | - } |
33 | | - if (country === 'states') { |
34 | | - isState = true; |
35 | | - } |
| 35 | + const [input] = cli.input; |
| 36 | + await showHelp(); |
| 37 | + const states = input === "states" ? true : false; |
| 38 | + const country = input; |
36 | 39 |
|
37 | 40 | // Table |
38 | | - let head; |
39 | | - if (xcolor) { |
40 | | - head = isState ? singleStates : single; |
41 | | - } else { |
42 | | - head = isState ? coloredStates : colored; |
43 | | - } |
44 | | - const table = new Table({ head, style }); |
| 41 | + const head = xcolor ? single : colored; |
| 42 | + const headStates = xcolor ? singleStates : coloredStates; |
| 43 | + const table = !states |
| 44 | + ? new Table({ head, style }) |
| 45 | + : new Table({ head: headStates, style }); |
45 | 46 |
|
46 | 47 | // Display data. |
47 | 48 | spinner.start(); |
48 | | - if (isState) { |
49 | | - await getStates(spinner, table); |
50 | | - } else { |
51 | | - await getWorldwide(table); |
52 | | - await getCountry(spinner, table, country); |
53 | | - } |
54 | | - await getAll(spinner, table, country, { sort: sortBy }); |
| 49 | + await getWorldwide(table, states); |
| 50 | + await getCountry(spinner, table, states, country); |
| 51 | + await getStates(spinner, table, states, { sort: sortBy }); |
| 52 | + await getAll(spinner, table, states, country, { sort: sortBy }); |
55 | 53 |
|
56 | | - theEnd(isState); |
| 54 | + theEnd(states); |
57 | 55 | })(); |
0 commit comments