diff --git a/.gitignore b/.gitignore index e43b0f9..9daa824 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ .DS_Store +node_modules diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..e7906ca --- /dev/null +++ b/.travis.yml @@ -0,0 +1,10 @@ +language: node_js +node_js: +- '0.10' +branches: + only: + - master +notifications: + email: + recipients: + - i@2fd.me diff --git a/package.json b/package.json new file mode 100644 index 0000000..41999e3 --- /dev/null +++ b/package.json @@ -0,0 +1,26 @@ +{ + "name": "registry", + "version": "0.0.0", + "description": "The jspm registry and package.json override service", + "scripts": { + "test": "node test" + }, + "repository": { + "type": "git", + "url": "https://github.com/jspm/registry.git" + }, + "bugs": { + "url": "https://github.com/jspm/registry/issues" + }, + "homepage": "https://github.com/jspm/registry", + "private": true, + "dependencies": { + "colors": "^1.0.3", + "commander": "^2.7.1", + "glob": "^5.0.3", + "jspm-github": "git://github.com/jspm/github", + "jspm-npm": "git://github.com/jspm/npm", + "mout": "^0.11.0", + "q": "^1.2.0" + } +} diff --git a/test/index.js b/test/index.js new file mode 100644 index 0000000..288aa6f --- /dev/null +++ b/test/index.js @@ -0,0 +1,78 @@ +"use strict"; + +var log = require('./lib/log'); +var RegistryFile = require('./lib/RegistryFile'); +var OverrideFile = require('./lib/OverrideFile'); +var summary = []; + +/** + * Commander + */ +var program = require('commander'); +program + .version('0.0.1') + .usage('[options] ') + .option('-e, --only-errors', 'Show only summary', 0) + .parse(process.argv); + + +/** + * registry.json + */ +var registry = RegistryFile(); +if(registry.errors().length) +{ + summary = summary.concat(registry.errors()); + log.error(registry.errors()); + process.exit(1); +} + +// log ok +if(!program.onlyErrors) + log.ok('registry.json'); + + +/** + * Packages + */ +registry.forEach(function(endpoint, name){ + + if(program.args.length && program.args.indexOf(name) === -1) + return; + + var override = new OverrideFile(endpoint); + + if(override.errors().length) { + + var errorDescription = name + ' => ' + endpoint + '\n\t' + override.errors().join('\n\t'); + summary.push(errorDescription); + + } else if(!program.onlyErrors) { + log.ok(name + ' => ' + endpoint); + + if(!override.isFile()) + log.info(' not override file from ' + name); + } + +}); + + +/** + * Summary + */ +if(summary.length) { + log.n() + ('Error summary ................') + .error(summary) + .n(); + + process.exit(1); + +} + +log.n() + ('Summary ................') + .ok('excellent!! all is well.') + .n(); + +process.exit(0); \ No newline at end of file diff --git a/test/lib/JsonFile.js b/test/lib/JsonFile.js new file mode 100644 index 0000000..708f565 --- /dev/null +++ b/test/lib/JsonFile.js @@ -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; +}; + + + diff --git a/test/lib/OverrideFile.js b/test/lib/OverrideFile.js new file mode 100644 index 0000000..16de9fc --- /dev/null +++ b/test/lib/OverrideFile.js @@ -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; +}; + diff --git a/test/lib/RegistryFile.js b/test/lib/RegistryFile.js new file mode 100644 index 0000000..ad26846 --- /dev/null +++ b/test/lib/RegistryFile.js @@ -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); \ No newline at end of file diff --git a/test/lib/log.js b/test/lib/log.js new file mode 100644 index 0000000..e6332bb --- /dev/null +++ b/test/lib/log.js @@ -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;