-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
63 lines (48 loc) · 1.48 KB
/
index.js
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
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env node
const yargs = require('yargs');
const ora = require('ora');
const { table } = require('table');
const { getCardsInfo } = require('./prize-checker')
async function getBalance(cards) {
const spinner = ora('Checking your balance for your(s) prize card(s):\n').start();
let numbers = [];
if (typeof cards === 'string') {
numbers = cards.split(',').map(item => item.trim());
} else if (typeof cards === 'number') {
numbers = [cards.toString()];
} else if(Array.isArray(cards)) {
numbers = cards;
}
numbers = [...new Set(numbers)];
numbers.forEach(number => {
spinner.text += `\n 💳 [${number.slice(-3).padStart(number.length, '*')}]`
});
const cardsInfo = await getCardsInfo(numbers);
const data = cardsInfo.cards.reduce((acc, item) => {
acc.push([item.card, `${item.amount} €`]);
return acc;
}, [['Card Number', 'Amount']]);
data.push(['Total', `${cardsInfo.totalAmount} €`]);
spinner.succeed('💸 Balance calculated!')
console.log(table(data));
process.exit(0);
}
yargs
.alias('h', 'help')
.alias('v', 'version')
.help()
.command('* [cards]', 'checks the prize card numbers', yargs => {
yargs
.option('card', {
alias: 'c',
describe: 'Card number to verify',
default: [],
type: 'string'
})
}, async argv => await getBalance(argv.cards))
.argv;
process.on('uncaughtException', function (exception) {
if (exception.code === 'ETIMEDOUT') {
return;
}
});