UNPKG

@benshi.ai/js-sdk

Version:

Benshi SDK

56 lines (44 loc) 1.48 kB
const { default: axios } = require("axios"); const { Command } = require('commander'); const chalk = require('chalk'); const assert = require('assert'); const fs = require("fs"); const validateFormat = (conversions) => { assert(conversions.date !== undefined) assert(conversions.usd !== undefined) assert(Object.keys(conversions.usd).length !== 0) } const replaceFile = (contents, filePath) => { fs.writeFile(filePath, contents, (err) => { if (err) { throw new Error('error-writing-file') } }) } const printError = (err) => { console.log() console.log(chalk.red('Some error occurred: ')); console.log(chalk.red(err)) console.log() } (async () => { const CONVERSIONS_URL = 'https://cdn.jsdelivr.net/gh/fawazahmed0/currency-api@1/latest/currencies/usd.json' const USD_FILE_PATH = `${__dirname}/../src/core/repositories/currency/usd.json` const program = new Command(); program .option('--replace', 'Replaces current usd.json') program.parse() const options = program.opts(); try { const { data: conversions } = await axios.get(CONVERSIONS_URL) validateFormat(conversions) // if (options.replace) { console.log(conversions) replaceFile(JSON.stringify(conversions), USD_FILE_PATH) // }else{ // console.log("file not updated") // } } catch (e) { printError(e.message) } })()