Close GH-10: Find .bower.json if it exists.

This commit is contained in:
Nick Heiner
2013-08-05 14:10:57 +01:00
committed by André Cruz
parent 4cfa94d304
commit 8da47dcd00
3 changed files with 33 additions and 13 deletions

View File

@@ -3,6 +3,8 @@ var path = require('path');
var deepExtend = require('deep-extend');
var createError = require('./util/createError');
var possibleJsons = ['bower.json', 'component.json', '.bower.json'];
function read(file, options, callback) {
if (typeof options === 'function') {
callback = options;
@@ -101,25 +103,27 @@ function normalize(json) {
}
function find(folder, callback) {
var file = path.join(folder, 'bower.json');
fs.exists(file, function (exists) {
if (exists) {
return callback(null, file);
function findRec(fileNames) {
var err;
var fileName;
if (!fileNames.length) {
err = createError('bower-json: None of "' + possibleJsons.join('", "') + '" were found in ' + folder, 'ENOENT');
return callback(err);
}
file = path.resolve(path.join(folder, 'component.json'));
fs.exists(file, function (exists) {
fileName = path.resolve(path.join(folder, fileNames[0]));
fs.exists(fileName, function(exists) {
if (exists) {
return callback(null, file);
return callback(null, fileName);
}
var err = new Error('Neither bower.json nor component.json were found in ' + folder);
err.code = 'ENOENT';
callback(err);
findRec(fileNames.slice(1));
});
});
}
findRec(possibleJsons);
}
module.exports = read;

View File

@@ -0,0 +1,5 @@
{
"name": "some-installed-pkg",
"version": "0.0.1",
"main": "bar.js"
}

View File

@@ -25,11 +25,22 @@ describe('.find', function () {
});
});
it('should fallback to the .bower.json file', function(done) {
bowerJson.find(__dirname + '/pkg-dot-bower-json', function(err, file) {
if (err) {
return done(err);
}
expect(file).to.equal(path.resolve(__dirname + '/pkg-dot-bower-json/.bower.json'));
done();
});
})
it('should error if no component.json / bower.json is found', function (done) {
bowerJson.find(__dirname, function (err) {
expect(err).to.be.an(Error);
expect(err.code).to.equal('ENOENT');
expect(err.message).to.equal('Neither bower.json nor component.json were found in ' + __dirname);
expect(err.message).to.equal('bower-json: None of "bower.json", "component.json", ".bower.json" were found in ' + __dirname);
done();
});
});