From 020a52d7729774dfa6aba9a53144335d0f980ccc Mon Sep 17 00:00:00 2001 From: Ahmad Nassri Date: Sat, 21 Mar 2015 17:04:39 -0700 Subject: [PATCH] minor clean ups, docs update, syntax clean for bin --- .gitignore | 3 +- LICENSE | 1 - README.md | 22 +++++------ bin/httpsnippet | 102 ++++++++++++++++++++++++------------------------ 4 files changed, 63 insertions(+), 65 deletions(-) diff --git a/.gitignore b/.gitignore index 382f19e29..dddca890c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,3 @@ -.DS_Store -npm-debug.log +*.log node_modules coverage* diff --git a/LICENSE b/LICENSE index 8eac993a2..fbab00b10 100644 --- a/LICENSE +++ b/LICENSE @@ -19,4 +19,3 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - diff --git a/README.md b/README.md index 7e96543e7..dab02e651 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # HTTP Snippet [![version][npm-version]][npm-url] [![License][npm-license]][license-url] -HTTP Request snippet generator for *many* languages. +HTTP Request snippet generator for *[many](https://github.com/Mashape/httpsnippet/wiki/Targets)* languages & tools. Relies on the popular [HAR](http://www.softwareishard.com/blog/har-12-spec/#request) format to import data and describe HTTP calls. @@ -14,8 +14,6 @@ See it in action on companion service: [APIembed](https://apiembed.com/) ## Install -install from source or through [npm](https://www.npmjs.com/): - ```shell # to use in cli npm install --global httpsnippet @@ -27,16 +25,18 @@ npm install --save httpsnippet ## Usage ``` -Usage: httpsnippet [options] -Options: + Usage: httpsnippet [options] + + Options: + + -h, --help output usage information + -V, --version output the version number + -t, --target target output + -c, --client [client] target client library + -o, --output write output to directory + -n, --output-name output file name - -h, --help output usage information - -V, --version output the version number - -t, --target target output - -c, --client [client] target client library - -o, --output write output to directory - -n, --output-name output file name ``` ###### Example diff --git a/bin/httpsnippet b/bin/httpsnippet index 5777d2a89..0a0fa6565 100755 --- a/bin/httpsnippet +++ b/bin/httpsnippet @@ -1,125 +1,125 @@ #!/usr/bin/env node -'use strict'; +'use strict' -var async = require('async'); -var chalk = require('chalk'); -var cmd = require('commander'); -var debug = require('debug')('httpsnippet'); -var fs = require('fs'); -var HTTPSnippet = require('../src'); -var path = require('path'); -var pkg = require('../package.json'); +var async = require('async') +var chalk = require('chalk') +var cmd = require('commander') +var debug = require('debug')('httpsnippet') +var fs = require('fs') +var HTTPSnippet = require('../src') +var path = require('path') +var pkg = require('../package.json') cmd .version(pkg.version) - .usage('[options] ') + .usage('[options] ') .option('-t, --target ', 'target output') .option('-c, --client [client]', 'target client library') .option('-o, --output ', 'write output to directory') .option('-n, --output-name ', 'output file name') - .parse(process.argv); + .parse(process.argv) if (!cmd.args.length || !cmd.target) { - cmd.help(); + cmd.help() } if (cmd.output) { - var dir = path.resolve(cmd.output); + var dir = path.resolve(cmd.output) if (!fs.existsSync(dir)) { - fs.mkdirSync(dir); + fs.mkdirSync(dir) } } async.waterfall([ function isFile (next) { var iterator = function (item, cb) { - cb(fs.statSync(item).isFile()); - }; + cb(fs.statSync(item).isFile()) + } async.filter(cmd.args, iterator, function (results) { - next(null, results); - }); + next(null, results) + }) }, function read (files, next) { var iterator = function (file, cb) { - fs.readFile(file, cb); - }; + fs.readFile(file, cb) + } async.map(files, iterator, function (err, results) { - next(err, files, results); - }); + next(err, files, results) + }) }, function parse (files, buffers, next) { var iterator = function (buffer, cb) { try { - cb(null, JSON.parse(buffer)); + cb(null, JSON.parse(buffer)) } catch (e) { - debug('failed to parse source json'); - cb('failed to parse source json', null); + debug('failed to parse source json') + cb('failed to parse source json', null) } - }; + } async.map(buffers, iterator, function (err, results) { - next(err, files, results); - }); + next(err, files, results) + }) }, function snippet (files, sources, next) { var iterator = function (source, cb) { - var snippet; + var snippet try { - snippet = new HTTPSnippet(source); + snippet = new HTTPSnippet(source) } catch (e) { - debug(e); + debug(e) - return cb(!e[0] ? 'invalid input' : (e[0].field + ' ' + e[0].message), null); + return cb(!e[0] ? 'invalid input' : (e[0].field + ' ' + e[0].message), null) } - cb(null, snippet.convert(cmd.target, cmd.client)); - }; + cb(null, snippet.convert(cmd.target, cmd.client)) + } async.map(sources, iterator, function (err, results) { - next(err, files, results); - }); + next(err, files, results) + }) }, function writeOutput (files, snippets, next) { if (cmd.output) { var iterator = function (file) { - var index = files.indexOf(file); - var name = path.basename(file, path.extname(file)); + var index = files.indexOf(file) + var name = path.basename(file, path.extname(file)) var filename = path.format({ dir: dir, base: name + HTTPSnippet.extname(cmd.target) - }); + }) - fs.writeFile(filename, snippets[index] + '\n'); - }; + fs.writeFile(filename, snippets[index] + '\n') + } - async.each(files, iterator); + async.each(files, iterator) } - next(null, files, snippets); + next(null, files, snippets) }, function log (files, snippets, next) { if (!cmd.output) { var iterator = function (file) { - var index = files.indexOf(file); - console.log('%s:\n%s\n', chalk.cyan.bold.underline(file), snippets[index]); - }; + var index = files.indexOf(file) + console.log('%s:\n%s\n', chalk.cyan.bold.underline(file), snippets[index]) + } - async.each(files, iterator); + async.each(files, iterator) } } ], function (err, result) { - if (err) { - console.log('%s: %s', chalk.red.bold('ERROR'), err); - } -}); + if (err) { + console.log('%s: %s', chalk.red.bold('ERROR'), err) + } + })