Permalink
Fetching contributors…
Cannot retrieve contributors at this time
executable file 133 lines (112 sloc) 3.38 KB
#!/usr/bin/env node
var program = require('commander');
/**
* Options.
*/
program
.usage('[name ...]')
.option('-d, --dev', 'install development dependencies')
.option('-p, --proxy', 'use a proxy, eg. http://localhost:3128')
.option('-t, --timeout <in ms>', 'use a timeout for remotes, default: 5000ms', 5000)
.option('--debug', 'turn on debug statements')
// .option('-p, --proxy <proxy>', 'use a proxy, eg. http://localhost:3128')
.parse(process.argv);
/**
* Examples.
*/
program.on('--help', function(){
console.log(' Examples:');
console.log();
console.log(' # install deps in ./component.json');
console.log(' $ component install');
console.log();
console.log(' # install a single component');
console.log(' $ component install component/tip');
console.log();
console.log(' # install several components');
console.log(' $ component install component/tip component/popover');
console.log();
console.log(' # install a specific tag / sha');
console.log(' $ component install component/tip@8bf1ffc3f');
console.log();
console.log(' # install regular & development deps in .');
console.log(' $ component install -d');
console.log();
process.exit();
});
if (program.debug) require('debug').enable('component-consoler*,component-resolver*');
var resolve = require('component-resolver');
var utils = require('component-consoler');
var semver = require('semver');
var path = require('path');
var fs = require('fs');
var exists = fs.existsSync;
var write = fs.writeFileSync;
var fatal = utils.fatal;
var log = utils.log;
var dependencies = program.args;
var jsonPath = path.resolve('component.json');
var hasJson = exists(jsonPath);
// whitespace
console.log();
process.on('exit', function(){
console.log();
});
/**
* Installer.
*/
var options = {
development: program.dev,
proxy: program.proxy,
timeout: program.timeout,
install: true,
verbose: true,
};
// Install everything in the dependency tree
if (!dependencies.length) {
if (!hasJson) fatal('missing component.json');
resolve(process.cwd(), options, function (err) {
if (err) fatal(err);
log('install', 'complete');
});
return;
}
var json = hasJson && require(jsonPath);
var deps = {};
dependencies = dependencies.map(function (dependency) {
var frags = dependency.split('@');
var name = frags[0].toLowerCase();
if (!~name.indexOf('/')) utils.fatal('"' + name + '" is not a valid dependency');
var version = frags[1];
deps[name] = version || '*';
return [name, version];
});
resolve({
dependencies: deps
}, options, function (err, tree) {
if (err) fatal(err);
log('install', 'complete');
if (!json) return;
json.dependencies = json.dependencies || {};
dependencies.forEach(function (dep) {
var name = dep[0];
var version = dep[1];
// if the dependency, don't install.
// not sure if this is correct behavior.
if (json.dependencies[name]) return;
// if we're installing "any" version,
// add a corresponding ^ or ~ to the current version
if (!version) {
var node = tree.dependencies[name];
if (!node.version) {
version = '*'; // this means master
} else if (semver.gte(node.version, '1.0.0')) {
version = '^' + node.version;
} else {
version = '~' + node.version;
}
}
json.dependencies[name] = version;
});
write(jsonPath, JSON.stringify(json, null, 2));
})