@@ -17,15 +17,17 @@ var protobuf = require(".."),
1717 * @returns {number|undefined } Exit code, if known
1818 */
1919exports . main = function ( args , callback ) {
20+ var lintDefault = "eslint-disable block-scoped-var, no-redeclare, no-control-regex" ;
2021 var argv = minimist ( args , {
2122 alias : {
2223 target : "t" ,
2324 out : "o" ,
2425 path : "p" ,
2526 wrap : "w" ,
26- root : "r"
27+ root : "r" ,
28+ lint : "l"
2729 } ,
28- string : [ "target" , "out" , "path" , "wrap" , "root" ] ,
30+ string : [ "target" , "out" , "path" , "wrap" , "root" , "lint" ] ,
2931 boolean : [ "keep-case" , "create" , "encode" , "decode" , "verify" , "convert" , "delimited" , "beautify" , "comments" ] ,
3032 default : {
3133 target : "json" ,
@@ -36,7 +38,8 @@ exports.main = function(args, callback) {
3638 convert : true ,
3739 delimited : true ,
3840 beautify : true ,
39- comments : true
41+ comments : true ,
42+ lint : lintDefault
4043 }
4144 } ) ;
4245
@@ -52,9 +55,9 @@ exports.main = function(args, callback) {
5255 callback ( Error ( "usage" ) ) ;
5356 else
5457 console . error ( [
55- "protobuf.js v" + pkg . version + " cli " ,
58+ "protobuf.js v" + pkg . version + " CLI for JavaScript " ,
5659 "" ,
57- "Consolidates imports and converts between file formats." ,
60+ chalk . bold . white ( "Consolidates imports and converts between file formats." ) ,
5861 "" ,
5962 " -t, --target Specifies the target format. Also accepts a path to require a custom target." ,
6063 "" ,
@@ -64,21 +67,26 @@ exports.main = function(args, callback) {
6467 "" ,
6568 " -o, --out Saves to a file instead of writing to stdout." ,
6669 "" ,
67- " Module targets only:" ,
70+ chalk . bold . gray ( " Module targets only:" ) ,
6871 "" ,
6972 " -w, --wrap Specifies the wrapper to use. Also accepts a path to require a custom wrapper." ,
7073 "" ,
7174 " default Default wrapper supporting both CommonJS and AMD" ,
72- " commonjs CommonJS only wrapper" ,
73- " amd AMD only wrapper" ,
75+ " commonjs CommonJS wrapper" ,
76+ " amd AMD wrapper" ,
77+ " es6 ES6 wrapper" ,
7478 "" ,
7579 " -r, --root Specifies an alternative protobuf.roots name." ,
7680 "" ,
77- " Proto sources only:" ,
81+ " -l, --lint Linter configuration. Defaults to protobuf.js-compatible rules:" ,
82+ "" ,
83+ " " + lintDefault ,
84+ "" ,
85+ chalk . bold . gray ( " Proto sources only:" ) ,
7886 "" ,
7987 " --keep-case Keeps field casing instead of converting to camel case (not recommended)." ,
8088 "" ,
81- " Static targets only:" ,
89+ chalk . bold . gray ( " Static targets only:" ) ,
8290 "" ,
8391 " --no-create Does not generate create functions used for runtime compatibility." ,
8492 " --no-encode Does not generate encode functions." ,
@@ -89,7 +97,7 @@ exports.main = function(args, callback) {
8997 " --no-beautify Does not beautify generated code." ,
9098 " --no-comments Does not output any JSDoc comments." ,
9199 "" ,
92- "usage: " + chalk . bold . green ( "pbjs" ) + " [options] file1.proto file2.json ..."
100+ "usage: " + chalk . bold . green ( "pbjs" ) + " [options] file1.proto file2.json ..." + chalk . gray ( " (or) " ) + "other | " + chalk . bold . green ( "pbjs" ) + " [options] -"
93101 ] . join ( "\n" ) ) ;
94102 return 1 ;
95103 }
@@ -127,30 +135,58 @@ exports.main = function(args, callback) {
127135 "keepCase" : argv [ "keep-case" ] || false
128136 } ;
129137
130- try {
131- root . loadSync ( files , parseOptions ) ; // sync is deterministic while async is not
132- } catch ( err ) {
133- if ( callback ) {
134- callback ( err ) ;
135- return undefined ;
136- }
137- throw err ;
138- }
138+ // Read from stdin
139+ if ( files . length === 1 && files [ 0 ] === "-" ) {
140+ var data = [ ] ;
141+ process . stdin . on ( "data" , function ( chunk ) {
142+ data . push ( chunk ) ;
143+ } ) ;
144+ process . stdin . on ( "end" , function ( ) {
145+ var source = Buffer . concat ( data ) . toString ( "utf8" ) ;
146+ if ( source . charAt ( 0 ) !== "{" ) {
147+ protobuf . parse ( source , root , parseOptions ) ;
148+ } else {
149+ var json = JSON . parse ( source ) ;
150+ root . setOptions ( json . options ) . addJSON ( json ) ;
151+ }
152+ callTarget ( ) ;
153+ } ) ;
139154
140- target ( root , argv , function targetCallback ( err , output ) {
141- if ( err ) {
142- if ( callback )
143- return callback ( err ) ;
155+ // Load from disk
156+ } else {
157+ try {
158+ root . loadSync ( files , parseOptions ) ; // sync is deterministic while async is not
159+ callTarget ( ) ;
160+ } catch ( err ) {
161+ if ( callback ) {
162+ callback ( err ) ;
163+ return undefined ;
164+ }
144165 throw err ;
145166 }
146- if ( output !== "" ) {
147- if ( argv . out )
148- fs . writeFileSync ( argv . out , output , { encoding : "utf8" } ) ;
149- else
150- process . stdout . write ( output , "utf8" ) ;
151- }
152- return callback
153- ? callback ( null )
154- : undefined ;
155- } ) ;
167+ }
168+
169+ function callTarget ( ) {
170+ target ( root , argv , function targetCallback ( err , output ) {
171+ if ( err ) {
172+ if ( callback )
173+ return callback ( err ) ;
174+ throw err ;
175+ }
176+ if ( output !== "" ) {
177+ output = [
178+ "// $> pbjs " + args . join ( " " ) ,
179+ "// Generated " + ( new Date ( ) ) . toUTCString ( ) . replace ( / G M T / , "UTC" ) ,
180+ ""
181+ ] . join ( "\n" ) + "\n" + output ;
182+ if ( argv . out )
183+ fs . writeFileSync ( argv . out , output , { encoding : "utf8" } ) ;
184+ else
185+ process . stdout . write ( output , "utf8" ) ;
186+ }
187+ return callback
188+ ? callback ( null )
189+ : undefined ;
190+ } ) ;
191+ }
156192} ;
0 commit comments