From 005b3356f2a84745a71ec0614f329cc3089e722d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andre=CC=81=20Cruz?= Date: Sun, 26 May 2013 13:47:13 +0100 Subject: [PATCH] Several things changed. - Make force and offline part of config, clearing all the options mess in the architecture - Fix some bugs with the renderer - Parse cli options --- NOTES.md | 1 + bin/bower_new | 9 +++------ lib/commands/install.js | 11 +++++++---- lib/config.js | 4 +++- lib/core/Manager.js | 8 +++----- lib/core/PackageRepository.js | 26 ++++++++++++-------------- lib/core/Project.js | 15 +++++++-------- lib/core/resolverFactory.js | 17 +++++++---------- lib/renderers/cli.js | 6 ++++-- lib/util/cli.js | 9 ++++++++- 10 files changed, 55 insertions(+), 51 deletions(-) diff --git a/NOTES.md b/NOTES.md index 6395030c..8621165d 100644 --- a/NOTES.md +++ b/NOTES.md @@ -53,6 +53,7 @@ - switch everything related with fs. to .graceful-fs - don't forget to do the same on all bower org modules - use dependency injection more? we are passing a lot of options around for deep arch components.. +- look at https://github.com/bower/bower/issues/475 Not BC changes: - shorthand_resolver -> shorthandResolver diff --git a/bin/bower_new b/bin/bower_new index c8e9f093..02e14e3d 100755 --- a/bin/bower_new +++ b/bin/bower_new @@ -29,9 +29,9 @@ if (options.version) { process.exit(); } -// Parse log levels -if (mout.object.hasOwn(options, 'log-levels')) { - levels = (options['log-levels'] || '') +// Parse log level +if (options.logLevel) { + levels = options.logLevel .split(',') .map(function (level) { return level.trim(); }); @@ -76,9 +76,6 @@ command.line(process.argv) renderer.end(data); }) .on('error', function (err) { - err.label = 'error'; - err.tag = err.code ? err.code.toLowerCase() : 'error'; - renderer.error(err); process.exit(1); }); diff --git a/lib/commands/install.js b/lib/commands/install.js index 573c681f..6daa3c2a 100644 --- a/lib/commands/install.js +++ b/lib/commands/install.js @@ -1,21 +1,24 @@ var Emitter = require('events').EventEmitter; +var mout = require('mout'); var Project = require('../core/Project'); var cli = require('../util/cli'); var help = require('./help'); +var defaultConfig = require('../config'); -function install(endpoints, options) { +function install(endpoints, options, config) { var project; var emitter = new Emitter(); options = options || {}; + config = mout.object.deepMixIn(config, defaultConfig); // If endpoints are an empty array, null them if (endpoints && !endpoints.length) { endpoints = null; } - project = new Project(options); - project.install(endpoints) + project = new Project(config); + project.install(endpoints, options) .then(function (installed) { emitter.emit('end', installed); }, function (error) { @@ -30,7 +33,7 @@ function install(endpoints, options) { // ------------------- install.line = function (argv) { - var options = module.exports.options(argv); + var options = install.options(argv); if (options.help) { return help('install'); diff --git a/lib/config.js b/lib/config.js index c29a9eb8..9271af96 100644 --- a/lib/config.js +++ b/lib/config.js @@ -44,7 +44,9 @@ try { 'strict-ssl': true, 'user-agent': 'node/' + process.version + ' ' + process.platform + ' ' + process.arch, 'color': true, - 'git': 'git' + 'git': 'git', + 'force': false, + 'offline': false }); } catch (e) { throw new Error('Unable to parse runtime configuration: ' + e.message); diff --git a/lib/core/Manager.js b/lib/core/Manager.js index a033f1ee..be46691a 100644 --- a/lib/core/Manager.js +++ b/lib/core/Manager.js @@ -10,11 +10,9 @@ var copy = require('../util/copy'); var createError = require('../util/createError'); var endpointParser = require('../util/endpointParser'); -function Manager(options) { - options = options || {}; - - this._config = options.config || defaultConfig; - this._repository = new PackageRepository(options); +function Manager(config) { + this._config = config || defaultConfig; + this._repository = new PackageRepository(config); } // ----------------- diff --git a/lib/core/PackageRepository.js b/lib/core/PackageRepository.js index b38921dd..4e7ccd5e 100644 --- a/lib/core/PackageRepository.js +++ b/lib/core/PackageRepository.js @@ -6,19 +6,17 @@ var resolverFactory = require('./resolverFactory'); var defaultConfig = require('../config'); var createError = require('../util/createError'); -function PackageRepository(options) { - options = options || {}; - options.config = options.config || defaultConfig; +function PackageRepository(config) { + var registryOptions; - this._options = options; - this._config = options.config; + this._config = config || defaultConfig; - // Instantiate the registry and store it in the options object - // because it will be passed to the resolver factory - this._options.registryClient = new RegistryClient(mout.object.fillIn({ - cache: this._config.roaming.registry - }, this._config)); + // Instantiate the registry + registryOptions = mout.object.deepMixIn({}, this._config); + registryOptions.cache = this._config.roaming.registry; + this._registryClient = new RegistryClient(registryOptions); + // Instantiate the resolve cache this._resolveCache = new ResolveCache(this._config.roaming.cache); } @@ -30,7 +28,7 @@ PackageRepository.prototype.fetch = function (decEndpoint) { var that = this; // Get the appropriate resolver - resolverFactory(decEndpoint, this._options) + resolverFactory(decEndpoint, this._registryClient, this._config) // Decide if we retrieve from the cache or not // Also decide we if validate the cached entry or not .then(function (resolver) { @@ -40,7 +38,7 @@ PackageRepository.prototype.fetch = function (decEndpoint) { decEndpoint.resolverName = resolver.getName(); // If force flag is used, bypass cache - if (that._options.force) { + if (that._config.force) { deferred.notify({ level: 'action', tag: 'resolve', @@ -57,7 +55,7 @@ PackageRepository.prototype.fetch = function (decEndpoint) { // If there's no package in the cache if (!canonicalPkg) { // And the offline flag is passed, error out - if (that._options.offline) { + if (that._config.offline) { throw createError('No cached version for ' + resolver.getSource() + '#' + resolver.getTarget(), 'ENOCACHE', { resolver: resolver }); @@ -86,7 +84,7 @@ PackageRepository.prototype.fetch = function (decEndpoint) { }); // If offline flag is used, use directly the cached one - if (that._options.offline) { + if (that._config.offline) { return [canonicalPkg, pkgMeta]; } diff --git a/lib/core/Project.js b/lib/core/Project.js index bfbdf075..813de548 100644 --- a/lib/core/Project.js +++ b/lib/core/Project.js @@ -9,17 +9,14 @@ var defaultConfig = require('../config'); var createError = require('../util/createError'); var endpointParser = require('../util/endpointParser'); -function Project(options) { - options = options || {}; - - this._options = options; - this._config = options.config || defaultConfig; - this._manager = new Manager(options); +function Project(config) { + this._config = config || defaultConfig; + this._manager = new Manager(this._config); } // ----------------- -Project.prototype.install = function (endpoints) { +Project.prototype.install = function (endpoints, options) { var repairResult; var that = this; @@ -86,6 +83,8 @@ Project.prototype.install = function (endpoints) { .fin(function () { that._working = false; }); + + // TODO: handle save saveDev production }; Project.prototype.update = function (names) { @@ -125,7 +124,7 @@ Project.prototype._analyse = function () { // that is, the parent-child relationships this._restoreNode(root, flattened); // Do the same for the dev dependencies - if (!this._options.production) { + if (!this._config.production) { this._restoreNode(root, flattened, 'devDependencies'); } diff --git a/lib/core/resolverFactory.js b/lib/core/resolverFactory.js index 098f1d21..763c57cc 100644 --- a/lib/core/resolverFactory.js +++ b/lib/core/resolverFactory.js @@ -6,19 +6,18 @@ var resolvers = require('./resolvers'); var defaultConfig = require('../config'); var createError = require('../util/createError'); -function createResolver(decEndpoint, options) { +function createResolver(decEndpoint, registryClient, config) { var resOptions; var source = decEndpoint.source; var resolvedPath; - options = options || {}; - options.config = options.config || defaultConfig; + config = config || defaultConfig; // Setup resolver options resOptions = { target: decEndpoint.target, name: decEndpoint.name, - config: options.config + config: config }; // Git case: git git+ssh, git+http, git+https @@ -38,7 +37,7 @@ function createResolver(decEndpoint, options) { } // Check if source is a git repository - resolvedPath = path.resolve(options.config.cwd, source); + resolvedPath = path.resolve(config.cwd, source); // Below we try a series of asyc tests to guess the type of resolver to use // If a step was unable to guess the resolver, it throws an error @@ -69,7 +68,7 @@ function createResolver(decEndpoint, options) { var parts = source.split('/'); if (parts.length === 2) { - source = mout.string.interpolate(options.config.shorthandResolver, { + source = mout.string.interpolate(config.shorthandResolver, { shorthand: source, owner: parts[0], package: parts[1] @@ -84,14 +83,12 @@ function createResolver(decEndpoint, options) { }) // As last resort, we try the registry .fail(function (err) { - var registry = options.registryClient; - - if (!registry) { + if (!registryClient) { throw err; } return function () { - return Q.nfcall(registry.lookup.bind(registry), source, options) + return Q.nfcall(registryClient.lookup.bind(registryClient), source) .then(function (entry) { decEndpoint.registryName = source; // TODO: Handle entry.type.. for now it's only 'alias' diff --git a/lib/renderers/cli.js b/lib/renderers/cli.js index 33e05519..81d0c1f0 100644 --- a/lib/renderers/cli.js +++ b/lib/renderers/cli.js @@ -50,12 +50,14 @@ function renderTagPlusLabel(data) { // ------------------------- colorful = { - begin: function () {}, + begin: function () {}, end: function () {}, error: function (err) { var str; - str = 'bower ' + renderTagPlusLabel(err) + ' ' + (err.code ? err.code + ' ,' : '') + err.message + '\n'; + err.tag = err.code || 'error'; + err.level = 'error'; + str = 'bower ' + renderTagPlusLabel(err) + ' ' + err.message + '\n'; // Check if additional details were provided if (err.details) { diff --git a/lib/util/cli.js b/lib/util/cli.js index 843e9c13..76863b73 100644 --- a/lib/util/cli.js +++ b/lib/util/cli.js @@ -5,6 +5,7 @@ var renderers = require('../renderers'); function readOptions(argv, options) { var types; var shorthands = {}; + var parsedOptions = {}; // Configure options that are common to all commands options.help = { type: Boolean, shorthand: 'h' }; @@ -20,7 +21,13 @@ function readOptions(argv, options) { shorthands[option.shorthand] = '--' + name; }); - return nopt(types, shorthands, argv); + options = nopt(types, shorthands, argv); + + mout.object.forOwn(options, function (value, key) { + parsedOptions[mout.string.camelCase(key)] = value; + }); + + return parsedOptions; } function getRenderer(options) {