@@ -21,6 +21,10 @@ var _mapStream = require('map-stream');
2121
2222var _mapStream2 = _interopRequireDefault ( _mapStream ) ;
2323
24+ var _split = require ( 'split' ) ;
25+
26+ var _split2 = _interopRequireDefault ( _split ) ;
27+
2428var _concatStream = require ( 'concat-stream' ) ;
2529
2630var _concatStream2 = _interopRequireDefault ( _concatStream ) ;
@@ -33,110 +37,104 @@ var _path = require('path');
3337
3438var _path2 = _interopRequireDefault ( _path ) ;
3539
36- function _interopRequireDefault ( obj ) {
37- return obj && obj . __esModule ? obj : { default : obj } ;
38- }
39-
40- var argv = _yargs2 . default
41- . usage (
42- 'Pipe $0 onto a JSON source from the commandline to parse the output:\n cat data.json | $0 [options] query'
43- )
44- . options ( {
45- p : {
46- alias : 'path' ,
47- describe : 'Use JSON Path notation (https://github.com/dchester/jsonpath)' ,
48- type : 'boolean'
49- } ,
50- k : {
51- alias : 'keys' ,
52- describe : 'Print object keys only' ,
53- type : 'boolean'
54- } ,
55- f : {
56- alias : 'file' ,
57- describe : 'Read input from file' ,
58- requiresArg : true ,
59- type : 'string'
60- } ,
61- i : {
62- alias : 'indent' ,
63- describe : 'Number of spaces for indentation (ignored by --human)' ,
64- requiresArg : true ,
65- default : 2 ,
66- type : 'number' ,
67- coerce : function coerce ( x ) {
68- return ! isNaN ( parseFloat ( x ) ) && isFinite ( x ) ? + x : x ;
69- }
70- } ,
71- h : {
72- alias : 'human' ,
73- describe : 'Print human-readable (non-JSON) format' ,
74- type : 'boolean'
75- } ,
76- b : {
77- alias : 'break' ,
78- describe : 'Set break length of object/array for human format' ,
79- requiresArg : true ,
80- type : 'number'
81- } ,
82- c : {
83- alias : 'no-color' ,
84- describe : 'Disable color for human format' ,
85- type : 'boolean'
86- } ,
87- d : {
88- alias : 'depth' ,
89- describe : 'Depth for human format' ,
90- requiresArg : true ,
91- type : 'number'
92- }
93- } )
94- . help ( )
95- . epilogue (
96- 'Queries use the Lodash get method by default.\nFor more information, see https://github.com/therealklanni/jp'
97- ) . argv ;
98-
99- var format = argv . human
100- ? function ( x ) {
101- return x ;
40+ function _interopRequireDefault ( obj ) { return obj && obj . __esModule ? obj : { default : obj } ; }
41+
42+ var argv = _yargs2 . default . usage ( 'Pipe $0 onto a JSON source from the commandline to parse the output:\n cat data.json | $0 [options] query' ) . options ( {
43+ p : {
44+ alias : 'path' ,
45+ describe : 'Use JSON Path notation (https://github.com/dchester/jsonpath)' ,
46+ type : 'boolean'
47+ } ,
48+ k : {
49+ alias : 'keys' ,
50+ describe : 'Print object keys only' ,
51+ type : 'boolean'
52+ } ,
53+ f : {
54+ alias : 'file' ,
55+ describe : 'Read input from file' ,
56+ requiresArg : true ,
57+ type : 'string'
58+ } ,
59+ i : {
60+ alias : 'indent' ,
61+ describe : 'Number of spaces for indentation (ignored by --human)' ,
62+ requiresArg : true ,
63+ default : 2 ,
64+ type : 'number' ,
65+ coerce : function coerce ( x ) {
66+ return ! isNaN ( parseFloat ( x ) ) && isFinite ( x ) ? Number ( x ) : x ;
10267 }
103- : _lodash2 . default . partialRight ( JSON . stringify , null , argv . indent || 2 ) ;
68+ } ,
69+ h : {
70+ alias : 'human' ,
71+ describe : 'Print human-readable (non-JSON) format' ,
72+ type : 'boolean'
73+ } ,
74+ b : {
75+ alias : 'break' ,
76+ describe : 'Set break length of object/array for human format' ,
77+ requiresArg : true ,
78+ type : 'number'
79+ } ,
80+ c : {
81+ alias : 'no-color' ,
82+ describe : 'Disable color for human format' ,
83+ type : 'boolean'
84+ } ,
85+ d : {
86+ alias : 'depth' ,
87+ describe : 'Depth for human format' ,
88+ requiresArg : true ,
89+ type : 'number'
90+ } ,
91+ L : {
92+ alias : 'line-by-line' ,
93+ describe : 'Parse each line as a separate input' ,
94+ type : 'boolean'
95+ }
96+ } ) . help ( ) . epilogue ( 'Queries use the Lodash get method by default.\nFor more information, see https://github.com/therealklanni/jp' ) . argv ;
97+
98+ var format = argv . human ? function ( x ) {
99+ return x ;
100+ } : _lodash2 . default . partialRight ( JSON . stringify , null , argv . indent || 2 ) ;
101+
102+ var logOpts = {
103+ colors : ! argv . noColor ,
104+ breakLength : argv . break || null ,
105+ depth : argv . depth >= 0 ? argv . depth : null
106+ } ;
104107
105- var log = argv . human
106- ? _lodash2 . default . partialRight ( console . dir . bind ( console ) , {
107- colors : ! argv . noColor ,
108- breakLength : argv . break || null ,
109- depth : argv . depth >= 0 ? argv . depth : null
110- } )
111- : console . log . bind ( console ) ;
108+ var log = argv . human ? _lodash2 . default . partialRight ( console . dir . bind ( console ) , logOpts ) : console . log . bind ( console ) ;
112109
113110var print = _lodash2 . default . flow ( format , log ) ;
114111
115112var query = argv . _ [ 0 ] ;
116113
114+ var parseBuf = function parseBuf ( buf ) {
115+ var obj = JSON . parse ( buf . toString ( ) ) ;
116+ var output = void 0 ;
117+
118+ if ( argv . path ) {
119+ output = query ? _jsonpath2 . default . query ( obj , query ) : obj ;
120+ } else {
121+ output = query ? _lodash2 . default . get ( obj , query ) : obj ;
122+ }
123+
124+ print ( argv . keys ? Object . keys ( output ) : output ) ;
125+ } ;
126+
117127var parse = function parse ( stream ) {
118- stream . pipe ( ( 0 , _utf8Stream2 . default ) ( ) ) . pipe (
119- ( 0 , _concatStream2 . default ) ( function ( buf ) {
120- var obj = JSON . parse ( buf . toString ( ) ) ;
121- var output = void 0 ;
122-
123- if ( argv . path ) {
124- output = query ? _jsonpath2 . default . query ( obj , query ) : obj ;
125- } else {
126- output = query ? _lodash2 . default . get ( obj , query ) : obj ;
127- }
128-
129- print ( argv . keys ? Object . keys ( output ) : output ) ;
130- } )
131- ) ;
128+ return stream . pipe ( ( 0 , _utf8Stream2 . default ) ( ) )
129+ // Use utf8 effectively as a noop
130+ . pipe ( argv . L ? ( 0 , _split2 . default ) ( ) : ( 0 , _utf8Stream2 . default ) ( ) ) . pipe ( argv . L ? ( 0 , _mapStream2 . default ) ( parseBuf ) : ( 0 , _concatStream2 . default ) ( parseBuf ) ) ;
132131} ;
133132
134133if ( ! process . stdin . isTTY ) {
135134 parse ( process . stdin ) ;
136135} else if ( argv . file ) {
137- parse (
138- _fs2 . default . createReadStream ( _path2 . default . resolve ( argv . file ) , 'utf8' )
139- ) ;
136+ parse ( _fs2 . default . createReadStream ( _path2 . default . resolve ( argv . file ) , 'utf8' ) ) ;
140137} else {
141138 _yargs2 . default . showHelp ( ) ;
142139}
140+
0 commit comments