1
+ #!/usr/bin/env node
1
2
/*
2
3
*
3
4
* Diff to HTML CLI (main.js)
@@ -57,6 +58,15 @@ var argv = require('yargs')
57
58
default : 'preview'
58
59
}
59
60
} )
61
+ . options ( {
62
+ 'u' : {
63
+ alias : 'diffy' ,
64
+ describe : 'Upload to diffy.org' ,
65
+ nargs : 1 ,
66
+ type : 'string' ,
67
+ choices : [ 'browser' , 'pbcopy' , 'print' ]
68
+ }
69
+ } )
60
70
. options ( {
61
71
'F' : {
62
72
alias : 'file' ,
@@ -66,8 +76,8 @@ var argv = require('yargs')
66
76
}
67
77
} )
68
78
. example ( 'diff2html -s line -f html -d word -i command -o preview -- -M HEAD~1' ,
69
- 'diff last commit, line by line, word comparison between lines,' +
70
- 'previewed in the browser and input from git diff command' )
79
+ 'diff last commit, line by line, word comparison between lines,' +
80
+ 'previewed in the browser and input from git diff command' )
71
81
. example ( 'diff2html -i file -- my-file-diff.diff' , 'reading the input from a file' )
72
82
. example ( 'diff2html -f json -o stdout -- -M HEAD~1' , 'print json format to stdout' )
73
83
. example ( 'diff2html -F my-pretty-diff.html -- -M HEAD~1' , 'print to file' )
@@ -77,31 +87,9 @@ var argv = require('yargs')
77
87
. help ( 'h' )
78
88
. alias ( 'h' , 'help' )
79
89
. epilog ( '© 2015 rtfpessoa\n' +
80
- 'For support, check out https://github.com/rtfpessoa/diff2html-cli' )
90
+ 'For support, check out https://github.com/rtfpessoa/diff2html-cli' )
81
91
. argv ;
82
92
83
- main ( ) ;
84
-
85
- function main ( ) {
86
- var input = getInput ( ) ;
87
- if ( input ) {
88
- var content = getOutput ( input ) ;
89
-
90
- if ( argv . file ) {
91
- writeFile ( argv . file , content ) ;
92
- } else if ( argv . output === 'preview' ) {
93
- preview ( content ) ;
94
- } else {
95
- print ( content ) ;
96
- }
97
- } else {
98
- error ( 'The input is empty. Try again.' ) ;
99
- argv . help ( ) ;
100
- }
101
-
102
- process . exit ( 0 ) ;
103
- }
104
-
105
93
function getInput ( ) {
106
94
if ( argv . input === 'file' ) {
107
95
return readFile ( argv . _ [ 0 ] ) ;
@@ -153,6 +141,52 @@ function prepareHTML(content) {
153
141
. replace ( '<!--diff-->' , content ) ;
154
142
}
155
143
144
+ function postToDiffy ( diff , postType ) {
145
+ var jsonParams = { udiff : diff } ;
146
+
147
+ post ( 'http://diffy.org/api/new' , jsonParams , function ( err , response ) {
148
+ if ( err ) {
149
+ print ( err ) ;
150
+ return ;
151
+ }
152
+
153
+ if ( response . status != 'error' ) {
154
+ print ( "Link powered by diffy.org:" ) ;
155
+ print ( response . url ) ;
156
+
157
+ if ( postType === 'browser' ) {
158
+ runCmd ( 'open ' + response . url ) ;
159
+ } else if ( postType === 'pbcopy' ) {
160
+ runCmd ( 'echo "' + response . url + '" | pbcopy' ) ;
161
+ }
162
+ } else {
163
+ print ( "Error: " + message ) ;
164
+ }
165
+ } ) ;
166
+ }
167
+
168
+ function post ( url , payload , callback ) {
169
+ var request = require ( 'request' ) ;
170
+
171
+ request ( {
172
+ url : url ,
173
+ method : 'POST' ,
174
+ form : payload
175
+ } )
176
+ . on ( 'response' , function ( response ) {
177
+ response . on ( 'data' , function ( body ) {
178
+ try {
179
+ callback ( null , JSON . parse ( body . toString ( 'utf8' ) ) ) ;
180
+ } catch ( err ) {
181
+ callback ( new Error ( 'could not parse response' ) ) ;
182
+ }
183
+ } )
184
+ } )
185
+ . on ( 'error' , function ( err ) {
186
+ callback ( err ) ;
187
+ } ) ;
188
+ }
189
+
156
190
function prepareJSON ( content ) {
157
191
return JSON . stringify ( content ) ;
158
192
}
@@ -179,3 +213,26 @@ function runCmd(cmd) {
179
213
var childProcess = require ( 'child_process' ) ;
180
214
return childProcess . execSync ( cmd ) . toString ( 'utf8' ) ;
181
215
}
216
+
217
+ /*
218
+ * CLI code
219
+ */
220
+
221
+ var input = getInput ( ) ;
222
+
223
+ if ( input && argv . diffy ) {
224
+ postToDiffy ( input , argv . diffy ) ;
225
+ } else if ( input ) {
226
+ var content = getOutput ( input ) ;
227
+
228
+ if ( argv . file ) {
229
+ writeFile ( argv . file , content ) ;
230
+ } else if ( argv . output === 'preview' ) {
231
+ preview ( content ) ;
232
+ } else {
233
+ print ( content ) ;
234
+ }
235
+ } else {
236
+ error ( 'The input is empty. Try again.' ) ;
237
+ argv . help ( ) ;
238
+ }
0 commit comments