Use the new bower.json, refactor readJson func.

This commit is contained in:
André Cruz
2013-04-06 12:09:56 +01:00
parent 3d8c8a36dd
commit 408417bbda
4 changed files with 46 additions and 37 deletions

View File

@@ -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));
};

View File

@@ -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) {