diff --git a/generators/app/lib/pkg_builder.js b/generators/app/lib/pkg_builder.js index 676524a..0857c67 100644 --- a/generators/app/lib/pkg_builder.js +++ b/generators/app/lib/pkg_builder.js @@ -1,4 +1,4 @@ -(function() { +(function () { 'use strict'; var pkgContent = {}; @@ -37,9 +37,33 @@ } } + /** + * Get the values of the current package. + * + * @returns {{}} A cloned map of the values. + */ + function getValues () { + return JSON.parse(JSON.stringify(pkgContent)); + } + + /** + * Get a specific value from the package.json file. + * + * @param {String} name The name of the value. + * @returns {{}} A clone of the referenced value. + */ + function getValue (name) { + if (pkgContent.hasOwnProperty(name)) { + return JSON.parse(JSON.stringify(pkgContent[name])); + } + return undefined; + } + module.exports = { fromJSON: readPackage, toJSON: writePackage, - setValues: setValues + setValues: setValues, + getValues: getValues, + getValue: getValue }; })(); diff --git a/spec/app/lib/pkg_builder.js b/spec/app/lib/pkg_builder.js index 6e22ed0..8be0b5d 100644 --- a/spec/app/lib/pkg_builder.js +++ b/spec/app/lib/pkg_builder.js @@ -1,20 +1,20 @@ -(function() { +(function () { 'use strict'; var builder = require('../../../generators/app/lib/pkg_builder'); - describe('generator-openstack:lib/pkg_builder', function() { + describe('generator-openstack:lib/pkg_builder', function () { - beforeEach(function() { + beforeEach(function () { builder.fromJSON("{}"); // Clear }); it('should start as an empty object', - function() { + function () { expect(builder.toJSON()).toBe("{}"); }); it('should honor and echo back any pre-loaded package file', - function() { + function () { var packageString = '{"name":"foo"}'; builder.fromJSON(packageString); @@ -23,7 +23,7 @@ }); it('should permit adding and overriding values.', - function() { + function () { builder.fromJSON('{"name":"foo"}'); builder.setValues({name: "bar", lol: "cat"}); @@ -33,7 +33,7 @@ }); it('should not add parent prototype values.', - function() { + function () { function Thing () { } @@ -51,5 +51,27 @@ expect(parsedResult.lol).toBe("cat"); expect(parsedResult.foo).toBeUndefined(); }); + + describe('getValues()', function () { + it('should permit retrieving the entire package block.', + function () { + builder.fromJSON('{"name":"foo"}'); + expect(builder.getValues()).toEqual({name: 'foo'}); + }); + }); + + describe('getValue()', function () { + it('should permit retrieving values from the package.', + function () { + builder.fromJSON('{"name":"foo"}'); + expect(builder.getValue('name')).toBe('foo'); + }); + + it('should return undefined if the value is not set.', + function () { + builder.fromJSON('{"name":"foo"}'); + expect(builder.getValue('invalidname')).toBeUndefined(); + }); + }); }); })();