@@ -21,6 +21,7 @@ const commander = require('commander')
21
21
. option ( '-s, --sort-keys' , 'sort object keys (not when prettifying)' )
22
22
. option ( '-E, --extensions [ext]' , 'file extensions to process for directory walk' , collectValues , [ 'json' , 'JSON' ] )
23
23
. option ( '-i, --in-place' , 'overwrite the input files' )
24
+ . option ( '-j, --diff' , 'print difference instead of writing the output' )
24
25
. option ( '-k, --check' , 'check that the input is equal to the output' )
25
26
. option ( '-t, --indent [num|char]' , 'number of spaces or specific characters to use for indentation' , 2 )
26
27
. option ( '-c, --compact' , 'compact error display' )
@@ -87,6 +88,7 @@ if (params.config === false) {
87
88
}
88
89
89
90
const extensions = options . extensions . map ( extension => '.' + extension )
91
+ let reported
90
92
91
93
function convertConfig ( config ) {
92
94
const result = { }
@@ -108,10 +110,16 @@ function mergeOptions (target, ...sources) {
108
110
return target
109
111
}
110
112
111
- function logNormalError ( error , file ) {
112
- if ( process . exitCode > 0 ) {
113
+ function separateBlocks ( ) {
114
+ if ( reported ) {
113
115
console . log ( )
116
+ } else {
117
+ reported = true
114
118
}
119
+ }
120
+
121
+ function logNormalError ( error , file ) {
122
+ separateBlocks ( )
115
123
console . log ( 'File:' , file )
116
124
console . error ( error . message )
117
125
}
@@ -227,6 +235,33 @@ function checkContents (file, source, parsed) {
227
235
}
228
236
}
229
237
238
+ function diffContents ( file , source , parsed ) {
239
+ const { createTwoFilesPatch, structuredPatch } = require ( 'diff' )
240
+ const compact = options . quiet || options . compact
241
+ let diff , length
242
+ if ( compact ) {
243
+ diff = structuredPatch ( `${ file } .orig` , file , source , parsed , '' , '' , { context : 3 } )
244
+ length = diff . hunks && diff . hunks . length
245
+ } else {
246
+ diff = createTwoFilesPatch ( `${ file } .orig` , file , source , parsed , '' , '' , { context : 3 } )
247
+ length = diff . split ( / \r ? \n / ) . length - 4
248
+ }
249
+ if ( length > 0 ) {
250
+ if ( compact ) {
251
+ const hunk = length === 1 ? 'hunk' : 'hunks'
252
+ console . info ( `${ file } : ${ length } ${ hunk } ` )
253
+ } else {
254
+ separateBlocks ( )
255
+ console . log ( 'File:' , file )
256
+ console . log ( diff )
257
+ }
258
+ } else {
259
+ if ( compact ) {
260
+ console . info ( `${ file } : no difference` )
261
+ }
262
+ }
263
+ }
264
+
230
265
function processFile ( file ) {
231
266
file = normalize ( file )
232
267
if ( options . logFiles ) {
@@ -238,6 +273,8 @@ function processFile (file) {
238
273
writeFileSync ( file , ensureLineBreak ( parsed , source ) )
239
274
} else if ( options . check ) {
240
275
checkContents ( file , source , ensureLineBreak ( parsed , source ) )
276
+ } else if ( options . diff ) {
277
+ diffContents ( file , source , ensureLineBreak ( parsed , source ) )
241
278
} else {
242
279
if ( ! ( options . quiet || options . logFiles ) ) {
243
280
console . log ( parsed )
@@ -311,6 +348,8 @@ function main () {
311
348
const parsed = processContents ( source , file )
312
349
if ( options . check ) {
313
350
checkContents ( file , source , ensureLineBreak ( parsed , source ) )
351
+ } else if ( options . diff ) {
352
+ diffContents ( file , source , ensureLineBreak ( parsed , source ) )
314
353
} else if ( ! ( options . quiet || options . logFiles ) ) {
315
354
console . log ( parsed )
316
355
}
0 commit comments