Skip to content

Commit e4d262c

Browse files
committed
update cli to work with new class structure
1 parent c0c8ba4 commit e4d262c

2 files changed

Lines changed: 14 additions & 10 deletions

File tree

bin/httpsnippet

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,23 @@
22

33
'use strict';
44

5-
var commander = require('commander');
5+
var cmd = require('commander');
66
var debug = require('debug')('httpsnippet');
77
var fs = require('fs');
8-
var httpsnippet = require('../src');
8+
var HTTPSnippet = require('../src');
99
var pkg = require('../package.json');
1010

11-
commander
11+
cmd
1212
.version(pkg.version)
1313
.usage('<file> [options]')
1414
.option('-l, --language <language>', 'target language')
1515
.parse(process.argv);
1616

17-
if (commander.args.length === 0 || !commander.language) {
18-
commander.help();
17+
if (cmd.args.length === 0 || !cmd.language) {
18+
cmd.help();
1919
}
2020

21-
commander.args.map(function (file) {
21+
cmd.args.map(function (file) {
2222
fs.stat(file, function (err, stats) {
2323
if (err) {
2424
return debug(err);
@@ -30,11 +30,13 @@ commander.args.map(function (file) {
3030
return debug(err);
3131
}
3232

33-
var har = JSON.parse(data);
33+
var source = JSON.parse(data);
3434

35-
var code = httpsnippet(har, commander.language);
35+
var snippet = new HTTPSnippet(source);
3636

37-
console.log(code);
37+
var code = snippet[cmd.language].apply(snippet);
38+
39+
console.log(code, '\n');
3840
});
3941
}
4042
});

src/targets/curl.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ module.exports = function (options) {
1818
code.push(opts.short ? '-0' : '--http1.0');
1919
}
2020

21-
// construct cookies argument
21+
// construct cookies
2222
if (this.source.cookies && this.source.cookies.length) {
2323
var cookies = this.source.cookies.map(function (cookie) {
2424
return encodeURIComponent(cookie.name) + '=' + encodeURIComponent(cookie.value);
@@ -27,12 +27,14 @@ module.exports = function (options) {
2727
code.push(util.format('%s "%s"', opts.short ? '-b' : '--cookie', cookies.join('; ')));
2828
}
2929

30+
// construct headers
3031
if (this.source.headers && this.source.headers.length) {
3132
this.source.headers.map(function (header) {
3233
code.push(util.format('%s "%s: %s"', opts.short ? '-H' : '--header', header.name, header.value));
3334
});
3435
}
3536

37+
// request body
3638
if (this.source.postData) {
3739
code.push(util.format('%s %s', opts.short ? '-d' : '--data', JSON.stringify(this.source.postData.text)));
3840
}

0 commit comments

Comments
 (0)