mirror of
https://github.com/bower/bower.git
synced 2026-02-11 22:44:58 -05:00
Use the new bower.json, refactor readJson func.
This commit is contained in:
@@ -170,45 +170,46 @@ Package.prototype._readJson = function (rc) {
|
||||
return Q.fcall(this._json);
|
||||
}
|
||||
|
||||
var jsonFile = path.join(this.getTempDir(), rc.json);
|
||||
var jsonFile;
|
||||
|
||||
// 1nd - Read json as string
|
||||
return Q.nfcall(fs.readFile, jsonFile)
|
||||
// 2nd - If successfull, parse and validate json
|
||||
// - If the file does not exist, make it an empty object
|
||||
function read(file) {
|
||||
jsonFile = file;
|
||||
return Q.nfcall(fs.readFile, path.join(this.getTempDir(), file));
|
||||
}
|
||||
|
||||
// Try package rc.json
|
||||
return read.call(this, rc.json)
|
||||
// Try bower.json
|
||||
.then(null, read.bind(this, 'bower.json'))
|
||||
// Try component.json (deprecated, remove later)
|
||||
.then(null, function () {
|
||||
return read.call(this, 'component.json')
|
||||
.then(function (contents) {
|
||||
this.emit('warn', 'Package "' + this.name + '" is using the deprecated component.json file');
|
||||
return contents;
|
||||
}.bind(this));
|
||||
}.bind(this))
|
||||
// If we got the file contents, validate them
|
||||
.then(function (contents) {
|
||||
return Q.fcall(function () {
|
||||
// TODO: change the read & validation to a separate package in the bower organization
|
||||
try {
|
||||
this._json = JSON.parse(contents);
|
||||
return this._json;
|
||||
} catch (e) {
|
||||
throw createError('Unable to parse local "' + this._rc.json + '" file', 'EINVJSON', {
|
||||
details: 'Unable to parse JSON file "' + jsonFile + '": ' + e.message
|
||||
});
|
||||
}
|
||||
});
|
||||
// TODO: change the validation to a separate module in the bower organization
|
||||
try {
|
||||
this._json = JSON.parse(contents);
|
||||
return this._json;
|
||||
} catch (e) {
|
||||
throw createError('Unable to parse local "' + path.basename(jsonFile) + '" file', 'EINVJSON', {
|
||||
details: 'Unable to parse JSON file "' + jsonFile + '": ' + e.message
|
||||
});
|
||||
}
|
||||
// Otherwise there was an error
|
||||
}.bind(this), function (err) {
|
||||
// At this point, there was an error reading the file
|
||||
// Throw if the error code is not ENOENT
|
||||
if (err.code !== 'ENOENT') {
|
||||
throw err;
|
||||
// If no json file was found, return an empty one
|
||||
if (err.code === 'ENOENT') {
|
||||
this._json = {};
|
||||
return {};
|
||||
}
|
||||
|
||||
// If the json was already the component.json,
|
||||
// simply assume an empty one
|
||||
if (rc.json === 'component.json') {
|
||||
this._json = {};
|
||||
return this._json;
|
||||
}
|
||||
// Otherwise, if the json is equal to the project's config
|
||||
// try the standard 'component.json'
|
||||
if (rc.json === config.json) {
|
||||
return this._readJson(mout.object.mixIn(rc, { json: 'component.json' }));
|
||||
}
|
||||
// Otherwise, the json was a custom defined one at the package level
|
||||
// try the project's config one
|
||||
return this._readJson(mout.object.mixIn(rc, { json: config.json }));
|
||||
// If we got here, the error code is something else so we throw it
|
||||
throw err;
|
||||
}.bind(this));
|
||||
};
|
||||
|
||||
|
||||
@@ -30,10 +30,10 @@ var proxy = process.env.HTTPS_PROXY
|
||||
var config;
|
||||
try {
|
||||
config = require('rc')('bower', {
|
||||
cwd: process.cwd(), // TODO: read working dir from the process argv, possibly using nopt
|
||||
cwd: process.cwd(),
|
||||
roaming: path.join(roaming, folder),
|
||||
json: 'component.json',
|
||||
directory: 'components',
|
||||
json: 'bower.json',
|
||||
directory: 'bower_components',
|
||||
proxy: proxy
|
||||
});
|
||||
} catch (e) {
|
||||
|
||||
Reference in New Issue
Block a user