Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -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"
}
Expand Down
98 changes: 78 additions & 20 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -34,33 +40,85 @@ interface CoinMarketResponse {
last_updated: string;
}

export const app = async (coffee: string, flags: Record<string, unknown>) => {
const coffeeParsed = coffee.toLowerCase();
export const app = async (crypto: string, flags: Record<string, unknown>) => {
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}`);
}
}
};
27 changes: 25 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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
`,
{
Expand All @@ -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'
}
}
}
Expand Down