Skip to content

Commit a8c94e4

Browse files
committed
updated cli
1 parent 3e428f8 commit a8c94e4

2 files changed

Lines changed: 32 additions & 18 deletions

File tree

bin/httpsnippet

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,38 +6,50 @@ var cmd = require('commander');
66
var debug = require('debug')('httpsnippet');
77
var fs = require('fs');
88
var HTTPSnippet = require('../src');
9+
var path = require('path');
910
var pkg = require('../package.json');
11+
var chalk = require('chalk');
1012

1113
cmd
1214
.version(pkg.version)
13-
.usage('<file> [options]')
15+
.usage('[options] <file>')
1416
.option('-l, --language <language>', 'target language')
17+
.option('-o, --output <directory>', 'write output to directory')
18+
.option('-n, --output-name <name>', 'output file name')
1519
.parse(process.argv);
1620

17-
if (cmd.args.length === 0 || !cmd.language) {
21+
if (!cmd.args.length || !cmd.language) {
1822
cmd.help();
1923
}
2024

25+
var output = {};
26+
2127
cmd.args.map(function (file) {
22-
fs.stat(file, function (err, stats) {
23-
if (err) {
24-
return debug(err);
25-
}
28+
var stats = fs.statSync(file);
29+
30+
if (stats.isFile()) {
31+
var data = fs.readFileSync(file);
2632

27-
if (stats.isFile()) {
28-
fs.readFile(file, function (err, data) {
29-
if (err) {
30-
return debug(err);
31-
}
33+
var source = JSON.parse(data);
3234

33-
var source = JSON.parse(data);
35+
var snippet = new HTTPSnippet(source);
36+
var code = snippet[cmd.language].apply(snippet);
37+
38+
output[file] = code;
39+
}
40+
});
3441

35-
var snippet = new HTTPSnippet(source);
42+
Object.keys(output).map(function (file) {
43+
if (cmd.output) {
44+
var name = path.basename(file, path.extname(file));
3645

37-
var code = snippet[cmd.language].apply(snippet);
46+
var filename = path.format({
47+
dir: path.resolve(cmd.output),
48+
base: name + HTTPSnippet.extname(cmd.language)
49+
});
3850

39-
console.log(code, '\n');
40-
});
41-
}
42-
});
51+
fs.writeFile(filename, output[file]);
52+
} else {
53+
console.log(chalk.cyan.bold.underline(file), chalk.cyan.bold(':'), '\n', output[file], '\n');
54+
}
4355
});

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@
1717
},
1818
"homepage": "https://github.com/ahmadnassri/httpsnippet",
1919
"devDependencies": {
20+
"async": "^0.9.0",
2021
"mocha": "^2.1.0",
2122
"should": "^5.0.1"
2223
},
2324
"dependencies": {
25+
"chalk": "^1.0.0",
2426
"commander": "^2.6.0",
2527
"debug": "^2.1.1",
2628
"requireindex": "^1.1.0"

0 commit comments

Comments
 (0)