Use standard and prettier (#10)

* switch from custom eslint config to standard + prettier

* fix new standard eslint violations

* add editorconfig file

* auto-fix all other violations

* update lint yarn script

* remove jshint comment
This commit is contained in:
Spencer
2019-07-09 13:24:13 -07:00
committed by GitHub
parent f69840c50f
commit 7c1573fb07
119 changed files with 4506 additions and 3521 deletions

View File

@ -1,6 +1,9 @@
var _ = require('lodash');
var pkg = require('../package.json');
var branches = [...pkg.config.supported_es_branches, ...pkg.config.unstable_es_branches];
var branches = [
...pkg.config.supported_es_branches,
...pkg.config.unstable_es_branches,
];
var semver = require('semver');
function nextMajorVersion() {
@ -8,14 +11,19 @@ function nextMajorVersion() {
.map(v => parseFloat(v.split('.')[0]))
.filter(n => !isNaN(n))
.sort((a, b) => b - a)
.shift()
.shift();
return new Version(`${largestMajor + 1}.0.0`)
return new Version(`${largestMajor + 1}.0.0`);
}
function nextMinorVersion(major) {
const largestMinor = branches
.map(v => v.split('.').map(parseFloat).slice(0, 2))
.map(v =>
v
.split('.')
.map(parseFloat)
.slice(0, 2)
)
// ensure all tuples have length 2
.filter(vt => vt.length === 2)
// ensure all values in tuples are not NaN
@ -37,38 +45,37 @@ function Version(v) {
this.patch = semver.patch(v);
}
Version.fromBranch = function (branch) {
Version.fromBranch = function(branch) {
// n.m -> n.m.0
if (/^\d+\.\d+$/.test(branch)) return new Version(branch + '.0');
// n.x -> n.(maxVersion + 1).0
const match = branch.match(/^(\d+)\.x$/i)
const match = branch.match(/^(\d+)\.x$/i);
if (match) return nextMinorVersion(match[1]);
// master => (maxMajorVersion + 1).0.0
if (branch === 'master') return nextMajorVersion()
if (branch === 'master') return nextMajorVersion();
throw new Error('unable to convert branch "' + branch + '" to semver');
};
Version.prototype.increment = function (which) {
Version.prototype.increment = function(which) {
return new Version(semver.inc(this.version, which));
};
Version.prototype.satisfies = function (range) {
Version.prototype.satisfies = function(range) {
return semver.satisfies(this.version, range);
};
// merge a list of option objects, each of which has a "version" key dictating
// the range of versions those options should be included in. Options are merged
// in the order of the array
Version.prototype.mergeOpts = function (versioned, overrides) {
Version.prototype.mergeOpts = function(versioned, overrides) {
const candidates = versioned
.filter(o => this.satisfies(o.version))
.map(o => _.omit(o, 'version'))
.map(o => _.omit(o, 'version'));
return _.merge({}, overrides || {}, ...candidates)
return _.merge({}, overrides || {}, ...candidates);
};
module.exports = Version;

View File

@ -14,9 +14,12 @@ function indent(line) {
function consume(stream) {
stream
.pipe(split())
.pipe(map(indent))
.pipe(process.stdout, { end: false });
.pipe(split())
.pipe(map(indent))
.pipe(
process.stdout,
{ end: false }
);
}
function _spawn(cmd, args, opts, cb) {
@ -27,11 +30,7 @@ function _spawn(cmd, args, opts, cb) {
}
var conf = {
stdio: [
'ignore',
opts.verbose ? 'pipe' : 'ignore',
'pipe'
]
stdio: ['ignore', opts.verbose ? 'pipe' : 'ignore', 'pipe'],
};
var subdir;
@ -41,7 +40,12 @@ function _spawn(cmd, args, opts, cb) {
subdir = path.relative(root, opts.cwd);
}
console.log(chalk.white.bold((subdir ? subdir + ' ' : '') + '$ ') + cmd + ' ' + args.join(' '));
console.log(
chalk.white.bold((subdir ? subdir + ' ' : '') + '$ ') +
cmd +
' ' +
args.join(' ')
);
var cp = spawn(cmd, args, conf);
@ -58,6 +62,6 @@ function _spawn(cmd, args, opts, cb) {
return cp;
}
_spawn.exec = function (cmd, opts, cb) {
_spawn.exec = function(cmd, opts, cb) {
return _spawn('/bin/sh', ['-c', cmd], opts, cb);
};

View File

@ -37,7 +37,7 @@ var TASKS = [];
var output; // main output stream
var taskOut; // task output stream
task('NODE_UNIT', true, function () {
task('NODE_UNIT', true, function() {
if (!JENKINS) {
return grunt('mochacov:ci_unit');
}
@ -45,67 +45,77 @@ task('NODE_UNIT', true, function () {
return grunt('mochacov:jenkins_unit');
});
task('NODE_INTEGRATION', true, function () {
task('NODE_INTEGRATION', true, function() {
var branch = ENV.ES_REF;
return node('scripts/generate', '--no-api', '--branch', branch)
.then(function () {
var target = (JENKINS ? 'jenkins_' : '') + 'integration:' + branch;
return grunt('esvm:ci_env', 'mocha_' + target, 'esvm_shutdown:ci_env');
});
return node('scripts/generate', '--no-api', '--branch', branch).then(
function() {
var target = (JENKINS ? 'jenkins_' : '') + 'integration:' + branch;
return grunt('esvm:ci_env', 'mocha_' + target, 'esvm_shutdown:ci_env');
}
);
});
task('SAUCE_LABS', false, function () {
return new Promise(function (resolve, reject) {
task('SAUCE_LABS', false, function() {
return new Promise(function(resolve, reject) {
// build the clients and start the server, once the server is ready call trySaucelabs()
var serverTasks = ['browser_clients:build', 'run:browser_test_server:keepalive'];
spawn(GRUNT, serverTasks, function (proc) {
var serverTasks = [
'browser_clients:build',
'run:browser_test_server:keepalive',
];
spawn(GRUNT, serverTasks, function(proc) {
var toLines = split();
proc.stdout
.pipe(toLines)
.pipe(through2(function (line, enc, cb) {
cb();
proc.stdout.pipe(toLines).pipe(
through2(function(line, enc, cb) {
cb();
if (String(line).indexOf('listening on port 8000') === -1) return;
if (String(line).indexOf('listening on port 8000') === -1) return;
trySaucelabs()
.finally(function() {
if (proc) proc.kill();
})
.then(resolve, reject);
trySaucelabs()
.finally(function () { if (proc) proc.kill(); })
.then(resolve, reject);
proc.on('exit', function () { proc = null; });
proc.stdout.unpipe(toLines);
toLines.end();
}));
proc.on('exit', function() {
proc = null;
});
proc.stdout.unpipe(toLines);
toLines.end();
})
);
})
// ignore server errors
.catch(_.noop);
// ignore server errors
.catch(_.noop);
// attempt to run tests on saucelabs and retry if it fails
var saucelabsAttempts = 0;
function trySaucelabs() {
saucelabsAttempts++;
return new Promise(function (resolve, reject) {
return new Promise(function(resolve, reject) {
log(chalk.green('saucelabs attempt #', saucelabsAttempts));
spawn(GRUNT, ['saucelabs-mocha'], function (cp) {
spawn(GRUNT, ['saucelabs-mocha'], function(cp) {
var failedTests = 0;
cp.stdout
.pipe(split())
.pipe(map(function (line) {
failedTests += String(line).trim() === 'Passed: false' ? 1 : 0;
}));
cp.stdout.pipe(split()).pipe(
map(function(line) {
failedTests += String(line).trim() === 'Passed: false' ? 1 : 0;
})
);
cp.on('error', reject);
cp.on('exit', function (code) {
cp.on('exit', function(code) {
if (code > 0) {
if (failedTests > 0) {
return reject(new Error('Browser tests failed'));
}
if (saucelabsAttempts >= 3) {
return reject(new Error('Saucelabs is like really really down. Tried 3 times'));
return reject(
new Error(
'Saucelabs is like really really down. Tried 3 times'
)
);
}
log(chalk.blue('trying saucelabs again...'));
@ -115,21 +125,20 @@ task('SAUCE_LABS', false, function () {
return resolve();
});
})
// swallow spawn() errors, custom error handler in place
.catch(_.noop);
// swallow spawn() errors, custom error handler in place
.catch(_.noop);
});
}
});
});
task('CHECK_COVERAGE', false, function () {
return grunt('mochacov:ship_coverage')
.catch(function () {
log('FAILED TO SHIP COVERAGE! but that\'s okay');
task('CHECK_COVERAGE', false, function() {
return grunt('mochacov:ship_coverage').catch(function() {
log("FAILED TO SHIP COVERAGE! but that's okay");
});
});
execTask('SETUP', function () {
execTask('SETUP', function() {
return Promise.try(function readVersion() {
if (!ENV.ES_V) {
if (ENV.ES_RELEASE) {
@ -141,8 +150,8 @@ execTask('SETUP', function () {
}
}
var match;
if (match = ENV.ES_V.match(/^(.*)_nightly$/)) {
var match = ENV.ES_V.match(/^(.*)_nightly$/);
if (match) {
return [match[1], null];
}
@ -152,55 +161,56 @@ execTask('SETUP', function () {
throw new Error('unable to parse ES_V ' + ENV.ES_V);
})
.then(function readOtherConf(ver) {
if (!ver) {
throw new Error('Unable to run the ci script without at least an ES_REF or ES_RELEASE environment var.');
}
.then(function readOtherConf(ver) {
if (!ver) {
throw new Error(
'Unable to run the ci script without at least an ES_REF or ES_RELEASE environment var.'
);
}
log('ES_PORT:', ENV.ES_PORT = parseInt(ENV.ES_PORT || 9400, 10));
log('ES_HOST:', ENV.ES_HOST = ENV.ES_HOST || 'localhost');
log('ES_PORT:', (ENV.ES_PORT = parseInt(ENV.ES_PORT || 9400, 10)));
log('ES_HOST:', (ENV.ES_HOST = ENV.ES_HOST || 'localhost'));
if (ver[0]) log('ES_REF:', ENV.ES_REF = ver[0]);
else delete ENV.ES_REF;
if (ver[0]) log('ES_REF:', (ENV.ES_REF = ver[0]));
else delete ENV.ES_REF;
if (ver[1]) log('ES_RELEASE:', ENV.ES_RELEASE = ver[1]);
else delete ENV.ES_RELEASE;
})
.then(function readTasks() {
if (!ENV.RUN) {
return _.filter(TASKS, { default: true });
}
return ENV.RUN
.split(',')
.map(function (name) {
return _.find(TASKS, { name: name.trim() });
if (ver[1]) log('ES_RELEASE:', (ENV.ES_RELEASE = ver[1]));
else delete ENV.ES_RELEASE;
})
.filter(Boolean);
});
})
.then(function (queue) {
if (!queue.length) {
throw new Error('no tasks to run');
}
.then(function readTasks() {
if (!ENV.RUN) {
return _.filter(TASKS, { default: true });
}
// Recursively do tasks until the queue is empty
return (function next() {
if (!queue.length) return;
return execTask(queue.shift()).then(next);
}());
return ENV.RUN.split(',')
.map(function(name) {
return _.find(TASKS, { name: name.trim() });
})
.filter(Boolean);
});
})
.then(function () {
logImportant(chalk.bold.green('✔︎ SUCCESS'));
})
.catch(function (e) {
logImportant(chalk.bold.red('✗ FAILURE\n\n' + e.stack));
.then(function(queue) {
if (!queue.length) {
throw new Error('no tasks to run');
}
// override process exit code once it is ready to close
process.once('exit', function () {
process.exit(1);
// Recursively do tasks until the queue is empty
return (function next() {
if (!queue.length) return;
return execTask(queue.shift()).then(next);
})();
})
.then(function() {
logImportant(chalk.bold.green('✔︎ SUCCESS'));
})
.catch(function(e) {
logImportant(chalk.bold.red('✗ FAILURE\n\n' + e.stack));
// override process exit code once it is ready to close
process.once('exit', function() {
process.exit(1);
});
});
});
/** ****
* utils
@ -217,7 +227,7 @@ function logImportant(text) {
}
function push(m) {
return function () {
return function() {
var args = _.toArray(arguments);
var cb = args.pop();
this.push(m.apply(this, args));
@ -227,8 +237,12 @@ function push(m) {
function indent() {
var str = through2(
push(function (chunk) { return String(chunk).replace(NL_RE, '$1 '); }),
push(function () { return '\n'; })
push(function(chunk) {
return String(chunk).replace(NL_RE, '$1 ');
}),
push(function() {
return '\n';
})
);
str.push(' ');
return str;
@ -243,7 +257,7 @@ function task(name, def, fn) {
TASKS.push({
name: name,
default: def,
fn: fn
fn: fn,
});
}
@ -254,20 +268,20 @@ function execTask(name, task) {
}
output = through2();
output
.pipe(process.stdout, { end: false });
output.pipe(
process.stdout,
{ end: false }
);
log(chalk.white.underline(name));
taskOut = through2();
taskOut
.pipe(indent())
.pipe(output);
taskOut.pipe(indent()).pipe(output);
function flushTaskOut() {
return new Promise(function (resolve) {
return new Promise(function(resolve) {
// wait for the taskOut to finish writing before continuing
output.once('finish', function () {
output.once('finish', function() {
log('');
resolve();
});
@ -280,26 +294,33 @@ function execTask(name, task) {
}
function spawn(file, args, block) {
return new Promise(function (resolve, reject) {
return new Promise(function(resolve, reject) {
var proc = cp.spawn(file, args, {
cwd: ROOT,
env: ENV,
stdio: [0, 'pipe', 'pipe']
stdio: [0, 'pipe', 'pipe'],
});
proc.stdout.pipe(taskOut, { end: false });
proc.stderr.pipe(taskOut, { end: false });
proc.stdout.pipe(
taskOut,
{ end: false }
);
proc.stderr.pipe(
taskOut,
{ end: false }
);
var stdout = '';
proc.stdout
.pipe(through2(function (chunk, enc, cb) {
stdout += chunk;
cb();
}));
proc.stdout.pipe(
through2(function(chunk, enc, cb) {
stdout += chunk;
cb();
})
);
if (block) block(proc);
proc.on('exit', function (code) {
proc.on('exit', function(code) {
if (code > 0) {
reject(new Error('non-zero exit code: ' + code));
} else {
@ -307,22 +328,25 @@ function spawn(file, args, block) {
}
});
proc.on('error', function (origErr) {
reject(new Error('Unable to execute "' + file + ' ' + args.join(' ') + '": ' + origErr.message));
proc.on('error', function(origErr) {
reject(
new Error(
'Unable to execute "' +
file +
' ' +
args.join(' ') +
'": ' +
origErr.message
)
);
});
});
}
function node(/* args... */) {
return spawn(
process.execPath,
_.toArray(arguments)
);
return spawn(process.execPath, _.toArray(arguments));
}
function grunt(/* args... */) {
return spawn(
GRUNT,
_.toArray(arguments)
);
return spawn(GRUNT, _.toArray(arguments));
}

2
scripts/eslint.js Normal file
View File

@ -0,0 +1,2 @@
process.argv.push('.');
require('eslint/bin/eslint');

View File

@ -1,25 +1,40 @@
module.exports = function (done) {
module.exports = function(done) {
var _ = require('lodash');
var gruntUtils = require('../../grunt/utils');
var chalk = require('chalk');
var fromRoot = _.partial(require('path').join, require('find-root')(__dirname));
var fromRoot = _.partial(
require('path').join,
require('find-root')(__dirname)
);
var write = require('fs').writeFileSync;
var nodeApiIndex = fromRoot('src/lib/apis/index.js');
var browserApiIndex = fromRoot('src/lib/apis/browser_index.js');
write(nodeApiIndex, require('./templates').apiIndex({
branches: gruntUtils.branches
}), 'utf8');
write(
nodeApiIndex,
require('./templates').apiIndex({
branches: gruntUtils.branches,
}),
'utf8'
);
console.log(chalk.white.bold('wrote'), 'api index to', nodeApiIndex);
write(browserApiIndex, require('./templates').apiIndexBrowser({
branches: gruntUtils.browserBranches
}), 'utf8');
write(
browserApiIndex,
require('./templates').apiIndexBrowser({
branches: gruntUtils.browserBranches,
}),
'utf8'
);
console.log(chalk.white.bold('wrote'), 'browser api index to', browserApiIndex);
console.log(
chalk.white.bold('wrote'),
'browser api index to',
browserApiIndex
);
done();
};

View File

@ -1,8 +1,11 @@
module.exports = function (done) {
module.exports = function(done) {
var _ = require('lodash');
var chalk = require('chalk');
var fromRoot = _.partial(require('path').join, require('find-root')(__dirname));
var fromRoot = _.partial(
require('path').join,
require('find-root')(__dirname)
);
var write = require('fs').writeFile;
var outputPath = fromRoot('docs/configuration.asciidoc');

View File

@ -1,18 +1,26 @@
module.exports = function (done) {
module.exports = function(done) {
var _ = require('lodash');
var gruntUtils = require('../../grunt/utils');
var chalk = require('chalk');
var fromRoot = _.partial(require('path').join, require('find-root')(__dirname));
var fromRoot = _.partial(
require('path').join,
require('find-root')(__dirname)
);
var write = require('fs').writeFile;
var outputPath = fromRoot('docs/index.asciidoc');
write(outputPath, require('./templates').docsIndex({
apiFiles: gruntUtils.stableBranches.map(function (branch) {
return 'api_methods' + gruntUtils.branchSuffix(branch) + '.asciidoc';
})
}), 'utf8', done);
write(
outputPath,
require('./templates').docsIndex({
apiFiles: gruntUtils.stableBranches.map(function(branch) {
return 'api_methods' + gruntUtils.branchSuffix(branch) + '.asciidoc';
}),
}),
'utf8',
done
);
console.log(chalk.white.bold('wrote'), 'doc index to', outputPath);
};

View File

@ -2,30 +2,29 @@
var async = require('async');
var fs = require('fs');
var spawn = require('../_spawn');
var argv = require('optimist')
.options({
verbose: {
alias: 'v',
default: false,
boolean: true
},
api: {
default: true,
boolean: true
},
tests: {
default: true,
boolean: true
},
update: {
default: true,
boolean: true
},
branch: {
default: null,
string: true
}
});
var argv = require('optimist').options({
verbose: {
alias: 'v',
default: false,
boolean: true,
},
api: {
default: true,
boolean: true,
},
tests: {
default: true,
boolean: true,
},
update: {
default: true,
boolean: true,
},
branch: {
default: null,
string: true,
},
});
var path = require('path');
var fromRoot = path.join.bind(path, require('find-root')(__dirname));
@ -59,82 +58,101 @@ var paths = {
docs: fromRoot('docs'),
docsIndex: fromRoot('docs/index.asciidoc'),
apiSrc: 'src/lib/apis',
getArchiveDir: function (branch) {
getArchiveDir: function(branch) {
return fromRoot('src/_elasticsearch_' + utils.snakeCase(branch));
},
getArchiveTarball: function (branch) {
getArchiveTarball: function(branch) {
return fromRoot('src/_elasticsearch_' + utils.snakeCase(branch) + '.tar');
},
getSpecPathInRepo: function (branch) {
return /^v?(master|[2-9]\.)/.test(branch) ? 'rest-api-spec/src/main/resources/rest-api-spec' : 'rest-api-spec';
}
getSpecPathInRepo: function(branch) {
return /^v?(master|[2-9]\.)/.test(branch)
? 'rest-api-spec/src/main/resources/rest-api-spec'
: 'rest-api-spec';
},
};
function isDirectory(dir) {
var stat;
try { stat = fs.statSync(dir); } catch (e) {}
return (stat && stat.isDirectory());
try {
stat = fs.statSync(dir);
} catch (e) {}
return stat && stat.isDirectory();
}
function dirFilter(dir, fn) {
try {
return fs.readdirSync(dir)
.filter(function (name) {
return name !== '.' && name !== '..' && fn(name);
})
.map(function (filename) {
return path.join(dir, filename);
});
return fs
.readdirSync(dir)
.filter(function(name) {
return name !== '.' && name !== '..' && fn(name);
})
.map(function(filename) {
return path.join(dir, filename);
});
} catch (e) {
return [];
}
}
function dirRegex(dir, regexp) {
return dirFilter(dir, function (name) {
return dirFilter(dir, function(name) {
return name.match(regexp);
});
}
function dirOpts(dir, opts) {
opts = _.isArray(opts) ? opts : [opts];
return dirFilter(dir, function (name) {
return dirFilter(dir, function(name) {
return _.includes(opts, name);
});
}
function spawnStep(cmd, args, cwd) {
return function (done) {
spawn(cmd, args, {
verbose: argv.verbose,
cwd: cwd
}, function (status) {
done(status ? new Error('Non-zero exit code: ' + status) : void 0);
});
return function(done) {
spawn(
cmd,
args,
{
verbose: argv.verbose,
cwd: cwd,
},
function(status) {
done(status ? new Error('Non-zero exit code: ' + status) : void 0);
}
);
};
}
function initStep() {
return function (done) {
return function(done) {
if (isDirectory(paths.esSrc)) {
async.series([
spawnStep('git', ['remote', 'set-url', 'origin', esUrl], paths.esSrc)
], done);
async.series(
[spawnStep('git', ['remote', 'set-url', 'origin', esUrl], paths.esSrc)],
done
);
} else {
async.series([
spawnStep('git', ['init', '--bare', paths.esSrc], paths.root),
spawnStep('git', ['remote', 'add', 'origin', esUrl], paths.esSrc)
], done);
async.series(
[
spawnStep('git', ['init', '--bare', paths.esSrc], paths.root),
spawnStep('git', ['remote', 'add', 'origin', esUrl], paths.esSrc),
],
done
);
}
};
}
function fetchBranchesStep() {
var branchArgs = branches.map(function (b) { return b + ':' + b; });
return spawnStep('git', ['fetch', '--no-tags', '--force', 'origin'].concat(branchArgs), paths.esSrc);
var branchArgs = branches.map(function(b) {
return b + ':' + b;
});
return spawnStep(
'git',
['fetch', '--no-tags', '--force', 'origin'].concat(branchArgs),
paths.esSrc
);
}
function findGeneratedApiFiles() {
var anyApiMethodDocs = /^(configuration|index|api_methods).*\.asciidoc$/;
var anyApiJsFiled = /^.+\.js$/;
@ -143,11 +161,11 @@ function findGeneratedApiFiles() {
if (allBranches) {
return [
dirRegex(paths.docs, anyApiMethodDocs),
dirRegex(paths.apiSrc, anyApiJsFiled)
dirRegex(paths.apiSrc, anyApiJsFiled),
];
}
return branches.reduce(function (files, branch) {
return branches.reduce(function(files, branch) {
var b = utils.snakeCase(branch);
files.push(dirOpts(paths.docs, 'api_methods_' + b + '.asciidoc'));
@ -163,7 +181,6 @@ function findGeneratedApiFiles() {
}, []);
}
function clearGeneratedFiles() {
var esArchives = /^_elasticsearch_(master|[\dx_]+|\.tar)$/;
var generatedFiles = [];
@ -175,16 +192,16 @@ function clearGeneratedFiles() {
generatedFiles.push(dirRegex(paths.src, esArchives));
var rmSteps = _.chain(generatedFiles)
.flattenDeep()
.uniq()
.map(function (path) {
return spawnStep('rm', ['-rf', path]);
})
.value();
.flattenDeep()
.uniq()
.map(function(path) {
return spawnStep('rm', ['-rf', path]);
})
.value();
if (!rmSteps.length) return;
return function (done) {
return function(done) {
return async.series(rmSteps, done);
};
}
@ -199,7 +216,7 @@ function removePrevArchive(branch) {
}
function createArchive(branch) {
return function (done) {
return function(done) {
var dir = paths.getArchiveDir(branch);
var tarball = paths.getArchiveTarball(branch);
var specPathInRepo = paths.getSpecPathInRepo(branch);
@ -210,36 +227,65 @@ function createArchive(branch) {
return process.nextTick(done);
}
async.series([
spawnStep('mkdir', [dir], paths.root),
spawnStep('git', ['archive', '--format', 'tar', '--output', tarball, branch, specPathInRepo], paths.esSrc),
spawnStep('tar', ['-x', '-f', tarball, '-C', dir, '--strip-components', subDirCount]),
spawnStep('rm', [tarball])
], done);
async.series(
[
spawnStep('mkdir', [dir], paths.root),
spawnStep(
'git',
[
'archive',
'--format',
'tar',
'--output',
tarball,
branch,
specPathInRepo,
],
paths.esSrc
),
spawnStep('tar', [
'-x',
'-f',
tarball,
'-C',
dir,
'--strip-components',
subDirCount,
]),
spawnStep('rm', [tarball]),
],
done
);
};
}
function generateStep(branch) {
return function (done) {
async.parallel([
argv.api && async.apply(require('./js_api'), branch),
argv.tests && async.apply(require('./yaml_tests'), branch)
].filter(Boolean), done);
return function(done) {
async.parallel(
[
argv.api && async.apply(require('./js_api'), branch),
argv.tests && async.apply(require('./yaml_tests'), branch),
].filter(Boolean),
done
);
};
}
var steps = [
initStep(),
clearGeneratedFiles(),
fetchBranchesStep()
].filter(Boolean);
var steps = [initStep(), clearGeneratedFiles(), fetchBranchesStep()].filter(
Boolean
);
branches.forEach(function (branch) {
steps.push(_.partial(async.series, [
removePrevArchive(branch),
createArchive(branch),
generateStep(branch)
].filter(Boolean)));
branches.forEach(function(branch) {
steps.push(
_.partial(
async.series,
[
removePrevArchive(branch),
createArchive(branch),
generateStep(branch),
].filter(Boolean)
)
);
});
if (argv.api) {
@ -251,15 +297,15 @@ if (argv.api) {
}
async.series(
steps.map(function (step) {
return function (done) {
step(function (err) {
steps.map(function(step) {
return function(done) {
step(function(err) {
console.log('----\n');
done(err);
});
};
}),
function (err) {
function(err) {
if (err) {
throw err;
}

View File

@ -1,4 +1,4 @@
module.exports = function (branch, done) {
module.exports = function(branch, done) {
/**
* Read the API actions form the rest-api-spec repo.
* @type {[type]}
@ -34,32 +34,29 @@ module.exports = function (branch, done) {
var steps = [
readSpecFiles,
function (done) {
function(done) {
parseSpecFiles(branch, done);
},
writeApiFile
writeApiFile,
];
if (!~gruntUtils.unstableBranches.indexOf(branch)) {
steps.push(
ensureDocsDir,
formatDocVars,
writeMethodDocs
);
steps.push(ensureDocsDir, formatDocVars, writeMethodDocs);
}
// generate the API
async.series(steps, function (err) {
async.series(steps, function(err) {
done(err);
});
function readSpecFiles(done) {
var apiDir = path.join(esDir, 'rest-api-spec/api/');
files = fs.readdirSync(apiDir)
.filter(function (filename) {
return filename[0] !== '_'
files = fs
.readdirSync(apiDir)
.filter(function(filename) {
return filename[0] !== '_';
})
.map(function (filename) {
.map(function(filename) {
var module = require(apiDir + filename);
delete require.cache[apiDir + filename];
return module;
@ -70,23 +67,25 @@ module.exports = function (branch, done) {
function parseSpecFiles(branch, done) {
var actions = [];
files.forEach(function (spec) {
__puke__transformSpec(branch, spec).forEach(function (action) {
files.forEach(function(spec) {
__puke__transformSpec(branch, spec).forEach(function(action) {
actions.push(action);
});
});
// collect the namespaces from the action locations
var namespaces = _.filter(_.map(actions, function (action) {
return action.location
.split('.')
.slice(0, -1)
.filter(step => step !== 'prototype')
.join('.prototype.')
}));
var namespaces = _.filter(
_.map(actions, function(action) {
return action.location
.split('.')
.slice(0, -1)
.filter(step => step !== 'prototype')
.join('.prototype.');
})
);
// seperate the proxy actions
var groups = _.groupBy(actions, function (action) {
var groups = _.groupBy(actions, function(action) {
return action.proxy ? 'proxies' : 'normal';
});
@ -94,7 +93,7 @@ module.exports = function (branch, done) {
actions: groups.normal || [],
proxies: groups.proxies || [],
namespaces: _.uniq(namespaces.sort()),
clientActionModifier: overrides.clientActionModifier
clientActionModifier: overrides.clientActionModifier,
};
if (!_.find(apiSpec.actions, { name: 'create' })) {
@ -105,7 +104,7 @@ module.exports = function (branch, done) {
name: 'create',
location: 'create',
proxy: 'index',
transformBody: 'params.op_type = \'create\';'
transformBody: "params.op_type = 'create';",
}
);
@ -116,36 +115,50 @@ module.exports = function (branch, done) {
apiSpec.proxies.push(create);
}
[].concat(apiSpec.actions, apiSpec.proxies)
.forEach(function (action) {
var examplePath = overrides.examples[action.name] || action.name + '.asciidoc';
var descriptionPath = overrides.descriptions[action.name] || action.name + '.asciidoc';
[].concat(apiSpec.actions, apiSpec.proxies).forEach(function(action) {
var examplePath =
overrides.examples[action.name] || action.name + '.asciidoc';
var descriptionPath =
overrides.descriptions[action.name] || action.name + '.asciidoc';
try {
action.examples = fs.readFileSync(fromRoot('docs/_examples', examplePath), 'utf8');
action.examples = fs.readFileSync(
fromRoot('docs/_examples', examplePath),
'utf8'
);
} catch (e) {
action.examples = '// no examples';
}
try {
action.description = fs.readFileSync(fromRoot('docs/_descriptions', descriptionPath), 'utf8');
action.description = fs.readFileSync(
fromRoot('docs/_descriptions', descriptionPath),
'utf8'
);
} catch (e) {
action.description = '// no description';
}
})
});
done();
}
function writeApiFile(done) {
var outputPath = fromRoot('src/lib/apis/' + utils.snakeCase(branch) + '.js');
var outputPath = fromRoot(
'src/lib/apis/' + utils.snakeCase(branch) + '.js'
);
fs.writeFileSync(outputPath, templates.apiFile(apiSpec));
console.log(chalk.white.bold('wrote'), apiSpec.actions.length, 'api actions to', outputPath);
console.log(
chalk.white.bold('wrote'),
apiSpec.actions.length,
'api actions to',
outputPath
);
done();
}
function ensureDocsDir(done) {
fs.stat(fromRoot('docs'), function (err, stat) {
fs.stat(fromRoot('docs'), function(err, stat) {
if (err) {
if (err.message.match(/enoent/i)) {
fs.mkdir('../../docs', done);
@ -177,34 +190,37 @@ module.exports = function (branch, done) {
function writeMethodDocs(done) {
var filename = fromRoot('docs/api_methods' + branchSuffix + '.asciidoc');
fs.writeFile(
filename,
templates.apiMethods(docVars),
function (err) {
if (!err) {
console.log(chalk.white.bold('wrote'), branch + ' method docs to', filename);
}
done(err);
fs.writeFile(filename, templates.apiMethods(docVars), function(err) {
if (!err) {
console.log(
chalk.white.bold('wrote'),
branch + ' method docs to',
filename
);
}
);
done(err);
});
}
function __puke__transformSpec(branch, spec) { // eslint-disable-line
var actions = [];
// itterate all of the specs within the file, should only be one
_.each(spec, function (def, name) {
_.each(spec, function(def, name) {
// camelcase the name
name = _.map(name.split('.'), utils.camelCase).join('.');
if (name === 'cat.aliases') {
def.documentation = 'http://www.elasticsearch.org/guide/en/elasticsearch/reference/master/cat.html';
def.documentation =
'http://www.elasticsearch.org/guide/en/elasticsearch/reference/master/cat.html';
}
def.documentation = def.documentation ? def.documentation.replace(
/^https?:\/\/.+?\/guide\/en\/elasticsearch\/(.+?)\/.+?\//,
`https://www.elastic.co/guide/en/elasticsearch/$1/${branch}/`
) : '';
def.documentation = def.documentation
? def.documentation.replace(
/^https?:\/\/.+?\/guide\/en\/elasticsearch\/(.+?)\/.+?\//,
`https://www.elastic.co/guide/en/elasticsearch/$1/${branch}/`
)
: '';
var steps = name.split('.');
@ -222,27 +238,36 @@ module.exports = function (branch, done) {
var allParams = _.extend({}, def.url.params, def.url.parts);
_.forOwn(allParams, (paramSpec, paramName) => {
const toMerge = _.get(overrides, ['mergeConcatParams', name, paramName])
const toMerge = _.get(overrides, [
'mergeConcatParams',
name,
paramName,
]);
if (toMerge) {
_.mergeWith(paramSpec, toMerge, (dest, src) => {
if (_.isArray(dest) && _.isArray(src)) {
return dest.concat(src)
return dest.concat(src);
}
})
});
}
if (paramSpec.options) {
const invalidOpts = paramSpec.options.some(opt => typeof opt !== 'string')
if (invalidOpts) throw new Error(`${name} has options that are not strings...`)
const invalidOpts = paramSpec.options.some(
opt => typeof opt !== 'string'
);
if (invalidOpts)
throw new Error(`${name} has options that are not strings...`);
}
})
});
var spec = {
name: name,
methods: _.map(def.methods, function (m) { return m.toUpperCase(); }),
methods: _.map(def.methods, function(m) {
return m.toUpperCase();
}),
params: def.url.params,
body: def.body || null,
path2lib: utils.repeat('../', steps.length + 1) + 'lib/'
path2lib: utils.repeat('../', steps.length + 1) + 'lib/',
};
if (def.body && def.body.required) {
@ -255,63 +280,83 @@ module.exports = function (branch, done) {
var urls = _.difference(def.url.paths, overrides.aliases[name]);
var urlSignatures = [];
urls = _.map(urls, function (url) {
urls = _.map(urls, function(url) {
var optionalVars = {};
var requiredVars = {};
var param;
var name;
var target;
var match;
if (url.charAt(0) !== '/') {
url = '/' + url;
}
while (match = urlParamRE.exec(url)) {
while (true) {
const match = urlParamRE.exec(url);
if (!match) {
break;
}
name = utils.camelCase(match[1]);
param = def.url.parts[name] || {};
target = (param.required || !param.default) ? requiredVars : optionalVars;
target =
param.required || !param.default ? requiredVars : optionalVars;
target[name] = _.omit(param, ['required', 'description', 'name']);
}
urlSignatures.push(_.union(_.keys(optionalVars), _.keys(requiredVars)).sort().join(':'));
urlSignatures.push(
_.union(_.keys(optionalVars), _.keys(requiredVars))
.sort()
.join(':')
);
return _.omitBy({
fmt: url.replace(urlParamRE, function (full, match) {
return '<%=' + utils.camelCase(match) + '%>';
}),
opt: _.size(optionalVars) ? optionalVars : null,
req: _.size(requiredVars) ? requiredVars : null,
sortOrder: _.size(requiredVars) * -1
}, function (v) {
return !v;
});
return _.omitBy(
{
fmt: url.replace(urlParamRE, function(full, match) {
return '<%=' + utils.camelCase(match) + '%>';
}),
opt: _.size(optionalVars) ? optionalVars : null,
req: _.size(requiredVars) ? requiredVars : null,
sortOrder: _.size(requiredVars) * -1,
},
function(v) {
return !v;
}
);
});
if (urlSignatures.length !== _.uniq(urlSignatures).length) {
throw new Error(
'Multiple URLS with the same signature detected for ' +
spec.name +
'\n' +
_.map(urls, 'fmt').join('\n') +
'\n'
spec.name +
'\n' +
_.map(urls, 'fmt').join('\n') +
'\n'
);
}
if (urls.length > 1) {
spec.urls = _.map(_.sortBy(urls, 'sortOrder'), function (url) {
spec.urls = _.map(_.sortBy(urls, 'sortOrder'), function(url) {
return _.omit(url, 'sortOrder');
});
} else {
spec.url = _.omit(urls[0], 'sortOrder');
}
spec.params = _.transform(spec.params, function (note, param, name) {
// param.name = name;
note[name] = _.pick(param, [
'type', 'default', 'options', 'required', 'name'
]);
}, {});
spec.params = _.transform(
spec.params,
function(note, param, name) {
// param.name = name;
note[name] = _.pick(param, [
'type',
'default',
'options',
'required',
'name',
]);
},
{}
);
if (overrides.paramAsBody[name]) {
spec.paramAsBody = overrides.paramAsBody[name];
@ -322,8 +367,10 @@ module.exports = function (branch, done) {
}
// escape method names with "special" keywords
var location = spec.name.split('.').join('.prototype.')
.replace(/(^|\.)(delete|default)(\.|$)/g, '[\'$2\']');
var location = spec.name
.split('.')
.join('.prototype.')
.replace(/(^|\.)(delete|default)(\.|$)/g, "['$2']");
var action = {
_methods: spec.methods,
@ -334,13 +381,16 @@ module.exports = function (branch, done) {
'needBody',
'requestTimeout',
'bulkBody',
'paramAsBody'
'paramAsBody',
]),
location: location,
docUrl: def.documentation,
name: spec.name,
namespace: spec.name.split('.').slice(0, -1).join('.'),
allParams: allParams
namespace: spec.name
.split('.')
.slice(0, -1)
.join('.'),
allParams: allParams,
};
function hasMethod(/* ...methods */) {
@ -354,7 +404,10 @@ module.exports = function (branch, done) {
return true;
}
function methodsAre(/* ...methods */) {
return hasMethod.apply(null, arguments) && arguments.length === action._methods.length;
return (
hasMethod.apply(null, arguments) &&
arguments.length === action._methods.length
);
}
var method;
@ -365,17 +418,13 @@ module.exports = function (branch, done) {
// we need to define what the default method(s) will be
if (hasMethod('DELETE', 'POST')) {
method = 'POST';
}
else if (methodsAre('DELETE')) {
} else if (methodsAre('DELETE')) {
method = 'DELETE';
}
else if (methodsAre('POST', 'PUT')) {
} else if (methodsAre('POST', 'PUT')) {
method = action.name.match(/put/i) ? 'PUT' : 'POST';
}
else if (methodsAre('GET', 'POST')) {
} else if (methodsAre('GET', 'POST')) {
method = 'POST';
}
else if (methodsAre('GET', 'HEAD')) {
} else if (methodsAre('GET', 'HEAD')) {
method = 'GET';
}
}
@ -385,7 +434,9 @@ module.exports = function (branch, done) {
action.spec.method = method;
}
} else {
throw new Error('unable to pick a method for ' + JSON.stringify(action, null, ' '));
throw new Error(
'unable to pick a method for ' + JSON.stringify(action, null, ' ')
);
}
actions.push(action);

View File

@ -4,7 +4,7 @@ module.exports = [
paramAsBody: {
scroll: 'scrollId',
clearScroll: 'scrollId',
}
},
},
{
version: '0.90.x',
@ -15,7 +15,7 @@ module.exports = [
'/_nodes/hot_threads',
'/_cluster/nodes/{node_id}/hotthreads',
'/_cluster/nodes/{node_id}/hot_threads',
'/_nodes/{node_id}/hot_threads'
'/_nodes/{node_id}/hot_threads',
],
'cluster.nodeInfo': [
'/_cluster/nodes',
@ -37,25 +37,19 @@ module.exports = [
'/_nodes/{node_id}/network',
'/_nodes/{node_id}/transport',
'/_nodes/{node_id}/http',
'/_nodes/{node_id}/plugin'
],
'cluster.nodeShutdown': [
'/_cluster/nodes/_shutdown'
'/_nodes/{node_id}/plugin',
],
'cluster.nodeShutdown': ['/_cluster/nodes/_shutdown'],
'cluster.nodeStats': [
'/_cluster/nodes/stats',
'/_nodes/stats/{metric_family}',
'/_nodes/stats/indices/{metric}/{fields}',
'/_cluster/nodes/{node_id}/stats',
'/_nodes/{node_id}/stats/{metric_family}',
'/_nodes/{node_id}/stats/indices/{metric}/{fields}'
],
'get': [
'/{index}/{type}/{id}/_source'
],
'indices.deleteMapping': [
'/{index}/{type}/_mapping'
'/_nodes/{node_id}/stats/indices/{metric}/{fields}',
],
get: ['/{index}/{type}/{id}/_source'],
'indices.deleteMapping': ['/{index}/{type}/_mapping'],
'indices.stats': [
'_stats/{metric_family}',
'/_stats/indexing',
@ -65,9 +59,9 @@ module.exports = [
'/{index}/_stats/{metric_family}',
'/{index}/_stats/indexing',
'/{index}/_stats/search/{search_groups}',
'/{index}/_stats/fielddata/{fields}'
]
}
'/{index}/_stats/fielddata/{fields}',
],
},
},
{
version: '>=1.0.0',
@ -78,7 +72,7 @@ module.exports = [
'/_nodes/hot_threads',
'/_cluster/nodes/{node_id}/hotthreads',
'/_cluster/nodes/{node_id}/hot_threads',
'/_nodes/{node_id}/hot_threads'
'/_nodes/{node_id}/hot_threads',
],
'nodes.info': [
'/_cluster/nodes',
@ -100,27 +94,23 @@ module.exports = [
'/_nodes/{node_id}/network',
'/_nodes/{node_id}/transport',
'/_nodes/{node_id}/http',
'/_nodes/{node_id}/plugin'
],
'nodes.shutdown': [
'/_cluster/nodes/_shutdown'
'/_nodes/{node_id}/plugin',
],
'nodes.shutdown': ['/_cluster/nodes/_shutdown'],
'nodes.stats': [
'/_cluster/nodes/stats',
'/_nodes/stats/{metric_family}',
'/_nodes/stats/indices/{metric}/{fields}',
'/_cluster/nodes/{node_id}/stats',
'/_nodes/{node_id}/stats/{metric_family}',
'/_nodes/{node_id}/stats/indices/{metric}/{fields}'
],
'get': [
'/{index}/{type}/{id}/_source'
'/_nodes/{node_id}/stats/indices/{metric}/{fields}',
],
get: ['/{index}/{type}/{id}/_source'],
'indices.deleteMapping': [
'/{index}/{type}',
'/{index}/_mapping/{type}',
'/{index}/{type}/_mappings',
'/{index}/_mappings/{type}'
'/{index}/_mappings/{type}',
],
'indices.putWarmer': [
// '/_warmer/{name}',
@ -128,23 +118,23 @@ module.exports = [
// '/{index}/{type}/_warmer/{name}',
'/_warmers/{name}',
'/{index}/_warmers/{name}',
'/{index}/{type}/_warmers/{name}'
'/{index}/{type}/_warmers/{name}',
],
'indices.deleteWarmer': [
// '/{index}/_warmer/{name}',
'/{index}/_warmer',
'/{index}/_warmers',
'/{index}/_warmers/{name}'
'/{index}/_warmers/{name}',
],
'indices.deleteAlias': [
// '/{index}/_alias/{name}',
'/{index}/_aliases/{name}'
'/{index}/_aliases/{name}',
],
'indices.putAlias': [
// '/{index}/_alias/{name}',
// '/_alias/{name}',
'/{index}/_aliases/{name}',
'/_aliases/{name}'
'/_aliases/{name}',
],
'indices.putMapping': [
// '/{index}/_mapping/{type}',
@ -154,7 +144,7 @@ module.exports = [
'/{index}/{type}/_mappings',
'/{index}/_mappings/{type}',
'/{index}/_mappings',
'/_mappings/{type}'
'/_mappings/{type}',
],
'indices.stats': [
'_stats/{metric_family}',
@ -165,31 +155,29 @@ module.exports = [
'/{index}/_stats/{metric_family}',
'/{index}/_stats/indexing',
'/{index}/_stats/search/{search_groups}',
'/{index}/_stats/fielddata/{fields}'
]
}
'/{index}/_stats/fielddata/{fields}',
],
},
},
{
version: '>1.4.0',
aliases: {
'indices.putAlias': [
// '/{index}/_alias/{name}',
'/{index}/_aliases/{name}'
]
}
'/{index}/_aliases/{name}',
],
},
},
{
version: '<1.6.0',
aliases: {
'snapshot.create': [
'/_snapshot/{repository}/{snapshot}'
]
}
'snapshot.create': ['/_snapshot/{repository}/{snapshot}'],
},
},
{
version: '>=1.6.0',
// strange indentation makes pretty api files
// strange indentation makes pretty api files
/* eslint-disable */
clientActionModifier:
function (spec) {
@ -202,95 +190,92 @@ function (spec) {
}
});
}
/* eslint-enable */
/* eslint-enable */
},
{
version: '>=2.0.0',
aliases: {
'reindex.rethrottle': [
'/_update_by_query/{task_id}/_rethrottle',
'/_delete_by_query/{task_id}/_rethrottle'
'/_delete_by_query/{task_id}/_rethrottle',
],
'reindexRethrottle': [
'/_update_by_query/{task_id}/_rethrottle'
]
}
reindexRethrottle: ['/_update_by_query/{task_id}/_rethrottle'],
},
},
{
version: '>=5.0.0',
mergeConcatParams: {
bulk: {
refresh: {
options: ['']
}
options: [''],
},
},
create: {
refresh: {
options: ['']
}
options: [''],
},
},
index: {
refresh: {
options: ['']
}
options: [''],
},
},
update: {
refresh: {
options: ['']
}
options: [''],
},
},
delete: {
refresh: {
options: ['']
}
}
options: [''],
},
},
},
aliases: {
'reindexRethrottle': [
reindexRethrottle: [
'/_update_by_query/{task_id}/_rethrottle',
'/_delete_by_query/{task_id}/_rethrottle'
]
}
'/_delete_by_query/{task_id}/_rethrottle',
],
},
},
{
version: '>=2.0',
paramAsBody: {
scroll: {
param: 'scrollId',
body: 'scroll_id'
body: 'scroll_id',
},
clearScroll: {
param: 'scrollId',
body: 'scroll_id'
}
}
body: 'scroll_id',
},
},
},
// description overrides by major version
{
version: '0.90.x',
descriptions: {
search: 'search_1.x.asciidoc'
}
search: 'search_1.x.asciidoc',
},
},
{
version: '1.x',
descriptions: {
search: 'search_1.x.asciidoc'
}
search: 'search_1.x.asciidoc',
},
},
{
version: '2.x',
descriptions: {
search: 'search_2.x.asciidoc'
}
search: 'search_2.x.asciidoc',
},
},
// examples changed at 5.0
{
version: '<5.0.0',
examples: {
scroll: 'scroll_<5.0.asciidoc'
}
scroll: 'scroll_<5.0.asciidoc',
},
},
{
version: '>=5.2 <5.5',
@ -299,7 +284,7 @@ function (spec) {
param: 'scrollId',
body: 'scroll_id',
castToArray: true,
}
}
}
},
},
},
];

View File

@ -1,11 +1,9 @@
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.
@ -14,18 +12,20 @@ var path = require('path');
* @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\':');
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':")
);
}
/**
@ -39,28 +39,30 @@ var templates = {};
* @type {Object}
*/
var templateGlobals = {
stringify: stringify,
_: _,
utils: utils,
indent: function (block, spaces) {
indent: function(block, spaces) {
var indent = utils.repeat(' ', spaces);
return block.split('\n').map(function (line) {
return indent + line;
}).join('\n');
return block
.split('\n')
.map(function(line) {
return indent + line;
})
.join('\n');
},
joinParagraphs: function (block) {
joinParagraphs: function(block) {
return block.split('\n\n').join('\n+\n');
},
paramType: function (type, paramName) {
paramType: function(type, paramName) {
switch (type && type.toLowerCase ? type.toLowerCase() : 'any') {
case 'time':
case 'duration':
if (paramName === 'timestamp') return 'Timestamp'
if (paramName === 'timestamp') return 'Timestamp';
return '<<api-param-type-duration-string,`DurationString`>>';
case 'any':
return 'anything';
@ -84,7 +86,7 @@ var templateGlobals = {
}
},
paramWithDefault: function (name, def) {
paramWithDefault: function(name, def) {
if (def) {
return '[' + name + '=' + def + ']';
} else {
@ -94,16 +96,16 @@ var templateGlobals = {
partials: templates,
gruntUtils: gruntUtils
gruntUtils: gruntUtils,
};
fs.readdirSync(path.resolve(__dirname)).forEach(function (filename) {
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
imports: templateGlobals,
}
);
}

View File

@ -1,4 +1,4 @@
module.exports = function (branch, done) {
module.exports = function(branch, done) {
/**
* Creates a JSON version of the YAML test suite that can be simply bundled for use in the browser.
*/
@ -14,24 +14,20 @@ module.exports = function (branch, done) {
var esDir = fromRoot('src/_elasticsearch_' + utils.snakeCase(branch));
// generate the yaml tests
async.series([
readYamlTests,
writeYamlTests,
writeTestIndex
], done);
async.series([readYamlTests, writeYamlTests, writeTestIndex], done);
function readYamlTests(done) {
var testDir = path.join(esDir, 'rest-api-spec/test/');
function readDirectories(dir) {
fs.readdirSync(dir).forEach(function (filename) {
fs.readdirSync(dir).forEach(function(filename) {
var filePath = path.join(dir, filename);
var stat = fs.statSync(filePath);
if (stat.isDirectory()) {
readDirectories(filePath);
} else if (filename.match(/\.yaml$/)) {
var file = tests[path.relative(testDir, filePath)] = [];
jsYaml.loadAll(fs.readFileSync(filePath, 'utf8'), function (doc) {
var file = (tests[path.relative(testDir, filePath)] = []);
jsYaml.loadAll(fs.readFileSync(filePath, 'utf8'), function(doc) {
file.push(doc);
});
}
@ -43,15 +39,21 @@ module.exports = function (branch, done) {
}
function writeYamlTests(done) {
var testFile = fromRoot('test/integration/yaml_suite/yaml_tests_' + utils.snakeCase(branch) + '.json');
var testFile = fromRoot(
'test/integration/yaml_suite/yaml_tests_' +
utils.snakeCase(branch) +
'.json'
);
fs.writeFileSync(testFile, JSON.stringify(tests, null, ' '), 'utf8');
console.log(chalk.white.bold('wrote') + ' YAML tests as JSON to', testFile);
done();
}
function writeTestIndex(done) {
var file = fromRoot('test/integration/yaml_suite/index_' + utils.snakeCase(branch) + '.js');
fs.writeFileSync(file, 'require(\'./run\')(\'' + branch + '\');\n', 'utf8');
var file = fromRoot(
'test/integration/yaml_suite/index_' + utils.snakeCase(branch) + '.js'
);
fs.writeFileSync(file, "require('./run')('" + branch + "');\n", 'utf8');
console.log(chalk.white.bold('wrote') + ' YAML index to', file);
done();
}

View File

@ -16,30 +16,45 @@ bowerJson.version = esjsJson.version;
bowerPackageJson.version = esjsJson.version;
// write the new bower.json file
fs.writeFileSync(bowerDir + '/bower.json', JSON.stringify(bowerJson, null, ' '));
fs.writeFileSync(
bowerDir + '/bower.json',
JSON.stringify(bowerJson, null, ' ')
);
// write the new package.json file
fs.writeFileSync(bowerDir + '/package.json', JSON.stringify(bowerPackageJson, null, ' '));
fs.writeFileSync(
bowerDir + '/package.json',
JSON.stringify(bowerPackageJson, null, ' ')
);
function make(cmd, args) {
return _.bind(spawn, null, cmd, args, {
verbose: true,
cwd: bowerDir
cwd: bowerDir,
});
}
async.series([
make('git', ['add', '-A']),
make('git', ['commit', '-m', 'version ' + bowerJson.version]),
make('git', ['tag', '-a', 'v' + bowerJson.version, '-m', 'version ' + bowerJson.version]),
make('git', ['push', 'origin', 'master']),
make('git', ['push', '--tags', 'origin']),
make('npm', ['publish'])
], function (err) {
if (err) {
if (_.isNumber(err)) {
console.log('Non-zero exit code: %d', err);
} else {
console.log('Error: ', err.message ? err.message : err);
async.series(
[
make('git', ['add', '-A']),
make('git', ['commit', '-m', 'version ' + bowerJson.version]),
make('git', [
'tag',
'-a',
'v' + bowerJson.version,
'-m',
'version ' + bowerJson.version,
]),
make('git', ['push', 'origin', 'master']),
make('git', ['push', '--tags', 'origin']),
make('npm', ['publish']),
],
function(err) {
if (err) {
if (_.isNumber(err)) {
console.log('Non-zero exit code: %d', err);
} else {
console.log('Error: ', err.message ? err.message : err);
}
}
}
});
);