diff --git a/README.md b/README.md index be7eecd0..10ee930b 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,7 @@ Think of it as an abstract class that implements the package interface as well a - name_change (fired when the name of the package has changed) - action (fired to inform the current action being performed by the package) +- warn (fired to inform a warning, e.g.: deprecation) ------------ diff --git a/TODO.md b/TODO.md new file mode 100644 index 00000000..2bf9df91 --- /dev/null +++ b/TODO.md @@ -0,0 +1,7 @@ +TODO list: + +- config + - Fix nodejs 0.10.x issue due to a bug in the `rc` package. I've already submited a [PR](https://github.com/dominictarr/config-chain/pull/11) + - Allow that `config.cwd` to be changed by an argument when using the CLI. Two ways of doing this: + - Read a --cwd or similar and change the `config.cwd` to it + - Allow any arbitrary `config.*` to be changed with --config.* arguments diff --git a/lib/core/Package.js b/lib/core/Package.js index 95f957c2..e61d2002 100644 --- a/lib/core/Package.js +++ b/lib/core/Package.js @@ -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)); }; diff --git a/lib/core/config.js b/lib/core/config.js index b2e76ec3..58ec4ea5 100644 --- a/lib/core/config.js +++ b/lib/core/config.js @@ -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) {