Skip to content

Commit

Permalink
Add reset option for resetting to default configuration ⏪
Browse files Browse the repository at this point in the history
  • Loading branch information
knutkirkhorn committed Jun 16, 2019
1 parent 02c0e20 commit ab1cfe7
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
40 changes: 39 additions & 1 deletion cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -70,6 +89,10 @@ const cli = meow(`
percentage: {
type: 'string',
alias: 'p'
},
reset: {
type: 'boolean',
alias: 'r'
}
}
});
Expand Down Expand Up @@ -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();

0 comments on commit ab1cfe7

Please sign in to comment.