Created simple package builder

This package builder reads in any existing package.json file,
exposes a set-value method, and then writes it back to the filesystem.
This commit is contained in:
Michael Krotscheck
2016-04-04 12:46:01 -07:00
parent 07cb1d455d
commit 46f08869e6
8 changed files with 260 additions and 18 deletions

View File

@@ -5,6 +5,7 @@
var projectBuilder = require('./lib/project_builder');
var Q = require('q');
var pkg = require('./lib/component/pkg');
var gerrit = require('./lib/component/gerrit');
var editorconfig = require('./lib/component/editorconfig');
var license = require('./lib/component/license');
@@ -30,7 +31,8 @@
// Initialize components.
Q(this)
.then(gerrit.init) // Gerrit
.then(pkg.init) // Package.json
.then(gerrit.init) // Gerrit
.then(editorconfig.init) // Editorconfig
.then(license.init) // Licensing
.then(eslint.init) // Linting
@@ -46,7 +48,8 @@
// Prompt components.
Q(this)
.then(gerrit.prompt) // Gerrit
.then(pkg.prompt) // Package.json
.then(gerrit.prompt) // Gerrit
.then(editorconfig.prompt) // Editorconfig
.then(license.prompt) // Licensing
.then(eslint.prompt) // Linting
@@ -62,7 +65,8 @@
// Configure components.
Q(this)
.then(gerrit.configure) // Gerrit
.then(pkg.configure) // Package.json
.then(gerrit.configure) // Gerrit
.then(editorconfig.configure) // Editorconfig
.then(license.configure) // Licensing
.then(eslint.configure) // Linting

View File

@@ -1,7 +1,8 @@
(function () {
(function() {
'use strict';
var projectBuilder = require('../project_builder');
var pkgBuilder = require('../pkg_builder');
/**
* No-op placeholder method, for handlers we don't need.
@@ -21,6 +22,7 @@
*/
function configureLicense (generator) {
projectBuilder.addFile('LICENSE');
pkgBuilder.setValues({license: 'Apache-2.0'});
return generator;
}

View File

@@ -0,0 +1,52 @@
(function() {
'use strict';
var pkgBuilder = require('../pkg_builder');
var projectBuilder = require('../project_builder');
var packagePath = 'package.json';
/**
* No-op placeholder method, for handlers we don't need.
*
* @param {generator} generator The currently active generator.
* @returns {generator} The passed generator, for promise chaining.
*/
function noop (generator) {
return generator;
}
/**
* Read any existing package.json file, to set our defaults.
*
* @param {generator} generator The currently active generator.
* @returns {generator} The passed generator, for promise chaining.
*/
function initializePackage (generator) {
var fs = generator.fs;
// Read package.json
if (fs.exists(packagePath)) {
pkgBuilder.fromJSON(fs.read(packagePath));
}
return generator;
}
/**
* Configure the project by adding required files.
*
* @param {generator} generator The currently active generator.
* @returns {generator} The passed generator, for promise chaining.
*/
function configurePkg (generator) {
projectBuilder.writeFile('package.json', pkgBuilder.toJSON);
return generator;
}
module.exports = {
init: initializePackage,
prompt: noop,
configure: configurePkg
};
})();

View File

@@ -0,0 +1,45 @@
(function() {
'use strict';
var pkgContent = {};
/**
* Initialize this builder from a JSON string.
*
* @param {String} pkgString The package string content.
* @returns {void}
*/
function readPackage (pkgString) {
pkgContent = JSON.parse(pkgString);
}
/**
* Write the package content to a JSON string.
*
* @returns {String} The JSON content of the package, as a string.
*/
function writePackage () {
return JSON.stringify(pkgContent, null, 2);
}
/**
* Set values on the current package.
*
* @param {{}} values A map of values.
* @returns {void}
*/
function setValues (values) {
for (var key in values) {
// Filter out things from prototype.
if (values.hasOwnProperty(key)) {
pkgContent[key] = values[key];
}
}
}
module.exports = {
fromJSON: readPackage,
toJSON: writePackage,
setValues: setValues
};
})();

View File

@@ -39,4 +39,4 @@
"yeoman-test": "^1.1.0"
},
"repository": "https://github.com/krotscheck/generator-openstack"
}
}

View File

@@ -1,65 +1,66 @@
(function () {
(function() {
'use strict';
var libDir = '../../../../generators/app/lib';
var license = require(libDir + '/component/license');
var projectBuilder = require(libDir + '/project_builder');
var pkgBuilder = require(libDir + '/pkg_builder');
var mocks = require('../../../helpers/mocks');
var mockGenerator;
describe('generator-openstack:lib/component/license', function () {
describe('generator-openstack:lib/component/license', function() {
beforeEach(function () {
beforeEach(function() {
mockGenerator = mocks.buildGenerator();
projectBuilder.clear();
});
it('should define init, prompt, and configure',
function () {
function() {
expect(typeof license.init).toBe('function');
expect(typeof license.prompt).toBe('function');
expect(typeof license.configure).toBe('function');
});
describe('init()', function () {
describe('init()', function() {
it('should return a generator',
function () {
function() {
var outputGenerator = license.init(mockGenerator);
expect(outputGenerator).toEqual(mockGenerator);
});
it('should do nothing',
function () {
function() {
var spy = spyOn(mockGenerator.config, 'defaults');
license.init(mockGenerator);
expect(spy.calls.any()).toBeFalsy();
});
});
describe('prompt()', function () {
describe('prompt()', function() {
it('should return a generator',
function () {
function() {
var outputGenerator = license.prompt(mockGenerator);
expect(outputGenerator).toEqual(mockGenerator);
});
it('should do nothing',
function () {
function() {
var spy = spyOn(mockGenerator, 'prompt');
license.prompt(mockGenerator);
expect(spy.calls.any()).toBeFalsy();
});
});
describe('configure()', function () {
describe('configure()', function() {
it('should return a generator',
function () {
function() {
var outputGenerator = license.configure(mockGenerator);
expect(outputGenerator).toEqual(mockGenerator);
});
it('should add license to the project files.',
function () {
function() {
license.configure(mockGenerator);
var files = projectBuilder.getIncludedFiles();
@@ -67,6 +68,14 @@
expect(files[0].from).toBe('LICENSE');
expect(files[0].to).toBe('LICENSE');
});
it('should add license to the package.json files.',
function() {
license.configure(mockGenerator);
var parsedResult = JSON.parse(pkgBuilder.toJSON());
expect(parsedResult.license).toBe("Apache-2.0");
});
});
});
})();

View File

@@ -0,0 +1,75 @@
(function() {
'use strict';
var libDir = '../../../../generators/app/lib';
var pkg = require(libDir + '/component/pkg');
var projectBuilder = require(libDir + '/project_builder');
var pkgBuilder = require(libDir + '/pkg_builder');
var mocks = require('../../../helpers/mocks');
var mockGenerator;
describe('generator-openstack:lib/component/pkg', function() {
beforeEach(function() {
mockGenerator = mocks.buildGenerator();
projectBuilder.clear();
});
it('should define init, prompt, and configure',
function() {
expect(typeof pkg.init).toBe('function');
expect(typeof pkg.prompt).toBe('function');
expect(typeof pkg.configure).toBe('function');
});
describe('init()', function() {
it('should return a generator',
function() {
var outputGenerator = pkg.init(mockGenerator);
expect(outputGenerator).toEqual(mockGenerator);
});
it('should read an existing package.json file into the package builder',
function() {
mockGenerator.fs.writeJSON("package.json", {name: "foo"});
pkg.init(mockGenerator);
var output = JSON.parse(pkgBuilder.toJSON());
expect(output.name).toBe('foo');
});
});
describe('prompt()', function() {
it('should return a generator',
function() {
var outputGenerator = pkg.prompt(mockGenerator);
expect(outputGenerator).toEqual(mockGenerator);
});
it('should do nothing',
function() {
var spy = spyOn(mockGenerator, 'prompt');
pkg.prompt(mockGenerator);
expect(spy.calls.any()).toBeFalsy();
});
});
describe('configure()', function() {
it('should return a generator',
function() {
var outputGenerator = pkg.configure(mockGenerator);
expect(outputGenerator).toEqual(mockGenerator);
});
it('should add package.json to the project files.',
function() {
pkg.configure(mockGenerator);
var files = projectBuilder.getIncludedFiles();
expect(files.length).toBe(1);
expect(files[0].to).toBe('package.json');
expect(files[0].content).toBe(pkgBuilder.toJSON);
});
});
});
})();

View File

@@ -0,0 +1,55 @@
(function() {
'use strict';
var builder = require('../../../generators/app/lib/pkg_builder');
describe('generator-openstack:lib/pkg_builder', function() {
beforeEach(function() {
builder.fromJSON("{}"); // Clear
});
it('should start as an empty object',
function() {
expect(builder.toJSON()).toBe("{}");
});
it('should honor and echo back any pre-loaded package file',
function() {
var packageString = '{"name":"foo"}';
builder.fromJSON(packageString);
var parsedResult = JSON.parse(builder.toJSON());
expect(parsedResult.name).toBe("foo");
});
it('should permit adding and overriding values.',
function() {
builder.fromJSON('{"name":"foo"}');
builder.setValues({name: "bar", lol: "cat"});
var parsedResult = JSON.parse(builder.toJSON());
expect(parsedResult.name).toBe("bar");
expect(parsedResult.lol).toBe("cat");
});
it('should not add parent prototype values.',
function() {
function Thing () {
}
Thing.prototype.foo = 'bar';
var thing = new Thing();
thing.name = 'bar';
thing.lol = 'cat';
builder.fromJSON('{"name":"foo"}');
builder.setValues(thing);
var parsedResult = JSON.parse(builder.toJSON());
expect(parsedResult.name).toBe("bar");
expect(parsedResult.lol).toBe("cat");
expect(parsedResult.foo).toBeUndefined();
});
});
})();