diff --git a/README.md b/README.md index 3205255..bc877f3 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,7 @@ $ btc-value --help --quantity -q [number] Print the value of the given quantity --autorefresh -a [seconds] Automatic refresh printing every x seconds --percentage -p [h|d|w] Print the percentage change (h = hour, d = day, w = week) + --reset -r Reset the configuration to the default Examples $ btc-value @@ -79,6 +80,17 @@ Set a timeout that print the value every `seconds` seconds. The timeout restarts Print the percentage change the last hour, day and week. If the flag is set to `h` then percentage change last hour is printed. It is the same for `d` and days, and `w` and week. If the percentage is negative it is printed in bright red otherwise it is printed in green. +### `--reset`, `-r` +Reset the configuration to the default: +``` +default: { + code: "USD", + symbol: "$" +}, +quantity: 1, +autorefresh: 15 +``` + ## Related - [btc-value](https://github.com/Knutakir/btc-value) - API for this module diff --git a/cli.js b/cli.js index 9f5713d..0675b96 100755 --- a/cli.js +++ b/cli.js @@ -7,13 +7,31 @@ const chalk = require('chalk'); const Ora = require('ora'); const logSymbols = require('log-symbols'); const spinner = new Ora(); + +const defaultConfiguration = { + default: { + code: "USD", + symbol: "$" + }, + quantity: 1, + autorefresh: 15 +}; const configFile = __dirname + '/config.json'; -const config = require(configFile); +let config; + +try { + config = require(configFile); +} catch (e) { + // Set the config to the default if its not found in the file + config = defaultConfiguration; +} + let defaultCurrency = config.default; let quantity = config.quantity; let autorefresh = config.autorefresh; let autorefreshTimer; + const cli = meow(` Usage $ btc-value @@ -26,6 +44,7 @@ const cli = meow(` --quantity -q [number] Print the value of the given quantity --autorefresh -a [seconds] Automatic refresh printing every x seconds --percentage -p [h|d|w] Print the percentage change (h = hour, d = day, w = week) + --reset -r Reset the configuration to the default Examples $ btc-value @@ -70,6 +89,10 @@ const cli = meow(` percentage: { type: 'string', alias: 'p' + }, + reset: { + type: 'boolean', + alias: 'r' } } }); @@ -256,4 +279,19 @@ if (cli.flags.l) { process.exit(0); } +// If `r` falg is set => reset configuration file +if (cli.flags.r) { + const newConfig = JSON.stringify(defaultConfiguration, null, 4); + + fs.writeFile(configFile, newConfig, function(error) { + if (error) { + exitError('Something wrong happened, could not save new default currency.'); + } else { + console.log(chalk.green(`${logSymbols.success} Default configuration reset to: ${defaultConfiguration.default.code} (${defaultConfiguration.default.symbol})`)); + } + + process.exit(0); + }); +} + checkAllFlags(); \ No newline at end of file