save point durring huge unorganized refactor

This commit is contained in:
Spencer Alger
2013-11-22 16:48:30 -07:00
parent 5bb70fbe58
commit 97ba084795
80 changed files with 46126 additions and 2410 deletions

View File

@ -95,9 +95,13 @@ function transformFile(entry) {
});
});
spec.urls = _.map(_.sortBy(urls, 'sortOrder'), function (url) {
return _.omit(url, 'sortOrder');
});
if (urls.length > 1) {
spec.urls = _.map(_.sortBy(urls, 'sortOrder'), function (url) {
return _.omit(url, 'sortOrder');
});
} else {
spec.url = urls[0];
}
spec.params = _.transform(spec.params, function (note, param, name) {
// param.name = name;
@ -106,6 +110,10 @@ function transformFile(entry) {
]);
}, {});
if (_.size(spec.params) === 0) {
delete spec.params;
}
// escape method names with "special" keywords
var location = spec.name.split('.').join('.prototype.')
.replace(/(^|\.)(delete|default)(\.|$)/g, '[\'$2\']');
@ -114,6 +122,7 @@ function transformFile(entry) {
spec: _.pick(spec, [
'methods',
'params',
'url',
'urls',
'needBody',
'bulkBody',
@ -126,6 +135,61 @@ function transformFile(entry) {
allParams: allParams
};
function hasMethod(/* ...methods */) {
for (var i = 0; i < arguments.length; i++) {
if (~action.spec.methods.indexOf(arguments[i])) {
continue;
} else {
return false;
}
}
return true;
}
function methodsAre(/* ...methods */) {
return hasMethod.apply(null, arguments) && arguments.length === action.spec.methods.length;
}
var method;
if (action.spec.methods.length === 1) {
method = action.spec.methods[0];
} else {
// we need to define what the default method(s) will be
if (hasMethod('DELETE', 'POST')) {
method = 'POST';
}
else if (methodsAre('DELETE')) {
method = 'DELETE';
}
else if (methodsAre('POST', 'PUT')) {
method = 'POST';
}
else if (methodsAre('GET', 'POST')) {
method = 'POST';
}
else if (methodsAre('GET', 'HEAD')) {
if (action.spec.castExists) {
method = 'HEAD';
} else {
method = 'GET';
}
}
}
if (method) {
if (method !== 'GET') {
action.spec.method = method;
}
delete action.spec.methods;
} else {
throw new Error('unable to pick a method for ' + JSON.stringify(action, null, ' '));
}
if (action.name === 'create') {
action.proxy = 'index';
action.transformBody = 'params.op_type = \'create\';';
}
if (actions.push(action) === specCount && doneParsing) {
module.exports.emit('ready', action);
}

View File

@ -17,12 +17,21 @@ function download() {
}
}));
// seperate the proxy actions
var groups = _.groupBy(actions, function (action) {
return action.proxy ? 'proxies' : 'normal';
});
clean(outputPath);
console.log('writing', actions.length, 'api actions to', outputPath);
fs.writeFileSync(outputPath, templates.apiFile({
actions: actions,
actions: groups.normal,
proxies: groups.proxies,
namespaces: _.unique(namespaces.sort(), true)
}));
fs.writeFileSync(docOutputPath, templates.apiDocs({
actions: actions
}));
@ -30,7 +39,7 @@ function download() {
}
restSpecUpdated(function (err, updated) {
if (process.env.FORCE_GEN || err || updated) {
if (process.env.FORCE_GEN || process.env.npm_config_force || err || updated) {
download();
}
});

View File

@ -1,73 +0,0 @@
var _ = require('<%= path2lib %>utils');
var errors = require('<%= path2lib %>errors');<%
if (_.keys(enumOptions).length) {
%>
<% _.each(enumOptions, function(options, name) {
%>
var <%= name %>Options = <%= stringify(options) %>;<%
});
}
%>
/**
* Perform an elasticsearch [<%= name %>](<%= docUrl %>) request
*
* @for Client
* @method <%= name %>
* @param {Object} params - An object with parameters used to carry out this action<% _.each(params, function(param, paramName) { %>
* @param {<%= paramType(param.type) %>} <%= paramWithDefault('params.' + paramName, param.default) %><% if (param.description) { %> - <%= param.description %><% } %><%
})
%>
*/
function do<%= _.studlyCase(name) %>(params, cb) {
if (typeof params === 'function') {
cb = params;
params = {};
} else {
params = params || {};
cb = typeof cb === 'function' ? cb : _.noop;
}
var request = {
<%= writeRequestObjectBody(4, name, body, methods) %>
};
var parts = {};
var query = {};
var responseOpts = {};
<%
if (methods.length > 1) { %>
// figure out the method
if (params.method = _.toUpperString(params.method)) {
if (<%= _.map(methods, function (method) { return 'params.method === ' + stringify(method) }).join(' || ') %>) {
request.method = params.method;
} else {
throw new TypeError('Invalid method: should be one of <%= methods.join(', ') %>');
}
} else {<%
if (_.contains(methods, 'GET')) {
var nonGet = _.find(methods, function (m) {return m !== 'GET'; });%>
request.method = params.body ? <%= stringify(nonGet) %> : 'GET';<%
} else {%>
request.method = <%= stringify(methods[0]) %>;<%
}%>
}<%
}
%>
// find the paths's params
<%= writeParams(2, urlParts, 'parts.') %>
// build the path
<%= writeUrls(2, urls, urlParts, params) %>
// build the query string
<%= writeParams(2, params, 'query.') %>
request.path = request.path + _.makeQueryString(query);
<%= returnStatement(2, name) %>
}
module.exports = do<%= _.studlyCase(name) %>;

View File

@ -12,16 +12,16 @@ _.each(actions, function (action) {
var className = _.studlyCase(namespace) + 'NS';
%>
api.<%= namespace %> = function <%= className %>(client) {
if (this instanceof <%= className %>) {
this.client = client;
} else {
return new <%= className %>(client);
}
api.<%= namespace %> = function <%= className %>(transport) {
this.transport = transport;
};<%
}
%>
}%>
<%= partials.client_action(action) %><%
}); %>
});
_.each(proxies, function (action) {%>
<%= partials.client_action_proxy(action) %><%
});
%>

View File

@ -0,0 +1,19 @@
/**
* Perform a [<%= name %>](<%= docUrl %>) request
*
* @param {Object} params - An object with parameters used to carry out this action<%
_.each(allParams, function(param, paramName) { %>
* @param {<%= paramType(param.type) %>} <%= paramWithDefault('params.' + paramName, param.default) %><%
if (param.description) {
%> - <%= param.description %><%
}
%><% }) %>
*/
api<%= (location[0] === '[' ? '' : '.') + location %> = ca.proxy(<%= 'api' + (proxy[0] === '[' ? '' : '.') + proxy %><%
if (typeof transformBody === 'string') { %>, {
transform: function (params) {
<%= indent(transformBody, 4) %>
}
}<%
}
%>);

View File

@ -42,6 +42,13 @@ var templateGlobals = {
_: _,
indent: function (block, spaces) {
var indent = _.repeat(' ', spaces);
return block.split('\n').map(function (line) {
return indent + line;
}).join('\n');
},
paramType: function (type) {
switch (type && type.toLowerCase ? type.toLowerCase() : 'any') {
case 'time':

View File

@ -34,7 +34,8 @@ var es = require('../../../src/elasticsearch'),
endingMoment = moment().endOf('day').add('days', days),
clientConfig = {
log: {
level: 'error'
level: 'trace',
type: 'stdio'
}
};
@ -95,7 +96,7 @@ fillIndecies(function () {
actions.push(event);
if (actions.length === 3000 || i === count - 1) {
client.config.log.info('writing', actions.length / 2, 'documents');
console.info('writing', actions.length / 2, 'documents');
client.bulk({
body: actions
}, done);
@ -176,10 +177,11 @@ function fillIndecies(cb) {
async.parallel(indexPushActions, function (err, responses) {
if (err) {
client.config.log.error(err.message = 'Unable to create indicies: ' + err.message);
console.error(err.message = 'Unable to create indicies: ' + err.message);
console.error(err.stack);
} else {
_.each(_.groupBy(responses), function (list, did) {
client.config.log.info(list.length, 'indicies', did);
console.info(list.length, 'indicies', did);
});
cb();
}

View File

@ -32,7 +32,7 @@ function download() {
restSpecUpdated(function (err, updated) {
if (process.env.FORCE_GEN || err || updated) {
if (process.env.FORCE_GEN || process.env.npm_config_force || err || updated) {
download();
}
});