11#!/usr/bin/env node
2+
23/*
34 *
45 * Diff to HTML CLI (main.js)
@@ -115,7 +116,7 @@ function runGitDiff(callback) {
115116 if ( argv . _ . length && argv . _ [ 0 ] ) {
116117 gitArgs = argv . _ . join ( ' ' ) ;
117118 } else {
118- gitArgs = '-M HEAD~1'
119+ gitArgs = '-M HEAD~1' ;
119120 }
120121
121122 var diffCommand = 'git diff ' + gitArgs ;
@@ -135,10 +136,11 @@ function getOutput(input) {
135136 htmlContent = diff2Html . getPrettyHtmlFromDiff ( input , config ) ;
136137 }
137138 return prepareHTML ( htmlContent ) ;
138- } else {
139- var jsonContent = diff2Html . getJsonFromDiff ( input , config ) ;
140- return prepareJSON ( jsonContent ) ;
141139 }
140+
141+ var jsonContent = diff2Html . getJsonFromDiff ( input , config ) ;
142+ return prepareJSON ( jsonContent ) ;
143+
142144}
143145
144146function preview ( content ) {
@@ -148,10 +150,16 @@ function preview(content) {
148150}
149151
150152function prepareHTML ( content ) {
151- var template = readFileSync ( __dirname + '/../dist/template.html' , 'utf8' ) ;
152- var css = readFileSync ( __dirname + '/../dist/diff2html.css' , 'utf8' ) ;
153+ var template = readFileSync ( __dirname + '/../dist/template.html' ) ;
154+
155+ var cssFile = __dirname + '/../node_modules/diff2html/css/diff2html.css' ;
156+ var cssFallbackFile = __dirname + '/../dist/diff2html.css' ;
157+ if ( existsSync ( cssFile ) ) cssFile = cssFallbackFile ;
158+
159+ var cssContent = readFileSync ( cssFile ) ;
160+
153161 return template
154- . replace ( '<!--css-->' , '<style>\n' + css + '\n</style>' )
162+ . replace ( '<!--css-->' , '<style>\n' + cssContent + '\n</style>' )
155163 . replace ( '<!--diff-->' , content ) ;
156164}
157165
@@ -164,8 +172,8 @@ function postToDiffy(diff, postType) {
164172 return ;
165173 }
166174
167- if ( response . status != 'error' ) {
168- print ( " Link powered by diffy.org:" ) ;
175+ if ( response . status !== 'error' ) {
176+ print ( ' Link powered by diffy.org:' ) ;
169177 print ( response . url ) ;
170178
171179 if ( postType === 'browser' ) {
@@ -174,7 +182,7 @@ function postToDiffy(diff, postType) {
174182 runCmd ( 'echo "' + response . url + '" | pbcopy' ) ;
175183 }
176184 } else {
177- print ( " Error: " + message ) ;
185+ print ( ' Error: ' + message ) ;
178186 }
179187 } ) ;
180188}
@@ -188,11 +196,11 @@ function post(url, payload, callback) {
188196 . on ( 'response' , function ( response ) {
189197 response . on ( 'data' , function ( body ) {
190198 try {
191- callback ( null , JSON . parse ( body . toString ( 'utf8' ) ) ) ;
199+ return callback ( null , JSON . parse ( body . toString ( 'utf8' ) ) ) ;
192200 } catch ( err ) {
193- callback ( new Error ( 'could not parse response' ) ) ;
201+ return callback ( new Error ( 'could not parse response' ) ) ;
194202 }
195- } )
203+ } ) ;
196204 } )
197205 . on ( 'error' , function ( err ) {
198206 callback ( err ) ;
@@ -211,6 +219,16 @@ function error(msg) {
211219 console . error ( msg ) ;
212220}
213221
222+ function existsSync ( filePath ) {
223+ try {
224+ fs . existsSync ( filePath ) ;
225+ } catch ( ignore ) {
226+ return false ;
227+ }
228+
229+ return true ;
230+ }
231+
214232function readFileSync ( filePath ) {
215233 return fs . readFileSync ( filePath , 'utf8' ) ;
216234}
0 commit comments