mirror of
https://github.com/bower/bower.git
synced 2026-04-24 03:00:19 -04:00
Close GH-10: Find .bower.json if it exists.
This commit is contained in:
@@ -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;
|
||||
|
||||
5
packages/bower-json/test/pkg-dot-bower-json/.bower.json
Normal file
5
packages/bower-json/test/pkg-dot-bower-json/.bower.json
Normal file
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"name": "some-installed-pkg",
|
||||
"version": "0.0.1",
|
||||
"main": "bar.js"
|
||||
}
|
||||
@@ -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();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user