Skip to content

Commit

Permalink
Add rdme --version/--help commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Dom Harrington committed May 8, 2018
1 parent 0441255 commit 5c40ec6
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 11 deletions.
5 changes: 4 additions & 1 deletion cli.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const path = require('path');
const config = require('config');
const { version } = require('./package.json');

function load(command = 'help') {
const file = path.join(__dirname, 'lib', `${command}.js`);
Expand All @@ -14,8 +15,10 @@ function load(command = 'help') {
}

module.exports = function(cmd, args, opts = {}) {
if (opts.version && !cmd) return Promise.resolve(version);

try {
const command = load(cmd);
const command = load(opts.help ? 'help' : cmd);
return command.run({ args, opts });
} catch(e) {
return Promise.reject(e);
Expand Down
13 changes: 12 additions & 1 deletion rdme.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
#! /usr/bin/env node
require('colors');
const parseArgs = require('minimist')(process.argv.slice(2));

const parseArgs = require('minimist')(process.argv.slice(2), {
alias: {
// Allows --version, -v, -V
v: 'version',
V: 'version',

// // Allows --help, -h, -H
h: 'help',
H: 'help',
}
});

require('./cli')(parseArgs._[0], parseArgs._.slice(1), parseArgs)
.then(() => process.exit())
Expand Down
29 changes: 20 additions & 9 deletions test/cli.test.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,33 @@
const assert = require('assert');
const minimist = require('minimist');

const cli = require('../cli');
const { version } = require('../package.json');

describe('cli', () => {
let error;
beforeAll(() => {
error = { console };
});

afterAll(() => {
console.error = error;
});

it('command not found', done =>
cli('notARealCommand').catch(e => {
assert.equal(e.message.includes('Command not found'), true)
return done();
}));

describe('--version', () => {
it('should return version from package.json', () =>
cli('', [], minimist(['--version'])).then(v => assert.equal(v, version)));

// This is necessary because we use --version for other commands like `docs`
it('should only return version if no command', () =>
cli('no-such-command', [], minimist(['--version'])).then(() => {
throw new Error('Should not get here');
}).catch(() => {
// This can be ignored as it's just going to be
// a command not found error
}));
});

describe('--help', () => {
it('should print help and not error', () =>
cli('', [], minimist(['--help']))
);
});
});

0 comments on commit 5c40ec6

Please sign in to comment.