Skip to content

Commit 4feb53b

Browse files
committed
update CLI to work with families
1 parent bdd310d commit 4feb53b

3 files changed

Lines changed: 40 additions & 12 deletions

File tree

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,15 @@ var snippet = new httpsnippet({
108108
});
109109

110110
// generate cURL output
111-
console.log(snippet.curl({
111+
console.log(snippet.convert('curl', {
112112
indent: '\t';
113113
}));
114114

115115
// generate nodeJS output
116116
console.log(snippet.convert('node'));
117+
118+
// generate PHP output
119+
console.log(snippet.convert('php', 'curl'));
117120
```
118121

119122
## Documentation
@@ -136,11 +139,12 @@ module.exports = function (opts) {
136139
module.exports.info = function () {
137140
// return target info
138141
return {
139-
key: 'node', // target key
142+
family: 'node', // target family
143+
key: 'native', // target key
140144
ext: '.js', // preferred extension
141145
title: '', // target label
142146
description: '' // target description
143-
}
147+
};
144148
};
145149
```
146150

bin/httpsnippet

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ cmd
1515
.version(pkg.version)
1616
.usage('[options] <file>')
1717
.option('-t, --target <target>', 'target ouput')
18+
.option('-f, --family [family]', 'target family')
1819
.option('-o, --output <directory>', 'write output to directory')
1920
.option('-n, --output-name <name>', 'output file name')
2021
.parse(process.argv);
@@ -23,6 +24,12 @@ if (!cmd.args.length || !cmd.target) {
2324
cmd.help();
2425
}
2526

27+
// short hand
28+
if (cmd.target && !cmd.family) {
29+
cmd.family = cmd.target;
30+
delete cmd.target;
31+
}
32+
2633
if (cmd.output) {
2734
var dir = path.resolve(cmd.output);
2835

@@ -70,7 +77,7 @@ async.waterfall([
7077
function snippet (files, sources, next) {
7178
var iterator = function (source, cb) {
7279
var snippet = new HTTPSnippet(source);
73-
cb(null, snippet[cmd.target].apply(snippet));
80+
cb(null, snippet.convert(cmd.family, cmd.target));
7481
};
7582

7683
async.map(sources, iterator, function (err, results) {
@@ -84,9 +91,11 @@ async.waterfall([
8491
var index = files.indexOf(file);
8592
var name = path.basename(file, path.extname(file));
8693

94+
console.info(cmd.family, cmd.target);
95+
8796
var filename = path.format({
8897
dir: dir,
89-
base: name + HTTPSnippet.info(cmd.target).ext
98+
base: name + HTTPSnippet.info(cmd.family, cmd.target).ext
9099
});
91100

92101
fs.writeFile(filename, snippets[index]);

src/index.js

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,21 @@ HTTPSnippet.prototype.getSource = function () {
6060
return this.source;
6161
};
6262

63-
HTTPSnippet.prototype.convert = function (familyName, target, opts) {
63+
HTTPSnippet.prototype.convert = function (family, target, opts) {
6464
if (!opts && target) {
6565
opts = target;
6666
}
6767

68+
var func = this._matchTarget(family, target);
69+
70+
if (func) {
71+
return func.call(this, opts);
72+
}
73+
74+
return false;
75+
};
76+
77+
HTTPSnippet.prototype._matchTarget = function (familyName, target) {
6878
// does it exist?
6979
if (targets[familyName] === undefined) {
7080
return false;
@@ -75,14 +85,13 @@ HTTPSnippet.prototype.convert = function (familyName, target, opts) {
7585

7686
// childless targets
7787
if (typeof family === 'function') {
78-
return family.call(this, opts);
88+
return family;
7989
}
80-
8190
// find the first possibel target
8291
var firstTarget = Object.keys(family).pop();
8392

8493
// shorthand
85-
if (typeof target === 'object') {
94+
if (!target || typeof target === 'object') {
8695
target = firstTarget;
8796
}
8897

@@ -95,7 +104,7 @@ HTTPSnippet.prototype.convert = function (familyName, target, opts) {
95104

96105
// last chance
97106
if (typeof family[target] === 'function') {
98-
return family[target].call(this, opts);
107+
return family[target];
99108
}
100109
}
101110

@@ -110,6 +119,12 @@ module.exports._targets = function () {
110119
return Object.keys(targets);
111120
};
112121

113-
module.exports.info = function (lang) {
114-
return targets[lang].info();
122+
module.exports.info = function (family, target) {
123+
var result = HTTPSnippet.prototype._matchTarget.call(null, family, target);
124+
125+
if (result) {
126+
return result.info();
127+
}
128+
129+
return false;
115130
};

0 commit comments

Comments
 (0)