├── .babelrc ├── img └── example.gif ├── .editorconfig ├── .eslintrc.json ├── CONTRIBUTING.md ├── bin ├── main.js └── ConvertBTC.js ├── src ├── main.js └── ConvertBTC.js ├── .gitignore ├── LICENSE.md ├── README.md ├── tests ├── main.spec.js └── ConvertBTC.spec.js └── package.json /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | presets: ['env'] 3 | } 4 | -------------------------------------------------------------------------------- /img/example.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/willianjusten/btc-converter/HEAD/img/example.gif -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = space 5 | indent_size = 2 6 | end_of_line = lf 7 | charset = utf-8 8 | trim_trailing_whitespace = true 9 | insert_final_newline = true 10 | -------------------------------------------------------------------------------- /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "airbnb-base", 3 | "plugins": [ 4 | "import" 5 | ], 6 | "rules": { 7 | "no-console": 0, 8 | "consistent-return": 0 9 | }, 10 | "env": { 11 | "mocha": true 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | 1. Fork it! 4 | 2. See the commands on [package.json](package.json) to build, test and run. 5 | 3. Create your feature branch: `git checkout -b my-new-feature` 6 | 4. Commit your changes: `git commit -m 'Add some feature'` 7 | 5. Push to the branch: `git push origin my-new-feature` 8 | 9 | *Remember that we have a pre-push hook with steps that analyzes and prevents mistakes.* 10 | 11 | **After your pull request is merged**, you can safely delete your branch. 12 | -------------------------------------------------------------------------------- /bin/main.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 'use strict'; 3 | 4 | var program = require('commander'); 5 | var pkg = require('../package.json'); 6 | var convertBTC = require('./ConvertBTC'); 7 | 8 | program.version(pkg.version).description('Convert Bitcoin to any currency defined.').option('-C, --currency ', 'Currency to be converted. (Default: USD)').option('-A, --amount ', 'Value in Bitcoin to convert. (Default: 1)').parse(process.argv); 9 | 10 | convertBTC(program.currency, program.amount); -------------------------------------------------------------------------------- /src/main.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | const program = require('commander'); 4 | const pkg = require('../package.json'); 5 | const convertBTC = require('./ConvertBTC'); 6 | 7 | program 8 | .version(pkg.version) 9 | .description('Convert Bitcoin to any currency defined.') 10 | .option('-C, --currency ', 'Currency to be converted. (Default: USD)') 11 | .option('-A, --amount ', 'Value in Bitcoin to convert. (Default: 1)') 12 | .parse(process.argv); 13 | 14 | convertBTC(program.currency, program.amount); 15 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | 6 | # Runtime data 7 | pids 8 | *.pid 9 | *.seed 10 | *.pid.lock 11 | 12 | # Directory for instrumented libs generated by jscoverage/JSCover 13 | lib-cov 14 | 15 | # Coverage directory used by tools like istanbul 16 | coverage 17 | 18 | # nyc test coverage 19 | .nyc_output 20 | 21 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 22 | .grunt 23 | 24 | # node-waf configuration 25 | .lock-wscript 26 | 27 | # Compiled binary addons (http://nodejs.org/api/addons.html) 28 | build/Release 29 | 30 | # Dependency directories 31 | node_modules 32 | jspm_packages 33 | 34 | # Optional npm cache directory 35 | .npm 36 | 37 | # Optional eslint cache 38 | .eslintcache 39 | 40 | # Optional REPL history 41 | .node_repl_history 42 | 43 | # Output of 'npm pack' 44 | *.tgz 45 | -------------------------------------------------------------------------------- /src/ConvertBTC.js: -------------------------------------------------------------------------------- 1 | const chalk = require('chalk'); 2 | const request = require('request-promise-native'); 3 | const ora = require('ora'); 4 | 5 | const spinner = ora({ 6 | text: 'Retrieving Bitcoin data...', 7 | color: 'yellow', 8 | }); 9 | 10 | function convertBTC(currency = 'USD', amount = 1) { 11 | const url = `https://apiv2.bitcoinaverage.com/convert/global?from=BTC&to=${currency}&amount=${amount}`; 12 | 13 | spinner.start(); 14 | 15 | return request(url) 16 | .then((body) => { 17 | spinner.stop(); 18 | return body; 19 | }) 20 | .then((body) => { 21 | const apiResponse = JSON.parse(body); 22 | console.info(`${chalk.red(amount)} BTC to ${chalk.cyan(currency)} = ${chalk.yellow(apiResponse.price)}`); 23 | }) 24 | .catch((err) => { 25 | spinner.stop(); 26 | console.info(chalk.red('Something went wrong in the API. Try in a few minutes.')); 27 | return err; 28 | }); 29 | } 30 | 31 | module.exports = convertBTC; 32 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | Copyright (c) 2017 - Willian Justen 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 5 | 6 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 7 | 8 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 9 | -------------------------------------------------------------------------------- /bin/ConvertBTC.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var chalk = require('chalk'); 4 | var request = require('request-promise-native'); 5 | var ora = require('ora'); 6 | 7 | var spinner = ora({ 8 | text: 'Retrieving Bitcoin data...', 9 | color: 'yellow' 10 | }); 11 | 12 | function convertBTC() { 13 | var currency = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'USD'; 14 | var amount = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1; 15 | 16 | var url = 'https://apiv2.bitcoinaverage.com/convert/global?from=BTC&to=' + currency + '&amount=' + amount; 17 | 18 | spinner.start(); 19 | 20 | return request(url).then(function (body) { 21 | spinner.stop(); 22 | return body; 23 | }).then(function (body) { 24 | var apiResponse = JSON.parse(body); 25 | console.info(chalk.red(amount) + ' BTC to ' + chalk.cyan(currency) + ' = ' + chalk.yellow(apiResponse.price)); 26 | }).catch(function (err) { 27 | spinner.stop(); 28 | console.info(chalk.red('Something went wrong in the API. Try in a few minutes.')); 29 | return err; 30 | }); 31 | } 32 | 33 | module.exports = convertBTC; -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Bitcoin Converter CLI 2 | 3 | > A CLI to convert Bitcoin to any currency provided. 4 | 5 | ![Example CLI running](img/example.gif) 6 | 7 | Want to learn how to create your own CLI? [Check this course about JS with TDD](https://www.udemy.com/js-com-tdd-na-pratica/?couponCode=GITHUB_LINK) 8 | 9 | ### Installing 10 | 11 | ``` 12 | $ npm install -g btc-converter 13 | ``` 14 | 15 | ### How to use 16 | 17 | ```sh 18 | btc-converter --help 19 | 20 | Usage: btc-converter [options] 21 | 22 | Convert Bitcoin to any currency defined 23 | 24 | Options: 25 | 26 | -h, --help output usage information 27 | -V, --version output the version number 28 | -C, --currency Currency to be converted. (Default: USD) 29 | -A, --amount Value in Bitcoin to convert. (Default: 1) 30 | ``` 31 | 32 | ## Contributing 33 | 34 | Please read [CONTRIBUTING.md](CONTRIBUTING.md) for details on our code of conduct, and the process for submitting pull requests to us. 35 | 36 | ## License 37 | 38 | This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details 39 | -------------------------------------------------------------------------------- /tests/main.spec.js: -------------------------------------------------------------------------------- 1 | const expect = require('chai').expect; 2 | 3 | const exec = require('child_process').exec; 4 | const btcConverter = './src/main.js'; 5 | const pkg = require('../package.json'); 6 | 7 | describe('Main CLI', () => { 8 | it('should return version of btc-converter', (done) => { 9 | exec(`${btcConverter} --version`, (err, stdout, stderr) => { 10 | if(err) throw err; 11 | expect(stdout.replace('\n', '')).to.be.equal(pkg.version); 12 | done(); 13 | }); 14 | }); 15 | 16 | it('should return the description when btc-converter --help', (done) => { 17 | exec(`${btcConverter} --help`, (err, stdout, stderr) => { 18 | if(err) throw err; 19 | expect(stdout.includes('Convert Bitcoin to any currency defined.')).to.be.true; 20 | done(); 21 | }); 22 | }); 23 | 24 | it('should return the currency option when btc-converter --help', (done) => { 25 | exec(`${btcConverter} --help`, (err, stdout, stderr) => { 26 | if(err) throw err; 27 | expect(stdout.includes('--currency')).to.be.true; 28 | done(); 29 | }); 30 | }); 31 | 32 | it('should return the amount option when btc-converter --help', (done) => { 33 | exec(`${btcConverter} --help`, (err, stdout, stderr) => { 34 | if(err) throw err; 35 | expect(stdout.includes('--amount')).to.be.true; 36 | done(); 37 | }); 38 | }); 39 | }); 40 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "btc-converter", 3 | "version": "1.1.0", 4 | "description": "A CLI to convert Bitcoin to any currency provided.", 5 | "main": "index.js", 6 | "scripts": { 7 | "clear": "rimraf bin", 8 | "build": "npm run clear && ./node_modules/.bin/babel --out-dir bin src", 9 | "build:watch": "npm run build -- --watch", 10 | "lint": "./node_modules/.bin/eslint src/*.js", 11 | "prepush": "npm run lint", 12 | "test": "./node_modules/.bin/mocha tests/**/*.spec.js --require babel-register --require babel-polyfill", 13 | "test:tdd": "npm run test -- --watch", 14 | "test:coverage": "nyc npm test" 15 | }, 16 | "preferGlobal": true, 17 | "bin": { 18 | "btc-converter": "bin/main.js" 19 | }, 20 | "nyc": { 21 | "reporter": [ 22 | "text", 23 | "html" 24 | ], 25 | "exclude": [ 26 | "tests/**" 27 | ] 28 | }, 29 | "repository": { 30 | "type": "git", 31 | "url": "git+https://github.com/willianjusten/btc-converter.git" 32 | }, 33 | "keywords": [ 34 | "js", 35 | "tdd", 36 | "library" 37 | ], 38 | "author": "Willian Justen (https://willianjusten.com.br/)", 39 | "license": "MIT", 40 | "bugs": { 41 | "url": "https://github.com/willianjusten/btc-converter/issues" 42 | }, 43 | "homepage": "https://github.com/willianjusten/btc-converter#readme", 44 | "devDependencies": { 45 | "babel-cli": "^6.24.1", 46 | "babel-polyfill": "^6.26.0", 47 | "babel-preset-env": "^1.3.2", 48 | "babel-register": "^6.24.0", 49 | "chai": "^3.5.0", 50 | "eslint": "^4.0.0", 51 | "eslint-config-airbnb-base": "^11.2.0", 52 | "eslint-plugin-import": "^2.6.1", 53 | "husky": "^0.11.9", 54 | "mocha": "^3.2.0", 55 | "nock": "^9.0.13", 56 | "nyc": "^10.2.0", 57 | "rimraf": "^2.6.1", 58 | "sinon": "^2.3.6", 59 | "sinon-chai": "^2.11.0" 60 | }, 61 | "dependencies": { 62 | "chalk": "^2.0.1", 63 | "commander": "^2.10.0", 64 | "ora": "^1.3.0", 65 | "request": "^2.81.0", 66 | "request-promise-native": "^1.0.5" 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /tests/ConvertBTC.spec.js: -------------------------------------------------------------------------------- 1 | const nock = require('nock'); 2 | const chalk = require('chalk'); 3 | const chai = require('chai'); 4 | const sinon = require('sinon'); 5 | const sinonChai = require('sinon-chai'); 6 | 7 | const expect = chai.expect; 8 | 9 | chai.use(sinonChai); 10 | 11 | const convertBTC = require('../src/ConvertBTC'); 12 | 13 | describe('ConvertBTC', () => { 14 | let consoleStub; 15 | 16 | const responseMock = { 17 | success: true, 18 | time: '2017-07-02 18:51:29', 19 | price: 2490.78, 20 | }; 21 | 22 | beforeEach(() => { 23 | consoleStub = sinon.stub(console, 'info'); 24 | }); 25 | 26 | afterEach(() => { 27 | consoleStub.restore(); 28 | }); 29 | 30 | it('should use currency USD and 1 as amount default', async () => { 31 | nock('https://apiv2.bitcoinaverage.com') 32 | .get('/convert/global') 33 | .query({ from: 'BTC', to: 'USD', amount: 1 }) 34 | .reply(200, responseMock); 35 | 36 | await convertBTC(); 37 | expect(consoleStub).to.have.been.calledWith(`${chalk.red(1)} BTC to ${chalk.cyan('USD')} = ${chalk.yellow(2490.78)}`); 38 | }); 39 | 40 | it('should use currency USD and 10 as amount', async () => { 41 | nock('https://apiv2.bitcoinaverage.com') 42 | .get('/convert/global') 43 | .query({ from: 'BTC', to: 'USD', amount: 10 }) 44 | .reply(200, responseMock); 45 | 46 | await convertBTC('USD', 10); 47 | expect(consoleStub).to.have.been.calledWith(`${chalk.red(10)} BTC to ${chalk.cyan('USD')} = ${chalk.yellow(2490.78)}`); 48 | }); 49 | 50 | it('should use currency BRL and 1 as amount default', async () => { 51 | nock('https://apiv2.bitcoinaverage.com') 52 | .get('/convert/global') 53 | .query({ from: 'BTC', to: 'BRL', amount: 1 }) 54 | .reply(200, responseMock); 55 | 56 | await convertBTC('BRL'); 57 | expect(consoleStub).to.have.been.calledWith(`${chalk.red(1)} BTC to ${chalk.cyan('BRL')} = ${chalk.yellow(2490.78)}`); 58 | }); 59 | 60 | it('should message user when api reply with error', async () => { 61 | nock('https://apiv2.bitcoinaverage.com') 62 | .get('/convert/global') 63 | .query({ from: 'BTC', to: 'BRL', amount: 1 }) 64 | .replyWithError('Error'); 65 | 66 | await convertBTC('BRL'); 67 | expect(consoleStub).to.have.been.calledWith(chalk.red('Something went wrong in the API. Try in a few minutes.')); 68 | }); 69 | }); 70 | --------------------------------------------------------------------------------