1
1
#!/usr/bin/env node
2
2
'use strict' ;
3
3
4
+ const Bossy = require ( 'bossy' ) ;
4
5
const Fs = require ( 'fs' ) ;
5
6
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
+ } ;
6
35
7
36
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
+ } ;
9
53
10
54
11
- internals . stdin = function ( stream , callback ) {
55
+
56
+ internals . parseStream = function ( stream ) {
12
57
13
58
let buf = '' ;
14
59
stream . setEncoding ( 'utf8' ) ;
@@ -20,56 +65,78 @@ internals.stdin = function (stream, callback){
20
65
21
66
stream . on ( 'end' , ( ) => {
22
67
23
- callback ( buf ) ;
68
+ try {
69
+ internals . parse ( buf ) ;
70
+ }
71
+ catch ( e ) {
72
+ internals . error ( e ) ;
73
+ }
24
74
} ) . resume ( ) ;
25
75
} ;
26
76
27
77
28
- internals . parse = function ( str , depth ) {
78
+ internals . logparse = function ( stream ) {
29
79
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 } ) ;
37
110
} ;
38
111
39
112
40
113
internals . main = function ( ) {
41
114
42
115
let stream = process . stdin ;
43
116
117
+ internals . args = internals . initalizeBossy ( ) ;
118
+
44
119
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 ) {
59
121
stream = process . stdin ;
122
+ internals . parseStream ( stream ) ;
60
123
}
61
124
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
+ }
64
136
}
65
- internals . stdin ( stream , ( str ) => {
66
-
67
- internals . parse ( str , depth ) ;
68
- } ) ;
69
137
}
70
138
catch ( e ) {
71
- Purdy ( e ) ;
72
- process . exit ( 1 ) ;
139
+ internals . error ( e ) ;
73
140
}
74
141
} ;
75
142
0 commit comments