Skip to content

Commit

Permalink
chore: re-format all files via Prettier (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
Zidious authored Nov 29, 2023
1 parent 9bc5088 commit 8aae650
Show file tree
Hide file tree
Showing 8 changed files with 203 additions and 202 deletions.
9 changes: 5 additions & 4 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{
"semi": true,
"singleQuote": true,
"printWidth": 80,
"arrowParens": "avoid",
"bracketSpacing": true,
"printWidth": 80,
"semi": false,
"tabWidth": 2,
"useTabs": false,
"trailingComma": "none",
"arrowParens": "avoid"
"trailingComma": "none"
}
52 changes: 26 additions & 26 deletions src/actions/priceStats.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { format, logSuccess } from '../utils.js';
import type { Flags } from '../constants.js';
import { format, logSuccess } from '../utils.js'
import type { Flags } from '../constants.js'

interface PriceStatsParams {
name: string;
current_price: number;
total_volume: number;
high_24h: number;
low_24h: number;
percent24h: number;
athPrice: number;
athPercent: number;
name: string
current_price: number
total_volume: number
high_24h: number
low_24h: number
percent24h: number
athPrice: number
athPercent: number
}

type PriceStatFlags = Omit<Flags, 'save'>;
type PriceStatFlags = Omit<Flags, 'save'>

export const priceStats = (
{
Expand All @@ -27,41 +27,41 @@ export const priceStats = (
}: PriceStatsParams,
{ price, priceChange, high, low, volume, ath, athChange }: PriceStatFlags
): void => {
const priceRes = `${name}: ${format(current_price)}`;
let priceChangeRes;
let volumeRes;
let highRes;
let lowRes;
let athRes;
let athChangeRes;
const priceRes = `${name}: ${format(current_price)}`
let priceChangeRes
let volumeRes
let highRes
let lowRes
let athRes
let athChangeRes

if (priceChange) {
priceChangeRes = `change (24H): ${percent24h.toFixed(2)}%`;
priceChangeRes = `change (24H): ${percent24h.toFixed(2)}%`
}

if (high) {
highRes = `high (24H): ${format(high_24h)}`;
highRes = `high (24H): ${format(high_24h)}`
}

if (low) {
lowRes = `low (24H): ${format(low_24h)}`;
lowRes = `low (24H): ${format(low_24h)}`
}

if (volume) {
volumeRes = `volume (24H): ${format(total_volume)}`;
volumeRes = `volume (24H): ${format(total_volume)}`
}

if (ath) {
athRes = `ATH: ${format(athPrice)}`;
athRes = `ATH: ${format(athPrice)}`
}

if (athChange) {
athChangeRes = `ATH (%): ${athPercent.toFixed(2)}%`;
athChangeRes = `ATH (%): ${athPercent.toFixed(2)}%`
}

logSuccess(
[priceRes, priceChangeRes, volumeRes, highRes, lowRes, athRes, athChangeRes]
.filter(Boolean)
.join(' - ')
);
};
)
}
42 changes: 21 additions & 21 deletions src/actions/saveCoinData.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import fs from 'fs';
import { parseAsync } from 'json2csv';
import { logError, logSuccess } from '../utils.js';
import { CSVEXT, JSONEXT } from '../constants.js';
import type { ExportData } from '../constants.js';
import fs from 'fs'
import { parseAsync } from 'json2csv'
import { logError, logSuccess } from '../utils.js'
import { CSVEXT, JSONEXT } from '../constants.js'
import type { ExportData } from '../constants.js'

export const saveCoinData = async (
options: string,
exportData: ExportData[]
) => {
const fileExts = options.toLowerCase().split(',');
const fileExts = options.toLowerCase().split(',')

if (
!fileExts.some(
Expand All @@ -18,45 +18,45 @@ export const saveCoinData = async (
) {
logError(
'Unable to export, unsupported file extension.\nPlease Check `crypto --help` for help'
);
)
}

logSuccess('Exporting coin data...');
logSuccess('Exporting coin data...')
if (fileExts.includes(JSONEXT)) {
writeFile(exportData, JSONEXT);
writeFile(exportData, JSONEXT)
}

if (fileExts.includes(CSVEXT)) {
await writeFile(exportData, CSVEXT);
await writeFile(exportData, CSVEXT)
}

logSuccess('Export complete.');
};
logSuccess('Export complete.')
}

const writeFile = async (exportData: ExportData[], fileExt: string) => {
for (const coin of exportData) {
const data =
fileExt === JSONEXT ? JSON.stringify(coin) : await formatCsvFile(coin);
fileExt === JSONEXT ? JSON.stringify(coin) : await formatCsvFile(coin)
try {
fs.writeFileSync(formatFileName(coin.name as string, fileExt), data, {
encoding: 'utf8'
});
})
} catch (error) {
logError(
`An error occured when attempting to save coin data: \n ${
(error as Error).message
}`
);
)
}
}
};
}

const formatFileName = (coinName: string, fileExt: string): string => {
/* use unix timestamp, resolves conflict of same filenames */
const timestamp = new Date().valueOf();
const timestamp = new Date().valueOf()

return `${coinName.toLowerCase()}-${timestamp}.${fileExt}`;
};
return `${coinName.toLowerCase()}-${timestamp}.${fileExt}`
}

const formatCsvFile = async (coin: ExportData): Promise<string> => {
return await parseAsync(coin as Readonly<ExportData>, {
Expand Down Expand Up @@ -96,5 +96,5 @@ const formatCsvFile = async (coin: ExportData): Promise<string> => {
value: 'ath_change_percentage'
}
]
});
};
})
}
34 changes: 17 additions & 17 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
import CoinGeckoAPI from '@crypto-coffee/coingecko-api';
import { saveCoinData } from './actions/saveCoinData.js';
import { logError } from './utils.js';
import { priceStats } from './actions/priceStats.js';
import type { ExportData, Flags } from './constants.js';
import CoinGeckoAPI from '@crypto-coffee/coingecko-api'
import { saveCoinData } from './actions/saveCoinData.js'
import { logError } from './utils.js'
import { priceStats } from './actions/priceStats.js'
import type { ExportData, Flags } from './constants.js'

export const app = async (action: string, flags: Record<string, unknown>) => {
const { price, priceChange, volume, high, low, ath, athChange, save } =
flags as unknown as Flags;
flags as unknown as Flags

if (!price.length) {
logError('No coin name provided. Check `crypto --help` for help');
logError('No coin name provided. Check `crypto --help` for help')
}

const gecko = new CoinGeckoAPI.default();
const gecko = new CoinGeckoAPI.default()
try {
const result = await gecko.coinMarkets({
vs_currency: 'usd',
ids: price.toString()
});
})

if (!result.length) {
logError(`Unknown coin: ${price.toString()}`);
logError(`Unknown coin: ${price.toString()}`)
}

const exportCoinData: ExportData[] = [];
const exportCoinData: ExportData[] = []
for (const {
name,
current_price,
Expand All @@ -43,7 +43,7 @@ export const app = async (action: string, flags: Record<string, unknown>) => {
price_change_percentage_24h: percent24h,
all_time_high: athPrice,
ath_change_percentage: athPercent
});
})

priceStats(
{
Expand All @@ -57,19 +57,19 @@ export const app = async (action: string, flags: Record<string, unknown>) => {
athPercent
},
{ price, priceChange, high, low, volume, ath, athChange }
);
)
}

if (save) {
await saveCoinData(save, exportCoinData);
await saveCoinData(save, exportCoinData)
}

process.exit(0);
process.exit(0)
} catch (error) {
logError(
`An error occured: ${
(error as Error).message
}\n Please report the issue here: https://github.com/Zidious/crypto-cli`
);
)
}
};
}
22 changes: 11 additions & 11 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
/** contstants */
export const JSONEXT = 'json';
export const CSVEXT = 'csv';
export const JSONEXT = 'json'
export const CSVEXT = 'csv'

/** types */
export type ExportData = Record<string, string | number>;
export type ExportData = Record<string, string | number>

export interface Flags {
price: string[];
priceChange: boolean;
volume: boolean;
high: boolean;
low: boolean;
ath: boolean;
athChange: boolean;
save: string;
price: string[]
priceChange: boolean
volume: boolean
high: boolean
low: boolean
ath: boolean
athChange: boolean
save: string
}
Loading

0 comments on commit 8aae650

Please sign in to comment.