11var commander = require ( "commander" ) ;
22var package = require ( '../../package.json' )
33var endOfLine = require ( 'os' ) . EOL ;
4+ var fs = require ( 'fs' ) ;
5+ var util = require ( 'util' ) ;
6+ var https = require ( 'https' ) ;
47
58commander
69 . version ( package . version )
10+ . option ( '-h, --host <host>' )
11+ . option ( '-p, --port <port>' )
712 . parse ( process . argv ) ;
813
9- var processData = function ( postData ) {
10- var https = require ( 'https' ) ;
14+ var host = commander . host ;
15+ var port = 443 ;
1116
17+ if ( commander . port ) {
18+ port = commander . port ;
19+ }
20+
21+ var fileContents = null ;
22+ if ( commander . args . length == 1 ) {
23+ var filePath = commander . args [ 0 ] ;
24+
25+ if ( fs . existsSync ( filePath ) ) {
26+ fileContents = fs . readFileSync ( filePath , 'utf8' ) ;
27+ }
28+ }
29+
30+ if ( fileContents && host && util . isNumber ( port ) ) {
1231 var options = {
13- hostname : 'central.github.com' ,
32+ protocol : "https:" ,
33+ hostname : host ,
34+ port : port ,
1435 path : '/api/usage/unity' ,
1536 method : 'POST' ,
1637 headers : {
@@ -19,49 +40,25 @@ var processData = function (postData) {
1940 } ;
2041
2142 var req = https . request ( options , function ( res ) {
22- process . stdout . write ( 'statusCode:' , res . statusCode ) ;
23-
2443 res . on ( 'data' , function ( d ) {
2544 process . stdout . write ( d ) ;
2645 process . stdout . write ( endOfLine ) ;
2746 } ) ;
2847
2948 res . on ( 'end' , function ( d ) {
30- process . exit ( ) ;
49+ process . exit ( res . statusCode == 200 ? 0 : - 1 ) ;
3150 } ) ;
3251 } ) ;
3352
3453 req . on ( 'error' , function ( e ) {
35- console . error ( e ) ;
54+ console . log ( e ) ;
3655 process . exit ( - 1 ) ;
3756 } ) ;
3857
39- req . write ( postData ) ;
58+ req . write ( fileContents ) ;
4059 req . end ( ) ;
4160}
42-
43- if ( process . stdin . isTTY ) {
44- var readlineSync = require ( "readline-sync" ) ;
45- var postData = readlineSync . question ( ) ;
46-
47- processData ( postData ) ;
48- }
4961else {
50- var data = '' ;
51- process . stdin . setEncoding ( encoding ) ;
52-
53- process . stdin . on ( 'readable' , function ( ) {
54- var chunk ;
55- while ( chunk = process . stdin . read ( ) ) {
56- data += chunk ;
57- }
58- } ) ;
59-
60- process . stdin . on ( 'end' , function ( ) {
61- var items = data . toString ( )
62- . split ( / \r ? \n / )
63- . filter ( function ( item ) { return item ; } ) ;
64-
65- processData ( items [ 0 ] ) ;
66- } ) ;
62+ commander . help ( ) ;
63+ process . exit ( - 1 ) ;
6764}
0 commit comments