Files
elasticsearch-js/scripts/generate/js_api/spec.js
Spencer Alger 20462c73fd Slight refactor to the api module, so it will simply extend the client like it did previously, and
also exposes itself on the module.exports so that is can be referenced externally.

Added "grunt run" task which currently has a single config, generate_js_api.

Removed the api spec submodule, generator just downloads master when it runs.
2013-10-23 16:07:56 -07:00

86 lines
2.0 KiB
JavaScript

var _ = require('../../../src/lib/utils')
var EventEmitter = require('events').EventEmitter;
var aliases = require('./aliases');
var https = require('https');
var unzip = require('unzip');
var castNotFoundRE = /exists/;
var usesBulkBodyRE = /^(bulk|msearch)$/;
var specCount = 0;
var doneParsing = false;
https.get('https://codeload.github.com/elasticsearch/elasticsearch-rest-api-spec/zip/master', function (incoming) {
incoming.pipe(unzip.Parse())
.on('entry', function (entry) {
if (entry.type === 'File' && entry.path.match(/(^|\/)api\/.*\.json$/)) {
specCount++;
return collectEntry(entry);
} else {
entry.autodrain();
}
})
.on('close', function () {
doneParsing = true;
if (specs.length === specCount) {
module.exports.emit('ready', specs);
}
});
})
var specs = [];
function collectEntry(entry) {
var file = '';
function onData (chunk) {
file+= chunk;
}
function onEnd () {
entry.removeListener('data', onData);
entry.removeListener('end', onEnd);
process.nextTick(function () {
transformFile(file);
});
}
entry.on('data', onData)
entry.on('end', onEnd);
}
function transformFile(file) {
// itterate all of the specs within the file, should only be one
_.each(JSON.parse(file), function (def, name) {
var steps = name.split('.');
var spec = {
name: name,
methods: _.map(def.methods, function (m) { return m.toUpperCase(); }),
docUrl: def.documentation,
urlParts: def.url.parts,
params: def.url.params,
urls: _.difference(def.url.paths, aliases[name]),
body: def.body || null,
path2lib: _.repeat('../', steps.length + 1) + 'lib/'
};
if (def.body && def.body.requires) {
spec.needBody = true;
}
if (usesBulkBodyRE.test(name)) {
spec.bulkBody = true;
}
if (castNotFoundRE.test(name)) {
spec.castNotFound = true;
}
if (specs.push(spec) === specCount && doneParsing) {
module.exports.emit('ready', specs);
}
})
}
module.exports = new EventEmitter();