Comparing version 1.1.1 to 1.2.1
45
index.js
@@ -17,2 +17,3 @@ 'use strict'; | ||
this.extensions = []; | ||
this.properties = []; | ||
@@ -76,2 +77,12 @@ this.addPlugin(require('./lib/plugins/help')); | ||
checkForProperties(object, type) { | ||
if (!object.properties || !object.properties.length) return; | ||
object.properties.forEach((name) => { | ||
if (this.hasProperty(name)) return; | ||
throw new Error(`${type} requires property named ${name}`); | ||
}); | ||
} | ||
addPlugin(plugin) { | ||
@@ -85,2 +96,4 @@ if (!plugin.command) | ||
this.checkForExtensions(plugin, 'Plugin'); | ||
this.checkForProperties(plugin, 'Plugin'); | ||
this.plugins.push(plugin); | ||
@@ -90,5 +103,13 @@ } | ||
hasExtension(name) { | ||
return !!this[name]; | ||
return !!this.extensions.find((value) => { | ||
return name === value; | ||
}); | ||
} | ||
hasProperty(name) { | ||
return !!this.properties.find((value) => { | ||
return name === value; | ||
}); | ||
} | ||
addExtension(extension) { | ||
@@ -98,6 +119,28 @@ if (this.hasExtension(extension.name)) | ||
this.checkForExtensions(extension, 'Extension'); | ||
this.checkForProperties(extension, 'Extension'); | ||
this.extensions.push(extension.name); | ||
this[extension.name] = extension.func; | ||
} | ||
addProperty(property) { | ||
if (this.hasProperty(property.name)) | ||
throw new Error('Property name already taken'); | ||
if (this.hasExtension(property.name)) | ||
throw new Error('Property name already taken by an extension'); | ||
this.checkForExtensions(property, 'Property'); | ||
this.checkForProperties(property, 'Property'); | ||
this.properties.push(property.name); | ||
let getter = function() { return property.value }; | ||
if (property.value instanceof Function) { | ||
getter = property.value; | ||
} | ||
Object.defineProperty(this, property.name, { get: getter }); | ||
} | ||
} | ||
module.exports = Fredrick; |
{ | ||
"name": "fredrick", | ||
"version": "1.1.1", | ||
"version": "1.2.1", | ||
"description": "simple module for writing your spiffy command line tools", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -29,2 +29,9 @@ # Fredrick | ||
fredrick.addProperty({ | ||
name: 'value1', | ||
value: function() { | ||
return 10; | ||
} | ||
}) | ||
fredrick.addPlugin({ | ||
@@ -35,2 +42,3 @@ command: 'test', | ||
extensions: ['test2'], | ||
properties: ['value1'], | ||
func(fredrick, args, options) { | ||
@@ -47,2 +55,4 @@ if (options.prod) { | ||
fredrick.value1 === 10; | ||
fredrick.write('TESTING'); | ||
@@ -49,0 +59,0 @@ fredrick.exit(0); |
@@ -46,2 +46,101 @@ var test = require('tape'); | ||
test('Fredrick has the ability to add properties', function(t) { | ||
t.test('adds property with static value', function(t) { | ||
t.plan(1); | ||
var fredrick = new Fredrick('fredrick'); | ||
var property = { | ||
name: 'myNumber', | ||
value: 10 | ||
}; | ||
fredrick.addProperty(property); | ||
fredrick.addPlugin({ | ||
command: 'command', | ||
func: function(fredrick, args, options) { | ||
t.equal(fredrick.myNumber, 10, 'adds property'); | ||
} | ||
}); | ||
fredrick.respond(['command']); | ||
}); | ||
t.test('adds property with function', function(t) { | ||
t.plan(1); | ||
var fredrick = new Fredrick('fredrick'); | ||
var property = { | ||
name: 'myNumber', | ||
value: function() { return 10; } | ||
}; | ||
fredrick.addProperty(property); | ||
fredrick.addPlugin({ | ||
command: 'command', | ||
func: function(fredrick, args, options) { | ||
t.equal(fredrick.myNumber, 10, 'adds property'); | ||
} | ||
}); | ||
fredrick.respond(['command']); | ||
}); | ||
t.test('with property name taken', function(t) { | ||
t.plan(1); | ||
var fredrick = new Fredrick('fredrick'); | ||
function func() {} | ||
var property = { | ||
name: 'b', | ||
value: 10 | ||
}; | ||
fredrick.addProperty(property); | ||
try { | ||
fredrick.addProperty(property); | ||
} catch(ex) { | ||
t.ok(true, 'throws error'); | ||
} | ||
}); | ||
t.test('with property name taken by an extension', function(t) { | ||
t.plan(1); | ||
var fredrick = new Fredrick('fredrick'); | ||
function func() {} | ||
var extension = { | ||
name: 'b', | ||
func(){} | ||
}; | ||
var property = { | ||
name: 'b', | ||
value: 10 | ||
}; | ||
fredrick.addExtension(property); | ||
try { | ||
fredrick.addProperty(property); | ||
} catch(ex) { | ||
t.ok(true, 'throws error'); | ||
} | ||
}); | ||
}); | ||
test('Fredrick has the ability to be extended', function(t) { | ||
@@ -169,4 +268,105 @@ | ||
t.test('with extension requiring property', function(t) { | ||
t.plan(1); | ||
var fredrick = new Fredrick('fredrick'); | ||
var extension = { | ||
name: 'helper', | ||
properties: ['tester'], | ||
func: function(){} | ||
}; | ||
try { | ||
fredrick.addExtension(extension); | ||
t.ok(false, 'should not reach'); | ||
} catch(ex) { | ||
t.ok(true, 'throws error'); | ||
} | ||
}); | ||
}); | ||
test('Plugin requires properties', function(t) { | ||
t.test('property loaded', function(t) { | ||
t.plan(1); | ||
var fredrick = new Fredrick('fredrick'); | ||
var property = { | ||
name: 'helper', | ||
value: 10 | ||
}; | ||
fredrick.addProperty(property); | ||
fredrick.addPlugin({ | ||
command: 'command', | ||
properties: ['helper'], | ||
func: function(fredrick, args, options) { | ||
t.equal(fredrick.helper, 10, 'adds method'); | ||
} | ||
}); | ||
fredrick.respond(['command']); | ||
}); | ||
t.test('property not loaded', function(t) { | ||
t.plan(1); | ||
var fredrick = new Fredrick('fredrick'); | ||
try { | ||
fredrick.addPlugin({ | ||
command: 'command', | ||
properties: ['helper'], | ||
func: function(fredrick, args, options) {} | ||
}); | ||
t.ok(false, 'should not reach'); | ||
} catch(ex) { | ||
t.ok(true, 'throws error'); | ||
} | ||
}); | ||
t.test('with property requiring extension', function(t) { | ||
t.plan(1); | ||
var fredrick = new Fredrick('fredrick'); | ||
var property = { | ||
name: 'helper', | ||
extensions: ['tester'], | ||
value: 10 | ||
}; | ||
try { | ||
fredrick.addProperty(property); | ||
t.ok(false, 'should not reach'); | ||
} catch(ex) { | ||
t.ok(true, 'throws error'); | ||
} | ||
}); | ||
t.test('with property requiring properties', function(t) { | ||
t.plan(1); | ||
var fredrick = new Fredrick('fredrick'); | ||
var property = { | ||
name: 'helper', | ||
properties: ['tester'], | ||
value: 10 | ||
}; | ||
try { | ||
fredrick.addProperty(property); | ||
t.ok(false, 'should not reach'); | ||
} catch(ex) { | ||
t.ok(true, 'throws error'); | ||
} | ||
}); | ||
}) | ||
test('Fredrick allows errors', function(t) { | ||
@@ -180,5 +380,3 @@ t.plan(1); | ||
t.ok( | ||
fakeStderr.write.calledWith('testing\n'), | ||
'writes out to stderr'); | ||
t.ok(fakeStderr.write.calledWith('testing\n'), 'writes out to stderr'); | ||
}); | ||
@@ -188,38 +386,32 @@ | ||
t.plan(3); | ||
t.plan(3); | ||
var fakeStdout = { write: sinon.spy() }; | ||
var fakeExit = sinon.spy(); | ||
var fakeStdout = { write: sinon.spy() }; | ||
var fakeExit = sinon.spy(); | ||
var fredrick = new Fredrick('fredrick', { | ||
stdout: fakeStdout, exit: fakeExit | ||
}); | ||
var fredrick = new Fredrick('fredrick', { | ||
stdout: fakeStdout, exit: fakeExit | ||
}); | ||
function func(fredrick, args, options) { | ||
t.ok(true, 'calls func'); | ||
t.deepEqual( | ||
args, | ||
['list'], | ||
'does not removes the subcommand from args'); | ||
function func(fredrick, args, options) { | ||
t.ok(true, 'calls func'); | ||
t.equal( | ||
options.prod, | ||
true, | ||
'receives prod as option'); | ||
return; | ||
} | ||
t.deepEqual(args, ['list'], 'does not removes the subcommand from args'); | ||
fredrick.addPlugin({ | ||
command: 'test1', | ||
func: func, | ||
description: 'description 1', | ||
usage: 'usage 1', | ||
subcommands: { | ||
} | ||
}); | ||
t.equal(options.prod, true, 'receives prod as option'); | ||
return; | ||
} | ||
var args = ['test1', 'list', '--prod']; | ||
fredrick.addPlugin({ | ||
command: 'test1', | ||
func: func, | ||
description: 'description 1', | ||
usage: 'usage 1', | ||
subcommands: { } | ||
}); | ||
fredrick.respond(args); | ||
var args = ['test1', 'list', '--prod']; | ||
fredrick.respond(args); | ||
}); | ||
@@ -277,6 +469,3 @@ | ||
t.ok(true, 'calls func'); | ||
t.deepEqual( | ||
args, | ||
['list'], | ||
'does not removes the subcommand from args'); | ||
t.deepEqual(args, ['list'], 'does not removes the subcommand from args'); | ||
return; | ||
@@ -318,9 +507,5 @@ } | ||
t.ok( | ||
fakeStdout.write.calledWith("'fredrick list' lists all available commands.\n"), | ||
'writes help for list'); | ||
t.ok(fakeStdout.write.calledWith("'fredrick list' lists all available commands.\n"), 'writes help for list'); | ||
t.ok( | ||
fakeStdout.write.calledWith("'fredrick usage <command>' shows usage for a command.\n"), | ||
'writes help for usage'); | ||
t.ok(fakeStdout.write.calledWith("'fredrick usage <command>' shows usage for a command.\n"), 'writes help for usage'); | ||
@@ -368,5 +553,3 @@ t.ok(fakeExit.calledWith(0), 'exits cleanly'); | ||
t.ok( | ||
fakeStderr.write.calledWith("fredrick usage <command>\n"), | ||
'write correct use of usage command'); | ||
t.ok(fakeStderr.write.calledWith("fredrick usage <command>\n"), 'write correct use of usage command'); | ||
@@ -392,5 +575,3 @@ t.ok(fakeExit.calledWith(1), 'exits with error'); | ||
t.ok( | ||
fakeStderr.write.calledWith("Invalid command\n"), | ||
'write for invalid command'); | ||
t.ok(fakeStderr.write.calledWith("Invalid command\n"), 'write for invalid command'); | ||
@@ -425,9 +606,5 @@ t.ok(fakeExit.calledWith(1), 'exits with error'); | ||
t.ok( | ||
fakeStdout.write.calledWith("Usage:\n"), | ||
'write header'); | ||
t.ok(fakeStdout.write.calledWith("Usage:\n"), 'write header'); | ||
t.ok( | ||
fakeStdout.write.calledWith("usage 1\n"), | ||
'write command usage'); | ||
t.ok(fakeStdout.write.calledWith("usage 1\n"), 'write command usage'); | ||
@@ -461,9 +638,5 @@ t.ok(fakeExit.calledWith(0), 'exits cleanly'); | ||
t.ok( | ||
fakeStdout.write.calledWith("Usage:\n"), | ||
'write header'); | ||
t.ok(fakeStdout.write.calledWith("Usage:\n"), 'write header'); | ||
t.ok( | ||
fakeStdout.write.calledWith("No usage defined\n"), | ||
'write command usage'); | ||
t.ok(fakeStdout.write.calledWith("No usage defined\n"), 'write command usage'); | ||
@@ -470,0 +643,0 @@ t.ok(fakeExit.calledWith(0), 'exits cleanly'); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
21159
681
68