Skip to content

Commit

Permalink
Rename a few things
Browse files Browse the repository at this point in the history
  • Loading branch information
Dom Harrington committed May 1, 2018
1 parent 810c7ee commit d6e0cec
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 47 deletions.
34 changes: 13 additions & 21 deletions api.js → cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,25 @@ const path = require('path');

const utils = require('./utils');

exports.api = function(args, opts = {}) {
const action = args[0];
const config = utils.config(opts.env);

const actionObj = exports.load(config, action);

if (!actionObj) {
return;
}

const info = {
args,
opts,
};

actionObj.run(config, info);
};

exports.load = function(config, action = 'help') {
const file = path.join(__dirname, 'lib', `${action}.js`);
function load(config, command = 'help') {
const file = path.join(__dirname, 'lib', `${command}.js`);
try {
// eslint-disable-next-line global-require, import/no-dynamic-require
return require(file);
} catch (e) {
console.error('Action not found.'.red);
console.error('Command not found.'.red);
console.warn(`Type ${`${config.cli} help`.yellow} to see all commands`);
process.exitCode = 1;
return undefined;
}
};

module.exports = function(args, opts = {}) {
const config = utils.config(opts.env);

const command = load(config, args[0]);

if (!command) return;

command.run(config, { args, opts });
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"name": "rdme",
"description": "ReadMe's API CLI",
"bin": {
"rdme": "index.js"
"rdme": "rdme.js"
},
"tags": [
"api",
Expand Down
2 changes: 1 addition & 1 deletion index.js → rdme.js
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#! /usr/bin/env node
const parseArgs = require('minimist')(process.argv.slice(2));

require('./api').api(parseArgs._, parseArgs);
require('./cli')(parseArgs._, parseArgs);
24 changes: 0 additions & 24 deletions test/api.test.js

This file was deleted.

22 changes: 22 additions & 0 deletions test/cli.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const assert = require('assert');

const cli = require('../cli');

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

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

it('command not found', done => {
console.error = message => {
assert(message.includes('Command not found'));
return done();
};
cli('notARealCommand');
});
});

0 comments on commit d6e0cec

Please sign in to comment.