From ef69a8cfad9314d03a774d055d686b14580629b7 Mon Sep 17 00:00:00 2001 From: Spencer Alger Date: Fri, 22 Nov 2013 19:19:17 -0700 Subject: [PATCH] Added client_export script which allows the clients to require elasticsearch-js via NPM, and then export the client via an NPM prepublish script. --- package.json | 1 + scripts/export_client.js | 108 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 109 insertions(+) create mode 100644 scripts/export_client.js diff --git a/package.json b/package.json index f6d62f88b..ea0f3a753 100644 --- a/package.json +++ b/package.json @@ -51,6 +51,7 @@ "test": "node scripts/run_tests.js", "build": "grunt", "generate": "node scripts/generate/js_api && node scripts/generate/yaml_tests", + "export_client": "node scripts/export_client.js", "blanket": { "pattern": "src" } diff --git a/scripts/export_client.js b/scripts/export_client.js new file mode 100644 index 000000000..59a645953 --- /dev/null +++ b/scripts/export_client.js @@ -0,0 +1,108 @@ +var path = require('path'); +var fs = require('fs'); +var format = require('util').format; +var cp = require('child_process'); + +var argv = require('optimist') + .default({ + buildName: '', + outputDir: '.', + verbose: false + }) + .alias({ + b: 'buildName', + o: 'outputDir', + v: 'verbose' + }) + .parse(JSON.parse(process.env.npm_config_argv).original); + +switch (argv.buildName) { +case 'jquery': + var buildFile = './dist/elasticsearch.jquery.js'; + var minifiedBuildFile = './dist/elasticsearch.jquery.min.js'; + break; +case 'angular': + var buildFile = './dist/elasticsearch.angular.js'; + var minifiedBuildFile = './dist/elasticsearch.angular.min.js'; + break; +default: + var buildFile = './dist/elasticsearch.js'; + var minifiedBuildFile = './dist/elasticsearch.min.js'; + break; +} + +var outputFile = path.join(argv.outputDir, 'elasticsearch.js'); +var minifiedOutputFile = path.join(argv.outputDir, 'elasticsearch.min.js'); + +var steps = [ + [runInModule, 'npm', ['run', 'build']], + [copy, buildFile, outputFile], + [copy, minifiedBuildFile, minifiedOutputFile] +]; + +(function next() { + var step = steps.shift(); + if (step) { + var fn = step.shift(); + step.push(next); + fn.apply(null, step); + } else { + console.log('Done'); + process.exit(); + } +})(); + +function log() { + var out = format.apply(console, arguments); + if (argv.verbose) { + out = '\n' + out + '\n'; + } + console.log(out); +} + +function runInModule(cmd, args, exitCb) { + log('running', cmd, args.join(' ')); + + var proc = cp.spawn(cmd, args, { + stdio: argv.verbose ? 'inherit' : 'ignore' + }); + + proc.on('error', function (err) { + console.error('Error! --', err.message); + process.exit(1); + }); + + proc.on('exit', function (status) { + if (status) { + console.error('Error! --', cmd, 'exit status was', status); + process.exit(1); + } else { + exitCb(); + } + }); +} + +function copy(from, to, done) { + log('copying', from, 'to', to); + + var read = fs.createReadStream(from); + var write = fs.createWriteStream(to); + + read.pipe(write); + + read.on('error', function (err) { + console.error('unable to read: ' + from); + console.error(err.message); + process.exit(1); + }); + + write.on('error', function (err) { + console.error('unable to write to: ' + to); + console.error(err.message); + process.exit(1); + }); + + write.on('finish', function () { + done(); + }); +}