diff --git a/README.md b/README.md index dfdb61e7..7a6da0f0 100644 --- a/README.md +++ b/README.md @@ -64,6 +64,7 @@ Options: - name - the package name (if none is passed, one will be guessed from the endpoint) - range - a valid semver range (defaults to *) - unitOfWork - the unit of work to use (if none is passed, one will be created) +- config - the config to use (defaults to the global config) ------------ @@ -88,7 +89,6 @@ The resolve process obeys a very explicit flow: - Enqueues the package to be resolved in the unit of work and waits - When accepted calls #_createTempDir and waits - When done, calls #_resolveSelf and waits -- When done, calls #_readRc and waits - When done, calls #_readJson and waits - When done, calls #_parseJson and waits - When done, marks the package as resolved and informs the unit of work @@ -120,10 +120,7 @@ Protected functions #### Package#_createTempDir() -> Promise Creates a temporary dir. -#### Package#_readRc() -> Promise -Reads the local .bowerrc configuration. - -#### Package#_readJson(rc) -> Promise +#### Package#_readJson() -> Promise Reads the package component.json, possibly by using a dedicated `read-json` package that will be available in the bower organization. It will ensure everything is valid. ### Package#_parseJson(json) -> Promise diff --git a/lib/core/Package.js b/lib/core/Package.js index dfe0fe7a..e6263bda 100644 --- a/lib/core/Package.js +++ b/lib/core/Package.js @@ -15,9 +15,10 @@ var Package = function (endpoint, options) { this._endpoint = endpoint; this._name = options.name; - this._explicitName = !!this.name; + this._guessedName = !this.name; this._range = options.range || '*'; this._unitOfWork = options.unitOfWork || new UnitOfWork(); + this._config = options.config || config; }; util.inherits(Package, events.EventEmitter); @@ -53,13 +54,11 @@ Package.prototype.resolve = function () { return this._createTempDir() // 3nd - Resolve self .then(this._resolveSelf.bind(this)) - // 4th - Read local rc - .then(this._readRc.bind(this)) - // 5th - Read json + // 4th - Read json .then(this._readJson.bind(this)) - // 6th - Parse json + // 5th - Parse json .then(this._parseJson.bind(this)) - // 7th - Mark as resolved & call done + // 6th - Mark as resolved & call done // to inform the unit of work .then(function (dependencies) { this._resolved = true; @@ -70,7 +69,7 @@ Package.prototype.resolve = function () { done(); throw err; }.bind(this)) - // 8th - Resolve dependencies + // 7th - Resolve dependencies .then(this._resolveDependencies.bind(this)); }.bind(this), function (err) { // If error is of a duplicate package, @@ -129,40 +128,7 @@ Package.prototype._createTempDir = function () { }.bind(this)); }; -Package.prototype._readRc = function () { - console.log('_readRc'); - - // Resolved if cached - if (this._rc) { - return Q.fcall(this._rc); - } - - var rcFile = path.join(this.getTempDir(), '.bowerrc'); - - // 1nd - Read rc as string - return Q.nfcall(fs.readFile, rcFile) - // 2nd - If successfull, parse it as json - // - If the file does not exist, make it the global config - .then(function (contents) { - try { - this._rc = JSON.parse(contents); - return this._rc; - } catch (e) { - throw createError('Unable to parse local ".bowerrc" file', 'EINVJSON', { - details: 'Unable to parse JSON file "' + rcFile + '": ' + e.message - }); - } - }.bind(this), function (err) { - // If the file does not exist, return the global config - if (err.code === 'ENOENT') { - return config; - } - - throw err; - }); -}; - -Package.prototype._readJson = function (rc) { +Package.prototype._readJson = function () { console.log('_readJson'); // Resolve if cached @@ -172,18 +138,18 @@ Package.prototype._readJson = function (rc) { var jsonFile; - 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') + jsonFile = path.join(this.getTempDir(), 'bower.json'); + return Q.nfcall(fs.readFile, jsonFile) + // Try component.json + .then(null, function (err) { + if (err.code !== 'ENOENT') { + throw err; + } + + jsonFile = path.join(this.getTempDir(), 'component.json'); + return Q.nfcall(fs.readFile, jsonFile) + // Issue a deprecation message if it exists .then(function (contents) { this.emit('warn', 'Package "' + this.name + '" is using the deprecated component.json file'); return contents; @@ -196,19 +162,19 @@ Package.prototype._readJson = function (rc) { 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 + throw createError('Unable to parse "' + jsonFile + '" file', e.code, { + details: e.message }); } // Otherwise there was an error }.bind(this), function (err) { - // If no json file was found, return an empty one + // If no json file was found, return one just with the name if (err.code === 'ENOENT') { this._json = { name: this.name }; - return {}; + return this._json; } - // If we got here, the error code is something else so we throw it + // If we got here, the error code is something else so we re-throw it throw err; }.bind(this)); }; @@ -221,19 +187,19 @@ Package.prototype._parseJson = function (json) { return Q.fcall(this._dependencies); } - // 1st - Check if name defined in the json is different - // Only handle it if the package name was not explicitly set - if (!this._explicitName && json.name !== this.name) { + // Check if name defined in the json is different + // If so and if the name was "guessed", assume the json name + if (this._guessedName && json.name !== this.name) { this.name = json.name; this.emit('name_change', this.name); } - // 2nd - Handle ignore property + // Handle ignore property, deleting all files from the temporary directory return Q.fcall(function () { // Delete all the files specified in the ignore from the temporary directory // TODO: }.bind(this)) - // 3rd - Handle the dependencies property + // Handle the dependencies property .then(function () { var key, promises = []; @@ -246,7 +212,7 @@ Package.prototype._parseJson = function (json) { } } - // Since the create package actually returns a promise, we must resolve them all + // Resolve all the create packages promises return Q.all(promises).then(function (packages) { this._dependencies = packages; return packages;