1
1
#!/usr/bin/env node
2
2
3
- const yargs = require ( 'yargs/yargs' )
4
- const { hideBin } = require ( 'yargs/helpers' )
5
- const RawProto = require ( './dist/rawproto.cjs' )
3
+ import yargs from 'yargs/yargs'
4
+ import { hideBin } from 'yargs/helpers'
5
+ import RawProto from './dist/rawproto.modern.js'
6
+ import { readFile } from 'fs/promises'
6
7
7
8
async function getStdin ( ) {
8
9
const chunks = [ ]
9
10
for await ( const chunk of process . stdin ) chunks . push ( chunk )
10
11
return Buffer . concat ( chunks )
11
12
}
12
13
13
- async function actionJson ( { types } ) {
14
- const p = new RawProto ( await getStdin ( ) , types )
15
- console . log ( JSON . stringify ( p . toJS ( ) , null , 2 ) )
14
+ async function actionJson ( { types, prefix } ) {
15
+ if ( types ) {
16
+ types = JSON . parse ( await readFile ( types ) )
17
+ } else {
18
+ types = { }
19
+ }
20
+ const j = new RawProto ( await getStdin ( ) ) . toJS ( types , prefix )
21
+ console . log ( JSON . stringify ( j , null , 2 ) )
16
22
}
17
23
18
- async function actionProto ( { types } ) {
19
- const p = new RawProto ( await getStdin ( ) , types )
20
- console . log ( p . toProto ( ) )
24
+ async function actionProto ( { types, prefix } ) {
25
+ if ( types ) {
26
+ types = JSON . parse ( await readFile ( types ) )
27
+ } else {
28
+ types = { }
29
+ }
30
+ const p = new RawProto ( await getStdin ( ) , types ) . toProto ( types , prefix )
31
+ console . log ( p )
21
32
}
22
33
23
- async function actionQuery ( { query, types } ) {
24
- const p = new RawProto ( await getStdin ( ) , types )
34
+ async function actionQuery ( { query } ) {
35
+ const p = new RawProto ( await getStdin ( ) )
25
36
console . log ( JSON . stringify ( p . query ( query ) , null , 2 ) )
26
37
}
27
38
@@ -36,8 +47,15 @@ yargs(hideBin(process.argv))
36
47
. positional ( 'types' , {
37
48
describe : 'JSON file that selects types for fields'
38
49
} )
50
+ . option ( 'prefix' , {
51
+ alias : 'p' ,
52
+ type : 'string' ,
53
+ description : 'Path-prefix to add to fields on output' ,
54
+ default : 'f'
55
+ } )
39
56
. example ( 'cat myfile.pb | $0 json' , 'Get the JSON from myfile.pb, guessing types' )
40
57
. example ( '$0 json < myfile.pb' , 'Get the JSON from myfile.pb, guessing types' )
58
+ . example ( '$0 json -p "" < myfile.pb' , 'Get the JSON from myfile.pb, guessing types, oputput no field-prefixes' )
41
59
. example ( '$0 json choices.json < myfile.pb' , 'Get the JSON from myfile.pb, using choices.json for types' )
42
60
} , ( argv ) => {
43
61
actionJson ( argv )
@@ -47,33 +65,35 @@ yargs(hideBin(process.argv))
47
65
. positional ( 'types' , {
48
66
describe : 'JSON file that selects types for fields'
49
67
} )
68
+ . option ( 'prefix' , {
69
+ alias : 'p' ,
70
+ type : 'string' ,
71
+ description : 'Path-prefix to add to fields on output' ,
72
+ default : 'f'
73
+ } )
50
74
. example ( 'cat myfile.pb | $0 proto' , 'Get the proto from myfile.pb, guessing types' )
51
75
. example ( '$0 proto < myfile.pb' , 'Get the proto from myfile.pb, guessing types' )
52
76
. example ( '$0 proto choices.json < myfile.pb' , 'Get the proto from myfile.pb, using choices.json for types' )
53
77
} , ( argv ) => {
54
78
actionProto ( argv )
55
79
} )
56
- . command ( 'query <query> [types] ' , 'Output JSON (on stdout) for a specific query, for binary protobuf (on stdin)' , ( yargs ) => {
80
+ . command ( 'query <query>' , 'Output JSON (on stdout) for a specific query, for binary protobuf (on stdin)' , ( yargs ) => {
57
81
return yargs
58
82
. positional ( 'query' , {
59
83
describe : 'Your protobuf query string'
60
84
} )
61
- . positional ( 'types' , {
62
- describe : 'JSON file that selects types for fields'
63
- } )
64
85
. example ( 'cat myfile.pb | $0 query 1.2.4.10.5:string' , 'Get the query from myfile.pb' )
65
86
. example ( '$0 query 1.2.4.10.5:string < myfile.pb' , 'Get the query from myfile.pb' )
66
- . example ( '$0 query 1.2.4.10.5 choices.json < myfile.pb' , 'Get the query from myfile.pb, using choices.json for types' )
67
87
} , ( argv ) => {
68
88
actionQuery ( argv )
69
89
} )
70
- . command ( '$0 types' , 'Output JSON types guess (on stdout) for binary protobuf (on stdin)' , ( yargs ) => {
71
- return yargs
72
- . example ( 'cat myfile.pb | $0 types' , 'Get the types from myfile.pb' )
73
- . example ( '$0 types < myfile.pb' , 'Get the types from myfile.pb' )
74
- } , ( argv ) => {
75
- actionTypes ( argv )
76
- } )
90
+ // .command('$0 types', 'Output JSON types guess (on stdout) for binary protobuf (on stdin)', (yargs) => {
91
+ // return yargs
92
+ // .example('cat myfile.pb | $0 types', 'Get the types from myfile.pb')
93
+ // .example('$0 types < myfile.pb', 'Get the types from myfile.pb')
94
+ // }, (argv) => {
95
+ // actionTypes(argv)
96
+ // })
77
97
. example ( '$0 json --help' , 'Get more detailed help on json' )
78
98
. example ( '$0 proto --help' , 'Get more detailed help on proto' )
79
99
. example ( '$0 query --help' , 'Get more detailed help on query' )
0 commit comments