Package builder now adds dependencies
This adds a convenience method to the package.json builder, which wraps various sanity checks around the process of adding a dependency to a project. Change-Id: I111b3d5ead31979933993916d091277c94290d25
This commit is contained in:
@@ -46,6 +46,41 @@
|
||||
return JSON.stringify(newContent, null, 2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add libraries to the package dependencies.
|
||||
*
|
||||
* @param {[]|String} libraryNames A list of all libraries to add to the dependencies.
|
||||
* @param {String} type The type of dependency.
|
||||
* @returns {void}
|
||||
*/
|
||||
function addDependencies (libraryNames, type) {
|
||||
// Default the type.
|
||||
type = type || 'dependencies';
|
||||
|
||||
// Valuecheck type.
|
||||
if (['devDependencies', 'peerDependencies', 'dependencies'].indexOf(type) === -1) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Default the array.
|
||||
if (!Array.isArray(libraryNames)) {
|
||||
libraryNames = [libraryNames];
|
||||
}
|
||||
|
||||
// Make sure the property exists.
|
||||
if (!pkgContent.hasOwnProperty(type)) {
|
||||
pkgContent[type] = {};
|
||||
}
|
||||
|
||||
// Add the dependency
|
||||
libraryNames.forEach(function (library) {
|
||||
var version = dependencies.read(library);
|
||||
if (version && !pkgContent[type].hasOwnProperty(library)) {
|
||||
pkgContent[type][library] = version;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Set values on the current package.
|
||||
*
|
||||
@@ -90,6 +125,7 @@
|
||||
toJSON: writePackage,
|
||||
setValues: setValues,
|
||||
getValues: getValues,
|
||||
getValue: getValue
|
||||
getValue: getValue,
|
||||
addDependencies: addDependencies
|
||||
};
|
||||
})();
|
||||
|
||||
@@ -117,5 +117,75 @@
|
||||
.toBe('defaultValue');
|
||||
});
|
||||
});
|
||||
|
||||
describe('addDependencies()', function () {
|
||||
var eslintVersion = dependencies.read('eslint');
|
||||
|
||||
it('should be able to add to dependencies', function () {
|
||||
builder.fromJSON('{"dependencies":{}}');
|
||||
builder.addDependencies('eslint');
|
||||
expect(builder.getValue('dependencies').eslint).toBe(eslintVersion);
|
||||
|
||||
builder.fromJSON('{"dependencies":{}}');
|
||||
builder.addDependencies(['eslint']);
|
||||
expect(builder.getValue('dependencies').eslint).toBe(eslintVersion);
|
||||
|
||||
builder.fromJSON('{"dependencies":{}}');
|
||||
builder.addDependencies('eslint', 'dependencies');
|
||||
expect(builder.getValue('dependencies').eslint).toBe(eslintVersion);
|
||||
|
||||
builder.fromJSON('{"dependencies":{}}');
|
||||
builder.addDependencies(['eslint'], 'dependencies');
|
||||
expect(builder.getValue('dependencies').eslint).toBe(eslintVersion);
|
||||
});
|
||||
|
||||
it('should be able to add to devDependencies', function () {
|
||||
builder.fromJSON('{"devDependencies":{}}');
|
||||
builder.addDependencies('eslint', 'devDependencies');
|
||||
expect(builder.getValue('devDependencies').eslint).toBe(eslintVersion);
|
||||
|
||||
builder.fromJSON('{"devDependencies":{}}');
|
||||
builder.addDependencies(['eslint'], 'devDependencies');
|
||||
expect(builder.getValue('devDependencies').eslint).toBe(eslintVersion);
|
||||
});
|
||||
|
||||
it('should be able to add to peerDependencies', function () {
|
||||
builder.fromJSON('{"peerDependencies":{}}');
|
||||
builder.addDependencies('eslint', 'peerDependencies');
|
||||
expect(builder.getValue('peerDependencies').eslint).toBe(eslintVersion);
|
||||
|
||||
builder.fromJSON('{"peerDependencies":{}}');
|
||||
builder.addDependencies(['eslint'], 'peerDependencies');
|
||||
expect(builder.getValue('peerDependencies').eslint).toBe(eslintVersion);
|
||||
});
|
||||
|
||||
it('should create dependency maps if they don\'t yet exist in the package', function () {
|
||||
builder.fromJSON('{}');
|
||||
builder.addDependencies('eslint');
|
||||
builder.addDependencies('eslint', 'devDependencies');
|
||||
builder.addDependencies('eslint', 'peerDependencies');
|
||||
expect(builder.getValue('dependencies')).not.toBeUndefined();
|
||||
expect(builder.getValue('devDependencies')).not.toBeUndefined();
|
||||
expect(builder.getValue('peerDependencies')).not.toBeUndefined();
|
||||
});
|
||||
|
||||
it('should not modify things if an invalid section was declared', function () {
|
||||
builder.fromJSON('{}');
|
||||
builder.addDependencies('eslint', 'lol');
|
||||
expect(builder.getValues()).toEqual({});
|
||||
});
|
||||
|
||||
it('should not override an existing dependency declaration', function () {
|
||||
builder.fromJSON('{"dependencies":{"eslint":"0.0.1"}}');
|
||||
builder.addDependencies(['eslint'], 'dependencies');
|
||||
expect(builder.getValue('dependencies').eslint).toEqual('0.0.1');
|
||||
});
|
||||
|
||||
it('should not add a dependency that is not globally managed', function () {
|
||||
builder.fromJSON('{}');
|
||||
builder.addDependencies('leftpad');
|
||||
expect(builder.getValues()).toEqual({dependencies: {}});
|
||||
});
|
||||
});
|
||||
});
|
||||
})();
|
||||
|
||||
Reference in New Issue
Block a user