This repository has been archived by the owner on Feb 24, 2022. It is now read-only.
forked from unclestay/3-musketeers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcash.js
54 lines (46 loc) · 1.34 KB
/
cash.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
'use strict';
const got = require('got');
const money = require('money');
const chalk = require('chalk');
const ora = require('ora');
const currencies = require('../lib/currencies.json');
const {API} = require('./constants');
const cash = async command => {
const {amount} = command;
const from = command.from.toUpperCase();
const to = command.to.filter(item => item !== from).map(item => item.toUpperCase());
console.log(from)
console.log(to)
console.log();
const loading = ora({
text: 'Converting...',
color: 'green',
spinner: {
interval: 150,
frames: to
}
});
loading.start();
await got(API, {
json: true
}).then(response => {
money.base = response.body.base;
money.rates = response.body.rates;
to.forEach(item => {
if (currencies[item]) {
loading.succeed(`${chalk.green(money.convert(amount, {from, to: item}).toFixed(3))} ${`(${item})`} ${currencies[item]}`);
} else {
loading.warn(`${chalk.yellow(`The "${item}" currency not found `)}`);
}
});
console.log(chalk.underline.gray(`\nConversion of ${chalk.bold(from)} ${chalk.bold(amount)}`));
}).catch(error => {
if (error.code === 'ENOTFOUND') {
loading.fail(chalk.red('Please check your internet connection!\n'));
} else {
loading.fail(chalk.red(`Internal server error :(\n${error}`));
}
process.exit(1);
});
};
module.exports = cash;