├── .babelrc ├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── examples ├── .testrc └── shell.js ├── package.json ├── src └── rc.js └── test └── test.js /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": [ "es2015" ] 3 | } 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | coverage 3 | dist 4 | *.log 5 | *.swp 6 | *~ 7 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | language: node_js 3 | cache: 4 | directories: 5 | - node_modules 6 | notifications: 7 | email: false 8 | node_js: 9 | - '5' 10 | before_install: 11 | - npm i -g npm@^3.8.5 12 | before_script: 13 | - npm prune 14 | script: 15 | - npm run test:single 16 | - npm run check-coverage 17 | - npm run build 18 | after_success: 19 | - npm run report-coverage 20 | - npm run semantic-release 21 | branches: 22 | only: 23 | - master 24 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 subk 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | vorpal-rc 2 | ========= 3 | 4 | [![Travis](https://img.shields.io/travis/subk/vorpal-rc.svg)](https://travis-ci.org/subk/vorpal-rc) 5 | [![Codecov](https://img.shields.io/codecov/c/github/subk/vorpal-rc.svg?maxAge=2592000)](https://codecov.io/github/subk/vorpal-rc) 6 | [![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/) 7 | 8 | .rc file support for [Vorpal.js](http://vorpal.js.org/) 9 | 10 | ## Installation 11 | ```bash 12 | $ npm i vorpal-rc 13 | ``` 14 | 15 | ## Usage 16 | ```javascript 17 | import Vorpal from 'vorpal'; 18 | import rc from 'vorpal-rc'; 19 | 20 | const vorpal = new Vorpal(); 21 | 22 | // first declare your commands 23 | vorpal 24 | .command('hello ') 25 | .action(function (args, callback) { 26 | this.log('Hi %s !', args['your-name']); 27 | callback(); 28 | }); 29 | 30 | // execute ~/.awesomerc then show command line 31 | vorpal 32 | .use(rc, path.join(os.homedir(), '.awesomerc')) 33 | .delimiter('example>') 34 | .show(); 35 | ``` 36 | 37 | ## Example 38 | 39 | Quick example : 40 | ```bash 41 | $ npm i 42 | $ cat ~/.awesomerc 43 | hello Vorpal 44 | exit 45 | $ npm run example ~/.awesomerc 46 | Hi Vorpal ! 47 | $ 48 | ``` 49 | 50 | ## License 51 | MIT 52 | -------------------------------------------------------------------------------- /examples/.testrc: -------------------------------------------------------------------------------- 1 | hello Billy 2 | Im sorry I dont exist 3 | -------------------------------------------------------------------------------- /examples/shell.js: -------------------------------------------------------------------------------- 1 | import vorpal from 'vorpal'; 2 | import rc from '../src/rc'; 3 | 4 | const shell = new vorpal(); 5 | const rcfile = process.argv[2]; 6 | 7 | // declare your commands first 8 | shell 9 | .command('hello ') 10 | .action(function (args, callback) { 11 | this.log('Hi %s !', args['your-name']); 12 | callback(); 13 | }); 14 | 15 | // execute .rcfile 16 | if (rcfile) { 17 | shell.use(rc, rcfile) 18 | } 19 | 20 | // show cli 21 | shell 22 | .delimiter('shell>') 23 | .show(); 24 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "vorpal-rc", 3 | "description": ".rc file support for Vorpal.js", 4 | "main": "./dist/rc.js", 5 | "scripts": { 6 | "prebuild": "rimraf dist", 7 | "build": "babel src -d dist", 8 | "example": "babel-node examples/shell.js", 9 | "check-coverage": "istanbul check-coverage --statements 100 --functions 100 --lines 100", 10 | "report-coverage": "cat ./coverage/lcov.info | codecov", 11 | "test": "mocha -w --compilers js:babel-register test/test.js", 12 | "test:single": "istanbul cover _mocha -- -R spec --report lcovonly --compilers js:babel-register test/test.js", 13 | "semantic-release": "semantic-release pre && npm publish && semantic-release post", 14 | "commit": "git-cz" 15 | }, 16 | "repository": { 17 | "type": "git", 18 | "url": "https://github.com/subk/vorpal-rc.git" 19 | }, 20 | "keywords": [ 21 | "vorpal", 22 | "rc" 23 | ], 24 | "author": "kbus", 25 | "license": "MIT", 26 | "bugs": { 27 | "url": "https://github.com/subk/vorpal-rc/issues" 28 | }, 29 | "files": [ 30 | "dist", 31 | "README.md" 32 | ], 33 | "homepage": "https://github.com/subk/vorpal-rc#readme", 34 | "devDependencies": { 35 | "babel-cli": "^6.6.5", 36 | "babel-preset-es2015": "^6.6.0", 37 | "chai": "^3.5.0", 38 | "codecov.io": "^0.1.6", 39 | "commitizen": "^2.7.6", 40 | "cz-conventional-changelog": "^1.1.5", 41 | "istanbul": "^1.0.0-alpha.2", 42 | "mocha": "^2.4.5", 43 | "rimraf": "^2.5.2", 44 | "semantic-release": "^4.3.5", 45 | "test-console": "^1.0.0", 46 | "vorpal": "^1.10.10" 47 | }, 48 | "dependencies": { 49 | "trim": "0.0.1" 50 | }, 51 | "config": { 52 | "commitizen": { 53 | "path": "node_modules/cz-conventional-changelog" 54 | }, 55 | "ghooks": { 56 | "pre-commit": "npm run test:single && npm run check-coverage" 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/rc.js: -------------------------------------------------------------------------------- 1 | import { readFileSync } from 'fs'; 2 | import { EOL } from 'os'; 3 | import trim from 'trim'; 4 | 5 | export default function (vorpal, rcfile) { 6 | if (typeof rcfile !== 'string') { 7 | throw new Error('Missing .rc filename'); 8 | } 9 | 10 | try { 11 | readFileSync(rcfile, 'utf-8') 12 | .split(EOL) 13 | .filter(cmd => trim(cmd).length) 14 | .forEach(cmd => vorpal.exec(cmd)); 15 | } 16 | catch (err) { 17 | console.warn(err.message); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | import { expect } from 'chai'; 2 | import rc from '../src/rc'; 3 | import { join } from 'path'; 4 | import Vorpal from 'vorpal'; 5 | import { stdout, stderr } from 'test-console'; 6 | 7 | function getInstance() { 8 | const vorpal = new Vorpal(); 9 | vorpal 10 | .command('hello ') 11 | .action(function (args, callback) { 12 | this.log('Hi %s !', args['your-name']); 13 | callback(); 14 | }); 15 | return vorpal; 16 | } 17 | 18 | describe('vorpal-rc', function() { 19 | this.timeout(10000); 20 | 21 | describe('instantiate', () => { 22 | it('should be properly imported as a function', () => { 23 | expect(rc).to.be.instanceof(Function); 24 | }); 25 | 26 | it('should throw an error if no .rc file is provided', () => { 27 | expect(rc).to.throw('Missing .rc filename'); 28 | }); 29 | 30 | it('should be imported by Vorpal', () => { 31 | // just mute stderr 32 | const output = stderr.inspectSync(() => { 33 | expect(() => { 34 | getInstance().use(rc, '.rcfile'); 35 | }).to.not.throw(); 36 | }); 37 | }); 38 | }); 39 | 40 | describe('loading', () => { 41 | it('should warn user about .foorc not found', () => { 42 | const output = stderr.inspectSync(() => { 43 | getInstance().use(rc, '.foorc'); 44 | }); 45 | expect(output).to.match(/ENOENT/); 46 | }); 47 | }); 48 | 49 | describe('execution', () => { 50 | it('should execute commands from .testrc', () => { 51 | const output = stdout.inspectSync(() => { 52 | getInstance().use(rc, __dirname + '/../examples/.testrc'); 53 | }); 54 | const hello = 'Hi Billy !'; 55 | const help = 'help [command...] Provides help for a given command.'; 56 | // hello Billy 57 | expect(output[0]).to.have.string(hello); 58 | // Im sorry I dont exist 59 | expect(output[1]).to.have.string(help); 60 | }); 61 | }); 62 | }); 63 | --------------------------------------------------------------------------------