You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
47 lines
1.7 KiB
47 lines
1.7 KiB
'use strict';
|
|
|
|
var _ = require('lodash');
|
|
var fs = require('fs');
|
|
|
|
var defineElement = function(fullName, baseClass, message) {
|
|
return fullName + ' = function() {\n' +
|
|
' this.message = ' + message + ';\n' +
|
|
' this.stack = this.message + \'\\n\' + (new Error()).stack;\n' +
|
|
'};\n' +
|
|
fullName + '.prototype = Object.create(' + baseClass + '.prototype);\n' +
|
|
fullName + '.prototype.name = "' + fullName + '";\n\n';
|
|
};
|
|
|
|
var traverseNode = function(baseClass, errorDefinition) {
|
|
var className = baseClass + '.' + errorDefinition.name;
|
|
var generated = defineElement(className, baseClass, errorDefinition.message);
|
|
if (errorDefinition.errors) {
|
|
generated += childDefinitions(className, errorDefinition.errors);
|
|
}
|
|
return generated;
|
|
};
|
|
|
|
/* jshint latedef: false */
|
|
var childDefinitions = function(parent, childDefinitions) {
|
|
var generated = '';
|
|
_.each(childDefinitions, function(childDefinition) {
|
|
generated += traverseNode(parent, childDefinition);
|
|
});
|
|
return generated;
|
|
};
|
|
/* jshint latedef: true */
|
|
|
|
var traverseRoot = function(errorsDefinition) {
|
|
var fullName = 'bitcore.Error';
|
|
var path = 'Error';
|
|
var generated = '\'use strict\';\n\nvar inherits = require(\'inherits\');\n\n';
|
|
generated += '/** AUTOGENERATED FILE. DON\'T EDIT, MODIFY "lib/errors/spec.js" INSTEAD */\n\n';
|
|
generated += 'var bitcore = {};\n\n';
|
|
generated += defineElement(fullName, path, '\'Internal error\'');
|
|
generated += childDefinitions(fullName, errorsDefinition);
|
|
generated += 'module.exports = bitcore.Error;\n';
|
|
return generated;
|
|
};
|
|
|
|
var data = require('./spec');
|
|
fs.writeFileSync(__dirname + '/index.js', traverseRoot(data));
|
|
|