Skip to content

Commit 2ecdbc8

Browse files
committed
update bin script
1 parent 5711d6d commit 2ecdbc8

File tree

2 files changed

+101
-33
lines changed

2 files changed

+101
-33
lines changed

bin/purdy.js

Lines changed: 100 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,59 @@
11
#!/usr/bin/env node
22
'use strict';
33

4+
const Bossy = require('bossy');
45
const Fs = require('fs');
56
const Purdy = require('../');
7+
const ReadLine = require('readline');
8+
9+
10+
const internals = {
11+
definition: {
12+
h: {
13+
alias: 'help',
14+
description: 'Show help',
15+
type: 'boolean'
16+
},
17+
d: {
18+
alias: 'depth',
19+
description: 'depth to show',
20+
default: 2,
21+
type: 'number'
22+
},
23+
's': {
24+
alias: 'stdin',
25+
description: 'parse from stdin',
26+
type: 'boolean'
27+
},
28+
'l': {
29+
alias: 'log',
30+
description: 'file is in log format with one JSON string per line',
31+
type: 'boolean'
32+
}
33+
}
34+
};
635

736

8-
const internals = {};
37+
internals.initalizeBossy = function () {
38+
39+
const args = Bossy.parse(internals.definition);
40+
41+
if (args instanceof Error) {
42+
console.error(args.message);
43+
process.exit(1);
44+
}
45+
46+
if (args.h) {
47+
console.log(Bossy.usage(internals.definition, 'node start.js'));
48+
process.exit(0);
49+
}
50+
51+
return args;
52+
};
953

1054

11-
internals.stdin = function (stream, callback){
55+
56+
internals.parseStream = function (stream){
1257

1358
let buf = '';
1459
stream.setEncoding('utf8');
@@ -20,56 +65,78 @@ internals.stdin = function (stream, callback){
2065

2166
stream.on('end', () => {
2267

23-
callback(buf);
68+
try {
69+
internals.parse(buf);
70+
}
71+
catch (e) {
72+
internals.error(e);
73+
}
2474
}).resume();
2575
};
2676

2777

28-
internals.parse = function (str, depth) {
78+
internals.logparse = function (stream){
2979

30-
try {
31-
Purdy(JSON.parse(str), { depth });
32-
}
33-
catch (e) {
34-
Purdy(e);
35-
process.exit(1);
36-
}
80+
stream.setEncoding('utf8');
81+
82+
const lineReader = ReadLine.createInterface({
83+
input: stream
84+
});
85+
86+
lineReader.on('line', (line) => {
87+
88+
try {
89+
internals.parse(line);
90+
}
91+
catch (e) {
92+
internals.error(e);
93+
}
94+
});
95+
};
96+
97+
98+
internals.error = function (e) {
99+
100+
Purdy(e);
101+
process.exit(1);
102+
};
103+
104+
105+
internals.parse = function (str) {
106+
107+
const depth = internals.args.depth;
108+
109+
Purdy(JSON.parse(str), { depth });
37110
};
38111

39112

40113
internals.main = function () {
41114

42115
let stream = process.stdin;
43116

117+
internals.args = internals.initalizeBossy();
118+
44119
try {
45-
const depthIdx = process.argv.indexOf('--depth');
46-
let depth = 2;
47-
if (depthIdx !== -1) {
48-
const pair = process.argv.splice(depthIdx, 2);
49-
depth = parseFloat(pair[1]);
50-
51-
if (String(depth) === 'NaN') {
52-
const e = new Error('Depth requires a numerical value');
53-
e.depth = pair[1];
54-
Purdy(e);
55-
process.exit(1);
56-
}
57-
}
58-
if (process.argv[2] === '-') {
120+
if (internals.args.stdin) {
59121
stream = process.stdin;
122+
internals.parseStream(stream);
60123
}
61124
else {
62-
const path = Fs.realpathSync(process.argv[2]);
63-
stream = Fs.createReadStream(path);
125+
for (let i = 0; i < internals.args._.length; ++i) {
126+
const file = internals.args._[i];
127+
const path = Fs.realpathSync(file);
128+
stream = Fs.createReadStream(path);
129+
if (internals.args.log) {
130+
internals.logparse(stream);
131+
}
132+
else {
133+
internals.parseStream(stream);
134+
}
135+
}
64136
}
65-
internals.stdin(stream, (str) => {
66-
67-
internals.parse(str, depth);
68-
});
69137
}
70138
catch (e) {
71-
Purdy(e);
72-
process.exit(1);
139+
internals.error(e);
73140
}
74141
};
75142

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"lab": "6.x.x"
3131
},
3232
"dependencies": {
33+
"bossy": "^3.0.4",
3334
"chalk": "0.4.x",
3435
"hoek": "2.x.x",
3536
"joi": "6.x.x"

0 commit comments

Comments
 (0)