Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
bthesorceror committed Feb 14, 2016
0 parents commit 4075367
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
71 changes: 71 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
'use strict';

const help = {
command: 'help',
hidden: true,
func: function(fredrick, args) {
fredrick.write('Help:');
fredrick.exit(0);
}
}

const list = {
command: 'list',
hidden: true,
func: function(fredrick, args) {
fredrick.write('List:');
fredrick.write();

fredrick.plugins.forEach((plugin) => {
if (plugin.hidden) return;

fredrick.write(plugin.command);
fredrick.write(` ${plugin.description}`);
});

fredrick.write();

fredrick.exit(0);
}
}

class Fredrick {
constructor(name, options) {
options = options || {};
this.name = name;
this.stdout = options.stdout || process.stdout;
this.exit = options.exit || process.exit.bind(process);
this.plugins = [];

this.addPlugin(help);
this.addPlugin(list);
}

write(str) {
if (!str) {
return this.stdout.write('\n');
}

this.stdout.write(`${str}\n`);
}

respond(args) {
let command = args[0];
let plugin = this.findPlugin(command);

if (!plugin) return;

args = args.slice(1);
plugin.func(this, args);
}

findPlugin(command) {
return this.plugins.find((plugin) => {
return plugin.command === command;
});
}

addPlugin(plugin) { this.plugins.push(plugin); }
}

module.exports = Fredrick;
16 changes: 16 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "fredrick",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "tape test/**/*_test.js"
},
"author": "Brandon Farmer <bthesorceror@gmail.com>",
"license": "ISC",
"devDependencies": {
"sinon": "^1.17.3",
"tape": "^4.4.0"
},
"dependencies": {}
}
82 changes: 82 additions & 0 deletions test/fredrick_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
var test = require('tape');
var Fredrick = require('../index');
var sinon = require('sinon');

test('Fredrick responds to help command', function(t) {

var fakeStdout = { write: sinon.spy() };
var fakeExit = sinon.spy();

var fredrick = new Fredrick('fredrick', {
stdout: fakeStdout, exit: fakeExit
});

var args = ['help'];

fredrick.respond(args);

t.plan(2);
t.ok(fakeStdout.write.calledWith('Help:\n'), 'writes header');

t.ok(fakeExit.calledWith(0), 'exits cleanly');

});

test('Fredrick responds to list command', function(t) {

var fakeStdout = { write: sinon.spy() };
var fakeExit = sinon.spy();

var fredrick = new Fredrick('fredrick', {
stdout: fakeStdout, exit: fakeExit
});

var args = ['list'];

function noop(){};

fredrick.addPlugin({
command: 'test1',
func: noop,
description: 'description 1'
});

fredrick.addPlugin({
command: 'test2',
func: noop,
description: 'description 2'
});

fredrick.respond(args);

t.plan(7);
t.ok(fakeStdout.write.calledWith('List:\n'), 'writes header');

t.ok(
fakeStdout.write.calledWith('test1\n'),
'writes test1 command'
);

t.ok(
fakeStdout.write.calledWith(' description 1\n'),
'writes test1 description'
);

t.ok(
fakeStdout.write.calledWith('test2\n'),
'writes test2 command'
);

t.ok(
fakeStdout.write.calledWith(' description 2\n'),
'writes test2 description'
);

t.notOk(
fakeStdout.write.calledWith('help\n'),
'does not write help command'
);

t.ok(fakeExit.calledWith(0), 'exits cleanly');

});

0 comments on commit 4075367

Please sign in to comment.