Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/generationtux/cufflink in…
Browse files Browse the repository at this point in the history
…to OP-266-teardown-command

* 'master' of https://github.com/generationtux/cufflink:
  Move drivers
  0.0.26
  Added bin
  0.0.25
  renamed entrypoint
  0.0.24
  Fixed failing test
  removed seeded data
  its working
  formatting and remove pointless test.js file
  Added create command
  • Loading branch information
troyharvey committed Oct 19, 2016
2 parents 6826adc + f5dd23b commit 2bb89a6
Show file tree
Hide file tree
Showing 13 changed files with 81 additions and 38 deletions.
39 changes: 39 additions & 0 deletions Drivers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
let fs = require('fs');
let counter = 0;
class CustomerDriver {

create() {
counter++;
fs.writeFileSync("./customer.txt", `Created Customer Counter: ${counter}`);
return {
"name": "Bob Dole"
};
}
}

class CartDriver {
create() {
counter++;
fs.writeFileSync("./cart.txt", `Created Cart Counter: ${counter}`);
return {
"items": ["Pizza"]
}
}
}

class ItemDriver {
create() {
counter++;
fs.writeFileSync("./item.txt", `Created Item Counter: ${counter}`);
return {
"name": "Pizza",
"price": 1.00
};
}
}

module.exports = {
"ItemDriver": ItemDriver,
"CustomerDriver": CustomerDriver,
"CartDriver": CartDriver
};
5 changes: 0 additions & 5 deletions bin/cufflink.js

This file was deleted.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "cufflink",
"version": "0.0.23",
"version": "0.0.26",
"description": "A command tool to help generate seed data based on your object schema in your applications",
"main": "index.js",
"main": "src/cufflink.js",
"directories": {
"test": "test"
},
Expand All @@ -29,7 +29,7 @@
"author": "Gentux",
"license": "MIT",
"bin": {
"cufflink": "./bin/cufflink.js"
"cufflink": "./src/cufflink.js"
},
"bugs": {
"url": "https://github.com/generationtux/cufflink/issues"
Expand Down
4 changes: 3 additions & 1 deletion src/CreateCommand.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class CreateCommand {

constructor(dependencyGraph, driverLocator, fs) {
this.dependencyGraph = dependencyGraph;
this.driverLocator = driverLocator;
Expand All @@ -19,6 +20,7 @@ class CreateCommand {

this.fs.writeFileSync('./seededData.json', JSON.stringify(result));
}

}

module.exports.CreateCommand = CreateCommand;
module.exports = CreateCommand;
24 changes: 11 additions & 13 deletions src/Driver/DriverLocator.js → src/DriverLocator.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,37 @@
class DriverLocator
{
constructor(fs, path)
{
class DriverLocator {

constructor(fs, path) {
this.fs = fs;
this.path = path;
}


drivers()
{
if (!this.fs.existsSync(this.path)){
drivers() {
if (!this.fs.existsSync(this.path)) {
throw Error(this.path + ' path does not exist');
}

let driverModules = require(this.path);
let driverNames = Object.keys(driverModules);
let driversList = {};
driverNames.forEach(function(driverName) {
driverNames.forEach(function (driverName) {
let driverKey = driverName.toLocaleLowerCase();
if(driverKey.indexOf('driver') < 0) {
if (driverKey.indexOf('driver') < 0) {
return;
}
driverKey = driverKey.replace('driver', '');
driversList[driverKey] = new driverModules[driverName]();
});
if(Object.keys(driversList).length <= 0) {
if (Object.keys(driversList).length <= 0) {
throw Error(
'No drivers found in module: ' +
this.path +
" drivers must have 'driver' in name"
);
}

return driversList;
}
}

}

module.exports.DriverLocator = DriverLocator;
module.exports = DriverLocator;
3 changes: 1 addition & 2 deletions src/ObjectLocator.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@ class ObjectLocator {
fileJson = JSON.parse(fileData);
}
if (fileJson != null && fileJson != {}) {
console.log(fileJson.dependencies);
if (fileJson.dependencies == null) {
throw new Error(`Json for the file ${dependencyName} must have a dependency array, if none provide a blank array`);
}
let dependencies = fileJson.dependencies;
if (dependencies instanceof Array) {
dependencies.forEach((dependency) => {
if(this.objects.find(x => x.name == dependency) == null) {
if (this.objects.find(x => x.name == dependency) == null) {
this.loadAllObjects(dependency);
}
});
Expand Down
15 changes: 15 additions & 0 deletions src/cufflink-create.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
let ObjectLocator = require('./ObjectLocator');
let DependencyGraph = require('./DependencyGraph');
let DriverLocator = require('./DriverLocator');
let CreateCommand = require('./CreateCommand');
let fs = require('fs');
let objectName = process.argv[2];

let objectLocator = new ObjectLocator(fs,objectName );
let objects = objectLocator.run();

let dependencyGraph = new DependencyGraph(objects);
let driverLocator = new DriverLocator(fs, process.cwd() + "/drivers/");
let createCommand = new CreateCommand(dependencyGraph,driverLocator,fs);
createCommand.run();

1 change: 1 addition & 0 deletions src/cufflink.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ program
*/
program
.command('create [dataObject]', 'Generate seed data').alias('c')
.parse(process.argv);

2 changes: 1 addition & 1 deletion test/CreateCommandTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
let expect = require('chai').expect;
let DepGraph = require('dependency-graph').DepGraph;

let CreateCommand = require('../src/CreateCommand').CreateCommand;
let CreateCommand = require('../src/CreateCommand');

describe('Create command tests', () => {
it('should write results from driver to file', () => {
Expand Down
4 changes: 2 additions & 2 deletions test/DependencyGraphTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('Dependency Graph Tests', () => {
{
"name": "SalesOrder",
"metadata": {
'dependencies': ['Contact','Account','Event']
'dependencies': ['Contact', 'Account', 'Event']
}
},
{
Expand All @@ -33,6 +33,6 @@ describe('Dependency Graph Tests', () => {
let dependencyGraph = new DependencyGraph(objectsToSeed);
expect(dependencyGraph.objectsToSeed).to.equal(objectsToSeed);
let result = dependencyGraph.run();
expect(result).to.deep.equal(['Account','Contact','Event','SalesOrder']);
expect(result).to.deep.equal(['Account', 'Contact', 'Event', 'SalesOrder']);
});
});
12 changes: 5 additions & 7 deletions test/DriverLocatorTest.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
let expect = require('chai').expect;
let fs = require('fs');
let DriverLocator = require('../src/Driver/DriverLocator').DriverLocator;
let DriverLocator = require('../src/DriverLocator');
let ContactDriver = require('./MockDrivers/ContactDriver').ContactDriver;
let EventDriver = require('./MockDrivers/EventDriver').EventDriver;



describe('Driver Locator Module', function() {
it('it should throw an error if no drivers are found at specified location', function() {
describe('Driver Locator Module', function () {
it('it should throw an error if no drivers are found at specified location', function () {
let invalidDriverPath = fs.realpathSync('./test/MockDrivers/InvalidDriver.js');
let driverLocator = new DriverLocator(fs, invalidDriverPath);
expect(driverLocator.drivers.bind(driverLocator)).to.throw(
Expand All @@ -16,14 +14,14 @@ describe('Driver Locator Module', function() {
);
});

it('should read all driver files in drivers directory', function(){
it('should read all driver files in drivers directory', function () {
let driverLocator = new DriverLocator(fs, fs.realpathSync('./test/MockDrivers/'));
let actualDrivers = driverLocator.drivers();
expect(actualDrivers['contact']).to.be.an.instanceOf(ContactDriver);
expect(actualDrivers['event']).to.be.an.instanceOf(EventDriver);
});

it('should throw exception if path doesnt exist', function(){
it('should throw exception if path doesnt exist', function () {
var fsMock = {
existsSync: function (path) {

Expand Down
1 change: 0 additions & 1 deletion test/ObjectLocatorTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ describe('Object Locator Tests', () => {
}).to.throw("Json for the file BadCustomer must have a dependency array, if none provide a blank array");
});


it('should return error on dependencies not being an array', () => {
let obj = new objLoc(fs, "BadCart");
expect(() => {
Expand Down
3 changes: 0 additions & 3 deletions test/test.js

This file was deleted.

0 comments on commit 2bb89a6

Please sign in to comment.