Skip to content

Commit

Permalink
Merge ae7f337 into 7476892
Browse files Browse the repository at this point in the history
  • Loading branch information
sivakumar-kailasam committed Jul 15, 2015
2 parents 7476892 + ae7f337 commit 5e3ec19
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 10 deletions.
14 changes: 9 additions & 5 deletions blueprints/http-mock/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*jshint node:true*/

var Blueprint = require('../../lib/models/blueprint');
var isPackageMissing = require('../../lib/utilities/is-package-missing');

module.exports = {
description: 'Generates a mock api endpoint in /api prefix.',
Expand All @@ -25,9 +25,13 @@ module.exports = {
return serverBlueprint.install(options);
},

afterInstall: function() {
return this.addPackagesToProject([
{ name: 'express', target: '^4.8.5' }
]);
afterInstall: function(options) {

if (!options.dryRun && isPackageMissing(this, 'express')) {
return this.addPackagesToProject([
{ name: 'express', target: '^4.8.5' }
]);
}

}
};
26 changes: 21 additions & 5 deletions blueprints/server/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,30 @@
/*jshint node:true*/
var isPackageMissing = require('../../lib/utilities/is-package-missing');

module.exports = {
description: 'Generates a server directory for mocks and proxies.',

normalizeEntityName: function() {},

afterInstall: function() {
return this.addPackagesToProject([
{ name: 'morgan', target: '^1.3.2' },
{ name: 'glob', target: '^4.0.5' }
]);
afterInstall: function(options) {

var isMorganMissing = isPackageMissing(this, 'morgan');
var isGlobMissing = isPackageMissing(this, 'glob');

var areDependenciesMissing = isMorganMissing || isGlobMissing;
var libsToInstall = [];

if (isMorganMissing) {
libsToInstall.push({ name: 'morgan', target: '^1.3.2' });
}

if (isGlobMissing) {
libsToInstall.push({ name: 'glob', target: '^4.0.5' });
}

if (!options.dryRun && areDependenciesMissing) {
return this.addPackagesToProject(libsToInstall);
}

}
};
19 changes: 19 additions & 0 deletions lib/utilities/is-package-missing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict';

/*
*
* Given the context and library that's supposed to be looked up in the package.json,
* this method detects if its already available.
*
* @method isPackageMissing
* @param context The context of the method its called from (ie., this)
* @param packageName The package that's supposed to be looked up
* @return Boolean
*
*/
module.exports = function (context, packageName) {
var pkgContent = context.project.pkg;
var isAvailableInDevDependency = pkgContent.devDependencies && pkgContent.devDependencies[packageName];
var isAvailableInDependency = pkgContent.dependencies && pkgContent.dependencies[packageName];
return !(isAvailableInDevDependency || isAvailableInDependency);
};
47 changes: 47 additions & 0 deletions tests/unit/utilities/is-package-missing-tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
'use strict';

var expect = require('chai').expect;
var isPackageMissing = require('../../../lib/utilities/is-package-missing');


var getContext = function(setDevDependency, setDependency) {
var context = {
project: {
pkg: {}
}
};

if (setDevDependency) {
context.project.pkg['devDependencies'] = {
'sivakumar': 'kailasam'
};
}

if (setDependency) {
context.project.pkg['dependencies'] = {
'sivakumar': 'kailasam'
};
}
return context;
};


describe('Is package missing in package.json', function() {

it('Package is declared in dependencies', function() {
expect(isPackageMissing(getContext(false, true), 'sivakumar')).to.be.false;
});

it('Package is declared in dev dependencies', function() {
expect(isPackageMissing(getContext(true, false), 'sivakumar')).to.be.false;
});

it('Package is declared in both dependencies and dev dependencies', function() {
expect(isPackageMissing(getContext(true, true), 'sivakumar')).to.be.false;
});

it('Package is missing', function() {
expect(isPackageMissing(getContext(false, false), 'sivakumar')).to.be.true;
});

});

0 comments on commit 5e3ec19

Please sign in to comment.