This commit is contained in:
Fede Ramirez
2015-03-19 05:11:58 -03:00
parent 42b00aeb14
commit 8bdeccca64
8 changed files with 333 additions and 0 deletions
+56
View File
@@ -0,0 +1,56 @@
"use strict;"
var fs = require('fs');
var mout = require('mout');
var extend = mout.object.deepMixIn;
var forOwn = mout.object.forOwn;
/**
* @constructor JsonFile
*/
var JsonFile = module.exports = function JsonFile(file){
if( !(this instanceof JsonFile) )
return new JsonFile(file);
this._file = String(file);
this._errors = this._errors || [];
this._json = this._json || {};
if( !(/\.json$/.test(file)) ){
this.error(this._file + ' is not json file');
} else {
try{
extend(this._json, require(this._file));
} catch(e) {
this.error(e.message);
}
}
};
JsonFile.prototype.error = function(msg){
this._errors.push(msg);
return this;
}
JsonFile.prototype.errors = function(){
return this._errors;
}
JsonFile.prototype.forEach = function(fn){
forOwn(this._json, fn, this);
return this;
};
+88
View File
@@ -0,0 +1,88 @@
"use strict;"
var fs = require('fs');
var util = require('util');
var path = require('path');
var glob = require('glob');
var mout = require('mout');
var colors = require('colors/safe');
var JsonFile = require('./JsonFile');
var registries = {
github: require('jspm-github'),
npm: require('jspm-npm')
};
/**
* @constructor OverrideFile
*/
var OverrideFile = module.exports = function OverrideFile(endpoint){
if( !(this instanceof OverrideFile) )
return new OverrideFile(endpoint);
this._endpoint = endpoint;
this._files = [];
this._errors = this._errors || [];
this._registry;
this._package;
var match = /^([^:\s]*):([^\s]*)$/.exec(endpoint);
if(!match)
{
this.error('invalid enpoint package');
}
else
{
this._registry = match[1];
this._package = match[2];
if(!registries[this._registry])
{
this.error('invalid registry');
}
else if(!registries[this._registry].packageFormat.test(this._package))
{
this.error('invalid pacakge name');
}
this._glob = path.resolve('./package-overrides', this._registry) + '/' + this._package + '@*.json';
this._files = glob.sync(this._glob);
this.forEach(function(file){
var err = JsonFile(file).errors();
if(err.length > 0)
this.error(JsonFile(file).errors().join('\n'));
});
}
};
util.inherits(OverrideFile, JsonFile);
OverrideFile.prototype.error = function(msg){
var result = '';
result += this._endpoint ? colors.gray(this._endpoint) + ' ' : '';
result += msg;
return JsonFile.prototype.error.call(this, result);
};
OverrideFile.prototype.forEach = function(fn){
mout.array.forEach(this._files, fn, this);
return this;
};
OverrideFile.prototype.isFile = function(){
return this._files.length > 0;
};
+19
View File
@@ -0,0 +1,19 @@
"use strict;"
var util = require('util');
var path = require('path');
var JsonFile = require('./JsonFile');
/**
* @constructor RegistryFile
*/
var RegistryFile = module.exports = function RegistryFile(){
if( !(this instanceof RegistryFile) )
return new RegistryFile();
JsonFile.call(this, path.resolve('./registry.json'));
};
util.inherits(RegistryFile, JsonFile);
+55
View File
@@ -0,0 +1,55 @@
"use strict";
var util = require('util');
var mout = require('mout');
var colors = require('colors/safe');
var log = function(msg, color){
if(colors[color])
msg = colors[color](msg);
console.log( msg );
return log;
};
log.ok = function(msg){
if(Array.isArray(msg))
{
msg.forEach(log.ok);
return log;
}
return log( util.format( '✓ %s', msg), 'green');
};
log.error = function(msg){
if(Array.isArray(msg))
{
msg.forEach(log.error);
return log;
}
return log(util.format( '✗ %s', msg), 'red');
};
log.info = function(msg){
if(Array.isArray(msg))
{
msg.forEach(log.info);
return log;
}
return log(util.format( ' %s', msg), 'cyan');
};
log.n = function(count){
count = mout.lang.isNumber(count) && count > 0 ? mout.number.toInt(count) : 0;
return log(mout.string.repeat("\n", count));
}
module.exports = log;