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
This commit is contained in:
André Cruz
2013-05-26 13:47:13 +01:00
parent c8d4d39068
commit 005b3356f2
10 changed files with 55 additions and 51 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -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'

View File

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

View File

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