1
1
var commander = require ( "commander" ) ;
2
2
var package = require ( '../../package.json' )
3
3
var endOfLine = require ( 'os' ) . EOL ;
4
+ var fs = require ( 'fs' ) ;
5
+ var util = require ( 'util' ) ;
6
+ var https = require ( 'https' ) ;
4
7
5
8
commander
6
9
. version ( package . version )
10
+ . option ( '-h, --host <host>' )
11
+ . option ( '-p, --port <port>' )
7
12
. parse ( process . argv ) ;
8
13
9
- var processData = function ( postData ) {
10
- var https = require ( 'https' ) ;
14
+ var host = commander . host ;
15
+ var port = 443 ;
11
16
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 ) ) {
12
31
var options = {
13
- hostname : 'central.github.com' ,
32
+ protocol : "https:" ,
33
+ hostname : host ,
34
+ port : port ,
14
35
path : '/api/usage/unity' ,
15
36
method : 'POST' ,
16
37
headers : {
@@ -19,49 +40,25 @@ var processData = function (postData) {
19
40
} ;
20
41
21
42
var req = https . request ( options , function ( res ) {
22
- process . stdout . write ( 'statusCode:' , res . statusCode ) ;
23
-
24
43
res . on ( 'data' , function ( d ) {
25
44
process . stdout . write ( d ) ;
26
45
process . stdout . write ( endOfLine ) ;
27
46
} ) ;
28
47
29
48
res . on ( 'end' , function ( d ) {
30
- process . exit ( ) ;
49
+ process . exit ( res . statusCode == 200 ? 0 : - 1 ) ;
31
50
} ) ;
32
51
} ) ;
33
52
34
53
req . on ( 'error' , function ( e ) {
35
- console . error ( e ) ;
54
+ console . log ( e ) ;
36
55
process . exit ( - 1 ) ;
37
56
} ) ;
38
57
39
- req . write ( postData ) ;
58
+ req . write ( fileContents ) ;
40
59
req . end ( ) ;
41
60
}
42
-
43
- if ( process . stdin . isTTY ) {
44
- var readlineSync = require ( "readline-sync" ) ;
45
- var postData = readlineSync . question ( ) ;
46
-
47
- processData ( postData ) ;
48
- }
49
61
else {
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 ) ;
67
64
}
0 commit comments