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:
@@ -5,6 +5,7 @@
|
|||||||
var projectBuilder = require('./lib/project_builder');
|
var projectBuilder = require('./lib/project_builder');
|
||||||
var Q = require('q');
|
var Q = require('q');
|
||||||
|
|
||||||
|
var pkg = require('./lib/component/pkg');
|
||||||
var gerrit = require('./lib/component/gerrit');
|
var gerrit = require('./lib/component/gerrit');
|
||||||
var editorconfig = require('./lib/component/editorconfig');
|
var editorconfig = require('./lib/component/editorconfig');
|
||||||
var license = require('./lib/component/license');
|
var license = require('./lib/component/license');
|
||||||
@@ -30,6 +31,7 @@
|
|||||||
|
|
||||||
// Initialize components.
|
// Initialize components.
|
||||||
Q(this)
|
Q(this)
|
||||||
|
.then(pkg.init) // Package.json
|
||||||
.then(gerrit.init) // Gerrit
|
.then(gerrit.init) // Gerrit
|
||||||
.then(editorconfig.init) // Editorconfig
|
.then(editorconfig.init) // Editorconfig
|
||||||
.then(license.init) // Licensing
|
.then(license.init) // Licensing
|
||||||
@@ -46,6 +48,7 @@
|
|||||||
|
|
||||||
// Prompt components.
|
// Prompt components.
|
||||||
Q(this)
|
Q(this)
|
||||||
|
.then(pkg.prompt) // Package.json
|
||||||
.then(gerrit.prompt) // Gerrit
|
.then(gerrit.prompt) // Gerrit
|
||||||
.then(editorconfig.prompt) // Editorconfig
|
.then(editorconfig.prompt) // Editorconfig
|
||||||
.then(license.prompt) // Licensing
|
.then(license.prompt) // Licensing
|
||||||
@@ -62,6 +65,7 @@
|
|||||||
|
|
||||||
// Configure components.
|
// Configure components.
|
||||||
Q(this)
|
Q(this)
|
||||||
|
.then(pkg.configure) // Package.json
|
||||||
.then(gerrit.configure) // Gerrit
|
.then(gerrit.configure) // Gerrit
|
||||||
.then(editorconfig.configure) // Editorconfig
|
.then(editorconfig.configure) // Editorconfig
|
||||||
.then(license.configure) // Licensing
|
.then(license.configure) // Licensing
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
(function () {
|
(function() {
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var projectBuilder = require('../project_builder');
|
var projectBuilder = require('../project_builder');
|
||||||
|
var pkgBuilder = require('../pkg_builder');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* No-op placeholder method, for handlers we don't need.
|
* No-op placeholder method, for handlers we don't need.
|
||||||
@@ -21,6 +22,7 @@
|
|||||||
*/
|
*/
|
||||||
function configureLicense (generator) {
|
function configureLicense (generator) {
|
||||||
projectBuilder.addFile('LICENSE');
|
projectBuilder.addFile('LICENSE');
|
||||||
|
pkgBuilder.setValues({license: 'Apache-2.0'});
|
||||||
|
|
||||||
return generator;
|
return generator;
|
||||||
}
|
}
|
||||||
|
|||||||
52
generators/app/lib/component/pkg.js
Normal file
52
generators/app/lib/component/pkg.js
Normal 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
|
||||||
|
};
|
||||||
|
})();
|
||||||
45
generators/app/lib/pkg_builder.js
Normal file
45
generators/app/lib/pkg_builder.js
Normal 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
|
||||||
|
};
|
||||||
|
})();
|
||||||
@@ -1,65 +1,66 @@
|
|||||||
(function () {
|
(function() {
|
||||||
'use strict';
|
'use strict';
|
||||||
var libDir = '../../../../generators/app/lib';
|
var libDir = '../../../../generators/app/lib';
|
||||||
|
|
||||||
var license = require(libDir + '/component/license');
|
var license = require(libDir + '/component/license');
|
||||||
var projectBuilder = require(libDir + '/project_builder');
|
var projectBuilder = require(libDir + '/project_builder');
|
||||||
|
var pkgBuilder = require(libDir + '/pkg_builder');
|
||||||
var mocks = require('../../../helpers/mocks');
|
var mocks = require('../../../helpers/mocks');
|
||||||
var mockGenerator;
|
var mockGenerator;
|
||||||
|
|
||||||
describe('generator-openstack:lib/component/license', function () {
|
describe('generator-openstack:lib/component/license', function() {
|
||||||
|
|
||||||
beforeEach(function () {
|
beforeEach(function() {
|
||||||
mockGenerator = mocks.buildGenerator();
|
mockGenerator = mocks.buildGenerator();
|
||||||
projectBuilder.clear();
|
projectBuilder.clear();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should define init, prompt, and configure',
|
it('should define init, prompt, and configure',
|
||||||
function () {
|
function() {
|
||||||
expect(typeof license.init).toBe('function');
|
expect(typeof license.init).toBe('function');
|
||||||
expect(typeof license.prompt).toBe('function');
|
expect(typeof license.prompt).toBe('function');
|
||||||
expect(typeof license.configure).toBe('function');
|
expect(typeof license.configure).toBe('function');
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('init()', function () {
|
describe('init()', function() {
|
||||||
it('should return a generator',
|
it('should return a generator',
|
||||||
function () {
|
function() {
|
||||||
var outputGenerator = license.init(mockGenerator);
|
var outputGenerator = license.init(mockGenerator);
|
||||||
expect(outputGenerator).toEqual(mockGenerator);
|
expect(outputGenerator).toEqual(mockGenerator);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should do nothing',
|
it('should do nothing',
|
||||||
function () {
|
function() {
|
||||||
var spy = spyOn(mockGenerator.config, 'defaults');
|
var spy = spyOn(mockGenerator.config, 'defaults');
|
||||||
license.init(mockGenerator);
|
license.init(mockGenerator);
|
||||||
expect(spy.calls.any()).toBeFalsy();
|
expect(spy.calls.any()).toBeFalsy();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('prompt()', function () {
|
describe('prompt()', function() {
|
||||||
it('should return a generator',
|
it('should return a generator',
|
||||||
function () {
|
function() {
|
||||||
var outputGenerator = license.prompt(mockGenerator);
|
var outputGenerator = license.prompt(mockGenerator);
|
||||||
expect(outputGenerator).toEqual(mockGenerator);
|
expect(outputGenerator).toEqual(mockGenerator);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should do nothing',
|
it('should do nothing',
|
||||||
function () {
|
function() {
|
||||||
var spy = spyOn(mockGenerator, 'prompt');
|
var spy = spyOn(mockGenerator, 'prompt');
|
||||||
license.prompt(mockGenerator);
|
license.prompt(mockGenerator);
|
||||||
expect(spy.calls.any()).toBeFalsy();
|
expect(spy.calls.any()).toBeFalsy();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('configure()', function () {
|
describe('configure()', function() {
|
||||||
it('should return a generator',
|
it('should return a generator',
|
||||||
function () {
|
function() {
|
||||||
var outputGenerator = license.configure(mockGenerator);
|
var outputGenerator = license.configure(mockGenerator);
|
||||||
expect(outputGenerator).toEqual(mockGenerator);
|
expect(outputGenerator).toEqual(mockGenerator);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should add license to the project files.',
|
it('should add license to the project files.',
|
||||||
function () {
|
function() {
|
||||||
license.configure(mockGenerator);
|
license.configure(mockGenerator);
|
||||||
|
|
||||||
var files = projectBuilder.getIncludedFiles();
|
var files = projectBuilder.getIncludedFiles();
|
||||||
@@ -67,6 +68,14 @@
|
|||||||
expect(files[0].from).toBe('LICENSE');
|
expect(files[0].from).toBe('LICENSE');
|
||||||
expect(files[0].to).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");
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
})();
|
})();
|
||||||
|
|||||||
75
spec/app/lib/component/pkg.js
Normal file
75
spec/app/lib/component/pkg.js
Normal 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);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
})();
|
||||||
55
spec/app/lib/pkg_builder.js
Normal file
55
spec/app/lib/pkg_builder.js
Normal 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();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
})();
|
||||||
Reference in New Issue
Block a user