diff --git a/package.json b/package.json index f477648..bb13d7f 100644 --- a/package.json +++ b/package.json @@ -2,11 +2,18 @@ "name": "crypto-cli", "version": "1.0.0", "description": "Cryptocurrency CLI price tool", - "main": "index.ts", - "repository": "https://github.com/zidious/crypto-cli", + "main": "dist/index.js", "author": "Gabe Olesen", "license": "MIT", + "repository": { + "type": "git", + "url": "https://github.com/zidious/crypto-cli" + }, + "bin": { + "crypto": "./dist/index.js" + }, "scripts": { + "prebuild": "rimraf dist", "build": "yarn tsc" }, "engines": { @@ -18,6 +25,7 @@ "chalk": "4.1.2", "meow": "9", "prettier": "^2.7.1", + "rimraf": "^3.0.2", "ts-node": "^10.8.2", "typescript": "^4.7.4" } diff --git a/src/app.ts b/src/app.ts index 1c77f33..d0e6db5 100644 --- a/src/app.ts +++ b/src/app.ts @@ -3,6 +3,12 @@ import { logError, logSuccess, format } from './utils'; interface Flags { price: string[]; + priceChange: boolean; + volume: boolean; + high: boolean; + low: boolean; + ath: boolean; + athChange: boolean; } interface CoinMarketResponse { @@ -34,33 +40,85 @@ interface CoinMarketResponse { last_updated: string; } -export const app = async (coffee: string, flags: Record) => { - const coffeeParsed = coffee.toLowerCase(); +export const app = async (crypto: string, flags: Record) => { + const cryptoParsed = crypto.toLowerCase(); + if (cryptoParsed === 'crypto') { + const gecko = new CoinGeckoAPI(); - if (coffeeParsed !== 'coffee') { - logError(`Unknown command: ${coffee}`); - } + const { price, priceChange, volume, high, low, ath, athChange } = + flags as unknown as Flags; - const gecko = new CoinGeckoAPI(); + if (price.length) { + try { + const result: CoinMarketResponse[] = await gecko.coinMarkets({ + vs_currency: 'usd', + ids: price.toString() + }); - const { price } = flags as unknown as Flags; + if (!result.length) { + logError(`Unknown coin: ${price.toString()}`); + } - if (price.length) { - try { - const result: CoinMarketResponse[] = await gecko.coinMarkets({ - vs_currency: 'usd', - ids: price.toString() - }); + for (const { + name, + current_price, + total_volume, + high_24h, + low_24h, + price_change_percentage_24h: percent24h, + ath: athPrice, + ath_change_percentage: athPercent + } of result) { + const priceRes = `${name}: ${format(current_price)}`; - if (!result.length) { - logError(`Unknown coin: ${price.toString()}`); - } + let priceChangeRes; + let volumeRes; + let highRes; + let lowRes; + let athRes; + let athChangeRes; + + if (priceChange) { + priceChangeRes = `change (24H): ${percent24h.toFixed(2)}%`; + } + + if (high) { + highRes = `high (24H): ${format(high_24h)}`; + } + + if (low) { + lowRes = `low (24H): ${format(low_24h)}`; + } + + if (volume) { + volumeRes = `24H volume: ${format(total_volume)}`; + } + + if (ath) { + athRes = `ATH: ${format(athPrice)}`; + } + + if (athChange) { + athChangeRes = `ATH (%): ${athPercent.toFixed(2)}%`; + } - for (const { name, current_price } of result) { - logSuccess(`${name}: ${format(current_price)}`); + logSuccess( + [ + priceRes, + priceChangeRes, + volumeRes, + highRes, + lowRes, + athRes, + athChangeRes + ] + .filter(Boolean) + .join(' - ') + ); + } + } catch (error) { + logError(`An error occured: ${(error as Error).message}`); } - } catch (error) { - logError(`An error occured: ${(error as Error).message}`); } } }; diff --git a/src/index.ts b/src/index.ts index 9594174..8796a21 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,16 +1,18 @@ +#!/usr/bin/env node + import meow from 'meow'; import { app } from './app'; const cli = meow( ` Usage: - $ coffee + $ crypto Options: --price, -p - Coin name Examples: - $coffee --price bitcoin + $crypto --price bitcoin >> bitoin: $1337 `, { @@ -19,6 +21,27 @@ const cli = meow( type: 'string', isMultiple: true, alias: 'p' + }, + priceChange: { + type: 'boolean', + alias: 'pc' + }, + volume: { + type: 'boolean', + alias: 'v' + }, + high: { + type: 'boolean' + }, + low: { + type: 'boolean' + }, + ath: { + type: 'boolean' + }, + athChange: { + type: 'boolean', + alias: 'athc' } } }