Merge pull request #1504 from bower/fix/hook-order

Run posthook after saving bower.json, fixes #1471
This commit is contained in:
Sindre Sorhus
2014-09-07 20:06:28 +02:00
5 changed files with 166 additions and 53 deletions

View File

@@ -54,4 +54,9 @@ function readCachedConfig(cwd) {
return config;
}
function resetCache () {
cachedConfigs = {};
}
module.exports = defaultConfig;
module.exports.reset = resetCache;

View File

@@ -109,6 +109,40 @@ Manager.prototype.resolve = function () {
}.bind(this));
};
Manager.prototype.preinstall = function (json) {
var that = this;
var componentsDir = path.join(this._config.cwd, this._config.directory);
// If nothing to install, skip the code bellow
if (mout.lang.isEmpty(that._dissected)) {
return Q.resolve({});
}
return Q.nfcall(mkdirp, componentsDir)
.then(function () {
return scripts.preinstall(
that._config, that._logger, that._dissected, that._installed, json
);
});
};
Manager.prototype.postinstall = function (json) {
var that = this;
var componentsDir = path.join(this._config.cwd, this._config.directory);
// If nothing to install, skip the code bellow
if (mout.lang.isEmpty(that._dissected)) {
return Q.resolve({});
}
return Q.nfcall(mkdirp, componentsDir)
.then(function () {
return scripts.postinstall(
that._config, that._logger, that._dissected, that._installed, json
);
});
};
Manager.prototype.install = function (json) {
var componentsDir;
var that = this;
@@ -125,9 +159,6 @@ Manager.prototype.install = function (json) {
componentsDir = path.join(this._config.cwd, this._config.directory);
return Q.nfcall(mkdirp, componentsDir)
.then(function () {
return scripts.preinstall(that._config, that._logger, that._dissected, that._installed, json);
})
.then(function () {
var promises = [];
@@ -169,9 +200,6 @@ Manager.prototype.install = function (json) {
return Q.all(promises);
})
.then(function () {
return scripts.postinstall(that._config, that._logger, that._dissected, that._installed, json);
})
.then(function () {
// Sync up dissected dependencies and dependants
// See: https://github.com/bower/bower/issues/879

View File

@@ -78,6 +78,12 @@ Project.prototype.install = function (decEndpoints, options, config) {
// Bootstrap the process
return that._bootstrap(targets, resolved, incompatibles);
})
.then(function () {
return that._manager.preinstall(that._json);
})
.then(function () {
return that._manager.install(that._json);
})
.then(function (installed) {
// Handle save and saveDev options
if (that._options.save || that._options.saveDev) {
@@ -100,7 +106,9 @@ Project.prototype.install = function (decEndpoints, options, config) {
// Save JSON, might contain changes to dependencies and resolutions
return that.saveJson()
.then(function () {
return installed;
return that._manager.postinstall(that._json).then(function () {
return installed;
});
});
})
.fin(function () {
@@ -181,11 +189,16 @@ Project.prototype.update = function (names, options) {
// Bootstrap the process
return that._bootstrap(targets, resolved, incompatibles)
.then(function () {
return that._manager.preinstall(that._json);
})
.then(function (installed) {
// Save JSON, might contain changes to resolutions
return that.saveJson()
.then(function () {
return installed;
return that._manager.postinstall(that._json).then(function () {
return installed;
});
});
});
})
@@ -529,9 +542,7 @@ Project.prototype._bootstrap = function (targets, resolved, incompatibles) {
if (!mout.object.size(this._json.resolutions)) {
delete this._json.resolutions;
}
}.bind(this))
// Install resolved ones
.then(this._manager.install.bind(this._manager, this._json));
}.bind(this));
};
Project.prototype._readJson = function () {

View File

@@ -1,6 +1,5 @@
var path = require('path');
var expect = require('expect.js');
var fs = require('fs');
var object = require('mout').object;
var helpers = require('../helpers');
var commands = helpers.require('lib/index').commands;
@@ -9,60 +8,40 @@ describe('bower install', function () {
var tempDir = new helpers.TempDir();
var package = new helpers.TempDir({
'bower.json': {
name: 'package'
}
}).prepare();
function bowerJson() {
var bowerJsonPath = path.join(
tempDir.path, 'bower_components', 'underscore', 'bower.json'
);
return JSON.parse(fs.readFileSync(bowerJsonPath));
}
var config = {
cwd: tempDir.path,
interactive: true
};
var install = function(options) {
options = options || {};
var install = function(packages, options, config) {
config = object.merge(config || {}, {
cwd: tempDir.path
});
var logger = commands.install(
options.packages, options.options, config
packages, options, config
);
return helpers.expectEvent(logger, 'end');
};
it.skip('installs a package', function () {
tempDir.prepare();
it('writes to bower.json if --save flag is used', function () {
package.prepare();
this.timeout(10000);
var logger = commands.install(['underscore'], undefined, config);
return helpers.expectEvent(logger, 'end')
.then(function () {
expect(bowerJson()).to.have.key('name');
tempDir.prepare({
'bower.json': {
name: 'test'
}
});
});
it.skip('installs package with --save flag', function () {
tempDir.prepare();
var logger = commands.install(['underscore'], {save: true}, config);
return helpers.expectEvent(logger, 'end')
.then(function () {
expect(bowerJson()).to.have.key('name');
return install([package.path], { save: true }).then(function() {
expect(tempDir.read('bower.json')).to.contain('dependencies');
});
});
it('reads .bowerrc from cwd', function () {
var package = new helpers.TempDir({
'bower.json': {
name: 'package'
},
foo: 'bar'
}).prepare();
package.prepare({ foo: 'bar' });
tempDir.prepare({
'.bowerrc': { directory: 'assets' },
@@ -79,4 +58,85 @@ describe('bower install', function () {
});
});
it('runs preinstall hook', function () {
package.prepare();
tempDir.prepare({
'bower.json': {
name: 'test',
dependencies: {
package: package.path
}
},
'.bowerrc': {
scripts: {
preinstall: 'bash -c "echo -n % > preinstall.txt"'
}
}
});
return install().then(function() {
expect(tempDir.read('preinstall.txt')).to.be('package');
});
});
it('runs preinstall hook', function () {
package.prepare();
tempDir.prepare({
'bower.json': {
name: 'test',
dependencies: {
package: package.path
}
},
'.bowerrc': {
scripts: {
postinstall: 'bash -c "echo -n % > postinstall.txt"'
}
}
});
return install().then(function() {
expect(tempDir.read('postinstall.txt')).to.be('package');
});
});
// To be discussed, but that's the implementation now
it('does not run hooks if nothing is installed', function () {
tempDir.prepare({
'bower.json': {
name: 'test'
},
'.bowerrc': {
scripts: {
postinstall: 'bash -c "echo -n % > hooks.txt"',
preinstall: 'bash -c "echo -n % > hooks.txt"'
}
}
});
return install().then(function() {
expect(tempDir.exists('hooks.txt')).to.be(false);
});
});
it('runs postinstall after bower.json is written', function () {
package.prepare();
tempDir.prepare({
'bower.json': {
name: 'test'
},
'.bowerrc': {
scripts: {
postinstall: 'bash -c "cat bower.json > hook.txt"',
}
}
});
return install([package.path], { save: true }).then(function() {
expect(tempDir.read('hook.txt')).to.contain('dependencies');
});
});
});

View File

@@ -6,11 +6,16 @@ var uuid = require('node-uuid');
var object = require('mout/object');
var fs = require('fs');
var object = require('mout/object');
var config = require('../lib/config');
exports.require = function (name) {
return require(path.join(__dirname, '../', name));
};
// We need to reset cache because tests are reusing temp directories
beforeEach(function () {
config.reset();
});
after(function () {
rimraf.sync(path.join(__dirname, 'tmp'));
@@ -34,7 +39,7 @@ exports.TempDir = (function() {
if (files) {
object.forOwn(files, function (contents, filepath) {
if (typeof contents === 'object') {
contents = JSON.stringify(contents, null, ' ');
contents = JSON.stringify(contents, null, ' ') + '\n';
}
var fullPath = path.join(that.path, filepath);
@@ -50,6 +55,10 @@ exports.TempDir = (function() {
return fs.readFileSync(path.join(this.path, name), 'utf8');
};
TempDir.prototype.exists = function (name) {
return fs.existsSync(path.join(this.path, name));
};
return TempDir;
})();