-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 4075367
Showing
4 changed files
with
170 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
|
||
}); |