From 394dd7c8d26cd031da9237d591406f6f76c5e690 Mon Sep 17 00:00:00 2001 From: blcook223 Date: Thu, 28 Jan 2016 20:49:30 -0600 Subject: [PATCH] add support for save and save-exact in .bowerrc The install command now supports designating "save" and "save-exact" values in .bowerrc. If the `--save` flag is not used, but the config file sets "save" to true, the command will behave as if the `--save` option was specified. The same is true of `--save-exact` if "save-exact" is set to true in `.bowerrc`. The uninstall command will also behave as if the `--save` flag had been specified if "save" is set to true in `.bowerrc`. --- lib/core/Project.js | 6 ++-- test/commands/install.js | 67 ++++++++++++++++++++++++++++++++++++++ test/commands/uninstall.js | 11 +++++++ 3 files changed, 81 insertions(+), 3 deletions(-) diff --git a/lib/core/Project.js b/lib/core/Project.js index 30b930726..bc988ae6f 100644 --- a/lib/core/Project.js +++ b/lib/core/Project.js @@ -82,14 +82,14 @@ Project.prototype.install = function (decEndpoints, options, config) { }) .then(function (installed) { // Handle save and saveDev options - if (that._options.save || that._options.saveDev || that._options.saveExact) { + if (that._options.save || that._options.saveDev || that._options.saveExact || that._config.save || that._config.saveExact) { // Cycle through the specified endpoints decEndpoints.forEach(function (decEndpoint) { var jsonEndpoint; jsonEndpoint = endpointParser.decomposed2json(decEndpoint); - if (that._options.saveExact) { + if (that._options.saveExact || that._config.saveExact) { if (decEndpoint.name !== decEndpoint.source) { jsonEndpoint[decEndpoint.name] = decEndpoint.source + '#' + decEndpoint.pkgMeta.version; } else { @@ -741,7 +741,7 @@ Project.prototype._removePackages = function (packages) { } // Remove from json only if successfully deleted - if (that._options.save && that._json.dependencies) { + if ((that._options.save || that._config.save) && that._json.dependencies) { promise = promise .then(function () { delete that._json.dependencies[name]; diff --git a/test/commands/install.js b/test/commands/install.js index 566f82810..e94f42d2f 100644 --- a/test/commands/install.js +++ b/test/commands/install.js @@ -85,6 +85,24 @@ describe('bower install', function() { }); }); + it('writes to bower.json if save config setting is set to true', function() { + mainPackage.prepare(); + + tempDir.prepare({ + 'bower.json': { + name: 'test' + } + }); + + return helpers.run(install, [ + [mainPackage.path], {}, { + save: true + } + ]).then(function() { + expect(tempDir.read('bower.json')).to.contain('dependencies'); + }); + }); + it('writes an exact version number to dependencies in bower.json if --save --save-exact flags are used', function() { mainPackage.prepare({ 'bower.json': { @@ -109,6 +127,30 @@ describe('bower install', function() { }); }); + it('writes an exact version number to dependencies in bower.json if save and save-exact config settings are set to true', function() { + mainPackage.prepare({ + 'bower.json': { + name: 'package', + version: '1.2.3' + } + }); + + tempDir.prepare({ + 'bower.json': { + name: 'test' + } + }); + + return helpers.run(install, [ + [mainPackage.path], {}, { + saveExact: true, + save: true + } + ]).then(function() { + expect(tempDir.readJson('bower.json').dependencies.package).to.equal(mainPackage.path + '#1.2.3'); + }); + }); + it('writes an exact version number to devDependencies in bower.json if --save-dev --save-exact flags are used', function() { mainPackage.prepare({ 'bower.json': { @@ -133,6 +175,31 @@ describe('bower install', function() { }); }); + it('writes an exact version number to devDependencies in bower.json if save-exact config setting is true and --save-dev flag is used', function() { + mainPackage.prepare({ + 'bower.json': { + name: 'package', + version: '0.1.0' + } + }); + + tempDir.prepare({ + 'bower.json': { + name: 'test' + } + }); + + return helpers.run(install, [ + [mainPackage.path], { + saveDev: true + }, { + saveExact: true + } + ]).then(function() { + expect(tempDir.readJson('bower.json').devDependencies.package).to.equal(mainPackage.path + '#0.1.0'); + }); + }); + it('reads .bowerrc from cwd', function() { mainPackage.prepare({ foo: 'bar' diff --git a/test/commands/uninstall.js b/test/commands/uninstall.js index f26a6c5fc..838f7674a 100644 --- a/test/commands/uninstall.js +++ b/test/commands/uninstall.js @@ -54,6 +54,17 @@ describe('bower uninstall', function () { }); }); + it('removes dependency from bower.json if save config setting is true', function () { + var configWithSave = { + cwd: tempDir.path, + interactive: true, + save: true + }; + return helpers.run(uninstall, [['underscore'], {}, configWithSave]).then(function () { + expect(bowerJson().dependencies).to.eql({}); + }); + }); + it('removes dependency from relative config.directory', function () { var targetPath = path.resolve(tempDir.path, 'other_directory/underscore'); mkdirp.sync(targetPath);