Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add ability to install dependencies from remote repos

  • Loading branch information...
commit 4efa3540488b261ef1b72b44875a4668aba0dfd2 1 parent f2c74d3
@jeremyworboys jeremyworboys authored tj committed
Showing with 75 additions and 63 deletions.
  1. +11 −30 bin/component-install
  2. +64 −33 lib/Package.js
View
41 bin/component-install
@@ -140,7 +140,7 @@ function report(pkg, options) {
pkg.on('error', function(err){
if (404 != err.status) utils.fatal(err.stack);
- if (options.error) {
+ if (err.fatal) {
error(err.message);
process.exit(1);
}
@@ -180,37 +180,18 @@ process.on('exit', function(){
*/
function install(name, version) {
- var i = 0;
-
- next();
-
- function next() {
- var remote = conf.remotes[i++];
- if (!remote) return;
-
- // parse remote
- var last = i == conf.remotes.length - 1;
- remote = url.parse(remote);
-
- // strip trailing /
- remote.href = remote.href.slice(0, -1);
-
- // kick of installation
- var pkg = component.install(name, version, {
- dest: program.out,
- force: program.force,
- dev: program.dev,
- remote: remote.href
- });
+ // kick off installation
+ var pkg = component.install(name, version, {
+ dest: program.out,
+ force: program.force,
+ dev: program.dev,
+ remotes: conf.remotes
+ });
- // only error on the last remote otherwise
- // we assume it may be fetchable
- pkg.once('error', next);
- report(pkg, { error: !last });
+ report(pkg);
- // TODO: add callback
- pkg.install();
- }
+ // TODO: add callback
+ pkg.install();
}
/**
View
97 lib/Package.js
@@ -14,6 +14,7 @@ var Emitter = require('events').EventEmitter
, debug = require('debug')('component:installer')
, Batch = require('batch')
, http = require('http')
+ , url = require('url')
, fs = require('fs');
/**
@@ -46,7 +47,7 @@ function Package(pkg, version, options) {
if (!version) throw new Error('version required');
this.name = pkg;
this.dest = options.dest || 'components';
- this.remote = options.remote || 'https://raw.github.com';
+ this.remotes = options.remotes || ['https://raw.github.com'];
this.auth = options.auth;
this.force = !! options.force;
this.version = version;
@@ -92,7 +93,7 @@ Package.prototype.join = function(path){
*/
Package.prototype.url = function(file){
- return this.remote + '/' + this.name + '/' + this.version + '/' + file;
+ return this.remote.href + '/' + this.name + '/' + this.version + '/' + file;
};
/**
@@ -244,7 +245,8 @@ Package.prototype.getDependencies = function(deps, fn){
batch.push(function(done){
var pkg = new Package(name, version, {
dest: self.dest,
- force: self.force
+ force: self.force,
+ remotes: self.remotes
});
self.emit('dep', pkg);
pkg.on('end', done);
@@ -292,43 +294,72 @@ Package.prototype.install = function(){
Package.prototype.reallyInstall = function(){
var self = this;
- var batch = new Batch;
- this.getJSON(function(err, json){
- if (err) return self.emit('error', err);
-
- var files = [];
- if (json.scripts) files = files.concat(json.scripts);
- if (json.styles) files = files.concat(json.styles);
- if (json.templates) files = files.concat(json.templates);
- if (json.files) files = files.concat(json.files);
- if (json.images) files = files.concat(json.images);
- if (json.fonts) files = files.concat(json.fonts);
- json.repo = json.repo || self.remote + '/' + self.name;
-
- if (json.dependencies) {
+ var i = 0;
+ var batch;
+ var last;
+
+ next();
+
+ function next() {
+ self.remote = self.remotes[i++];
+ if (!self.remote) return self.emit('error', new Error('can\'t find remote for "' + self.name + '"'));
+
+ // parse remote
+ last = i == self.remotes.length - 1;
+ self.remote = url.parse(self.remote);
+
+ // strip trailing /
+ self.remote.href = self.remote.href.slice(0, -1);
+
+ // only error on the last remote otherwise
+ // we assume it may be fetchable
+ self.once('error', next);
+
+ // kick off installation
+ batch = new Batch;
+ self.getJSON(function(err, json){
+ if (err) {
+ err.fatal = !last;
+ return self.emit('error', err);
+ }
+
+ var files = [];
+ if (json.scripts) files = files.concat(json.scripts);
+ if (json.styles) files = files.concat(json.styles);
+ if (json.templates) files = files.concat(json.templates);
+ if (json.files) files = files.concat(json.files);
+ if (json.images) files = files.concat(json.images);
+ if (json.fonts) files = files.concat(json.fonts);
+ json.repo = json.repo || self.remote.href + '/' + self.name;
+
+ if (json.dependencies) {
+ batch.push(function(done){
+ self.getDependencies(json.dependencies, done);
+ });
+ }
+
batch.push(function(done){
- self.getDependencies(json.dependencies, done);
+ self.mkdir(self.dirname(), function(err){
+ json = JSON.stringify(json, null, 2);
+ self.writeFile('component.json', json, done);
+ });
});
- }
- batch.push(function(done){
- self.mkdir(self.dirname(), function(err){
- json = JSON.stringify(json, null, 2);
- self.writeFile('component.json', json, done);
+ batch.push(function(done){
+ self.mkdir(self.dirname(), function(err){
+ self.getFiles(files, done);
+ });
});
- });
- batch.push(function(done){
- self.mkdir(self.dirname(), function(err){
- self.getFiles(files, done);
+ batch.end(function(err){
+ if (err) {
+ err.fatal = !last;
+ return self.emit('error', err);
+ }
+ self.emit('end');
});
});
-
- batch.end(function(err){
- if (err) return self.emit('error', err);
- self.emit('end');
- });
- });
+ }
};
/**
Please sign in to comment.
Something went wrong with that request. Please try again.