@@ -5,6 +5,7 @@ const klaw = require('klaw');
5
5
const path = require ( 'path' )
6
6
const minimist = require ( 'minimist' ) ;
7
7
const compareVersions = require ( 'compare-versions' ) ;
8
+ const { diffStringsUnified} = require ( 'jest-diff' ) ;
8
9
9
10
let DEBUG = false ;
10
11
let COMPILED = { } ;
@@ -91,15 +92,17 @@ async function run() {
91
92
}
92
93
}
93
94
else {
94
- let fileContent = await fs . readFile ( file ) ;
95
+ let fileContent = await fs . readFile ( file , "utf8" ) ;
95
96
json = JSON . parse ( fileContent ) ;
96
97
if ( doLint || doFormat ) {
97
- // 2 spaces, *nix newlines, newline at end of file
98
- const expectedContent = JSON . stringify ( json , null , 2 ) . replace ( / ( \r \n | \r ) / g, "\n" ) + "\n" ;
99
- if ( fileContent !== expectedContent ) {
98
+ const expectedContent = JSON . stringify ( json , null , 2 ) ;
99
+ if ( ! matchFile ( fileContent , expectedContent ) ) {
100
100
stats . malformed ++ ;
101
101
if ( doLint ) {
102
102
console . warn ( "--- Lint: File is malformed -> use `--format` to fix the issue" ) ;
103
+ if ( typeof args . verbose !== 'undefined' ) {
104
+ console . log ( diffStringsUnified ( fileContent , expectedContent ) ) ;
105
+ }
103
106
}
104
107
if ( doFormat ) {
105
108
console . warn ( "--- Format: File was malformed -> fixed the issue" ) ;
@@ -221,7 +224,8 @@ async function run() {
221
224
if ( doLint || doFormat ) {
222
225
console . info ( "Malformed: " + stats . malformed ) ;
223
226
}
224
- process . exit ( stats . invalid ) ;
227
+ let errored = ( stats . invalid > 0 || ( doLint && ! doFormat && stats . malformed > 0 ) ) ? 1 : 0 ;
228
+ process . exit ( errored ) ;
225
229
}
226
230
catch ( error ) {
227
231
console . error ( error ) ;
@@ -231,6 +235,15 @@ async function run() {
231
235
232
236
const SUPPORTED_PROTOCOLS = [ 'http' , 'https' ] ;
233
237
238
+ function matchFile ( given , expected ) {
239
+ return normalizeNewline ( given ) === normalizeNewline ( expected ) ;
240
+ }
241
+
242
+ function normalizeNewline ( str ) {
243
+ // 2 spaces, *nix newlines, newline at end of file
244
+ return str . trimRight ( ) . replace ( / ( \r \n | \r ) / g, "\n" ) + "\n" ;
245
+ }
246
+
234
247
function isUrl ( uri ) {
235
248
let part = uri . match ( / ^ ( \w + ) : \/ \/ / i) ;
236
249
if ( part ) {
0 commit comments