Files
elasticsearch-js/scripts/generate/templates/index.js
Dominykas Blyžė f1de944809 Upgrade to lodash v4 (#660)
* npm install lodash-2

Someone handily published a lodash-2 v4.17.4 - it is exactly the same as lodash v4.17.4, so it is safe to use during the migration.

* use lodash-2 in tests

* update tests to split utils vs lodash

* remove Utils.nextTick usage

Utils.nextTick with a single argument is the same as process.nextTick

* lowercase utils

Because it seems that this is the coding style in this repo

* upgrade lodash in grunt/*

* keep lodash-2 as a dev dep for now

* use lodash-2 in scripts

* use snakeCase from utils

It was a mistake in my previous commit to not update this usage

* fix naming gruntUtils vs utils

As all three - gruntUtils, utils and lodash (_) are getting passed into templates, it makes sense to keep the naming consistent

* fix naming gruntUtils vs utils

As all three - gruntUtils, utils and lodash (_) are getting passed into templates, it makes sense to keep the naming consistent

* split utils vs lodash in scripts/generate

Also use lodash-2 where it is easy to do so

* use utils.get until lodash upgrade

* remove lodash.isempty; lodash-2 now used in prod (in src/lib/apis/ code)

* unbundle lodash from utils

* upgrade to lodash 4

* remove lodash.get and lodash.trimEnd

* clean out unused code

* clean out unused code

* fix a breaking change listed under "notable changes" rather than under "breaking changes"...
2018-05-14 11:37:23 -07:00

122 lines
3.4 KiB
JavaScript

var _ = require('lodash');
var utils = require('../../../src/lib/utils');
var gruntUtils = require('../../../grunt/utils');
var fs = require('fs');
var path = require('path');
/**
* we want strings in code to use single-quotes, so this will JSON encode vars, but then
* modify them to follow our code standards.
*
* @param {*} thing - Any thing
* @return {String} - our pretty string
*/
function stringify(thing, pretty) {
return (pretty ? JSON.stringify(thing, null, ' ') : JSON.stringify(thing))
.replace(/'/g, '\\\'')
.replace(/\\?"/g, function (quote) {
// replace external (unescaped) double quotes with single quotes
return quote === '\\"' ? '"' : '\'';
})
// inject a space between STRING array parts
.replace(/([^\\])','/g, '$1\', \'')
// remove quotes around key names that are only made up of letters
.replace(/^( +)'([a-zA-Z_]+)':/gm, '$1$2:')
// requote "special" key names
.replace(/^( +)(default):/gm, '$1\'$2\':');
}
/**
* We'll collect the templates here
* @type {Object}
*/
var templates = {};
/**
* These keys will be available as local variables to each template
* @type {Object}
*/
var templateGlobals = {
stringify: stringify,
_: _,
utils: utils,
indent: function (block, spaces) {
var indent = utils.repeat(' ', spaces);
return block.split('\n').map(function (line) {
return indent + line;
}).join('\n');
},
joinParagraphs: function (block) {
return block.split('\n\n').join('\n+\n');
},
paramType: function (type, paramName) {
switch (type && type.toLowerCase ? type.toLowerCase() : 'any') {
case 'time':
case 'duration':
if (paramName === 'timestamp') return 'Timestamp'
return '<<api-param-type-duration-string,`DurationString`>>';
case 'any':
return 'anything';
case 'enum':
case 'string':
case 'text':
return '<<api-param-type-string,`String`>>';
case 'boolean':
return '<<api-param-type-boolean,`Boolean`>>';
case 'number':
case 'integer':
return '<<api-param-type-number,`Number`>>';
case 'list':
return '<<api-param-type-string,`String`>>, <<api-param-type-string-array,`String[]`>>, <<api-param-type-boolean,`Boolean`>>';
case 'bulkbody':
return '<<api-param-type-object-array,`Object[]`>>, <<api-param-type-json-lines,`JSONLines`>>';
case 'body':
return '<<api-param-type-object,`Object`>>, <<api-param-type-json,`JSON`>>';
default:
throw new Error(`unknown type "${type}"`);
}
},
paramWithDefault: function (name, def) {
if (def) {
return '[' + name + '=' + def + ']';
} else {
return name;
}
},
partials: templates,
gruntUtils: gruntUtils
};
fs.readdirSync(path.resolve(__dirname)).forEach(function (filename) {
var name = filename.replace(/\..+$/, '');
if (name !== 'index') {
templates[name] = _.template(
fs.readFileSync(path.resolve(__dirname, filename), 'utf8'),
{
imports: templateGlobals
}
);
}
});
templates.text = templates.string;
module.exports = {
apiFile: templates.api_file,
apiMethodList: templates.api_method_list,
apiMethods: templates.api_methods,
docsIndex: templates.docs_index,
apiIndex: templates.api_index,
apiIndexBrowser: templates.api_index_browser,
configurationDocs: templates.configuration_docs,
};