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
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
dist
node_modules
yarn-error.log
package-lock.json

.vscode
9 changes: 9 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"semi": true,
"singleQuote": true,
"printWidth": 80,
"bracketSpacing": true,
"useTabs": false,
"trailingComma": "none",
"arrowParens": "avoid"
}
24 changes: 24 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "crypto-cli",
"version": "1.0.0",
"description": "Cryptocurrency CLI price tool",
"main": "index.ts",
"repository": "https://github.com/zidious/crypto-cli",
"author": "Gabe Olesen",
"license": "MIT",
"scripts": {
"build": "yarn tsc"
},
"engines": {
"node": "16"
},
"devDependencies": {
"@crypto-coffee/coingecko-api": "^1.1.0",
"@types/node": "^18.0.3",
"chalk": "4.1.2",
"meow": "9",
"prettier": "^2.7.1",
"ts-node": "^10.8.2",
"typescript": "^4.7.4"
}
}
62 changes: 62 additions & 0 deletions src/actions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import CoinGeckoAPI from '@crypto-coffee/coingecko-api';
import { logError, logSuccess, format } from './utils';

interface Flags {
price: string[];
}

interface CoinMarketResponse {
id: string;
symbol: string;
name: string;
image: string;
current_price: number;
market_cap: number;
market_cap_rank: number;
fully_diluted_valuation: number;
total_volume: number;
high_24h: number;
low_24h: number;
price_change_24h: number;
price_change_percentage_24h: number;
market_cap_change_24h: number;
market_cap_change_percentage_24h: number;
circulating_supply: number;
total_supply: number;
max_supply: number;
ath: number;
ath_change_percentage: number;
ath_date: string;
atl: number;
atl_change_percentage: number;
atl_date: string;
roi: unknown;
last_updated: string;
}

export const app = async (coffee: string, flags: Record<string, unknown>) => {
const coffeeParsed = coffee.toLowerCase();

if (coffeeParsed !== 'coffee') {
logError(`Unknown command: ${coffee}`);
}

const gecko = new CoinGeckoAPI();

const { price } = flags as unknown as Flags;

if (price.length) {
try {
const result: CoinMarketResponse[] = await gecko.coinMarkets({
vs_currency: 'usd',
ids: price.toString()
});

for (const { name, current_price } of result) {
logSuccess(`${name}: ${format(current_price)}`);
}
} catch (error) {
logError(`An error occured: ${(error as Error).message}`);
}
}
};
27 changes: 27 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import meow from 'meow';
import { app } from './actions';

const cli = meow(
`
Usage:
$ coffee

Options:
--price, -p - Coin name

Examples:
$coffee --price bitcoin
>> bitoin: $1337
`,
{
flags: {
price: {
type: 'string',
isMultiple: true,
alias: 'p'
}
}
}
);

app(cli.input[0], cli.flags);
21 changes: 21 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import chalk from 'chalk';

const ERROR = chalk.bold.red;
const SUCCESS = chalk.bold.green;

const formatter = new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD'
});

export const logError = (message: string) => {
console.log(ERROR(message));
};

export const logSuccess = (message: string) => {
console.log(SUCCESS(message));
};

export const format = (price: number) => {
return formatter.format(price);
};
16 changes: 16 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"compilerOptions": {
"target": "ES2020",
"module": "commonjs",
"moduleResolution": "node",
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"resolveJsonModule": true,
"skipLibCheck": true,
"strict": true,
"outDir": "dist",
"typeRoots": ["node_modules/@types"]
},
"include": ["src"],
"exclude": ["node_modules"]
}
Loading