@@ -8,18 +8,18 @@ import { create } from "../lib/with-text-diffs.js";
8
8
const allowedFlags = [
9
9
"--help" ,
10
10
"--format" ,
11
- "--omit-removed-values" ,
12
- "--no-moves" ,
13
- "--no-text-diff" ,
14
- "--object-keys" ,
11
+ "--omit-removed-values" ,
12
+ "--no-moves" ,
13
+ "--no-text-diff" ,
14
+ "--object-keys" ,
15
15
] ;
16
16
17
17
const args = process . argv . slice ( 2 ) ;
18
18
const flags = { } ;
19
19
const files = [ ] ;
20
20
for ( const arg of args ) {
21
21
if ( arg . startsWith ( "--" ) ) {
22
- const argParts = arg . split ( "=" ) ;
22
+ const argParts = arg . split ( "=" ) ;
23
23
if ( allowedFlags . indexOf ( argParts [ 0 ] ) === - 1 ) {
24
24
console . error ( `unrecognized option: ${ argParts [ 0 ] } ` ) ;
25
25
process . exit ( 2 ) ;
@@ -49,8 +49,10 @@ example:`;
49
49
} ;
50
50
51
51
function createInstance ( ) {
52
- const format = typeof flags [ "--format" ] === "string" ? flags [ "--format" ] : "console" ;
53
- const objectKeys = ( flags [ "--object-keys=" ] ?? "id,key" ) . split ( "," )
52
+ const format =
53
+ typeof flags [ "--format" ] === "string" ? flags [ "--format" ] : "console" ;
54
+ const objectKeys = ( flags [ "--object-keys=" ] ?? "id,key" )
55
+ . split ( "," )
54
56
. map ( ( key ) => key . trim ( ) ) ;
55
57
56
58
const jsondiffpatch = create ( {
@@ -77,19 +79,19 @@ function createInstance() {
77
79
: { } ) ,
78
80
} ,
79
81
} ) ;
80
- return jsondiffpatch ;
82
+ return jsondiffpatch ;
81
83
}
82
84
83
85
function printDiff ( delta ) {
84
- if ( flags [ "--format" ] === "json" ) {
85
- console . log ( JSON . stringify ( delta , null , 2 ) ) ;
86
- } else if ( flags [ "--format" ] === "json-compact" ) {
87
- console . log ( JSON . stringify ( delta ) ) ;
88
- } else if ( flags [ "--format" ] === "jsonpatch" ) {
89
- jsonpatchFormatter . log ( delta ) ;
90
- } else {
91
- consoleFormatter . log ( delta ) ;
92
- }
86
+ if ( flags [ "--format" ] === "json" ) {
87
+ console . log ( JSON . stringify ( delta , null , 2 ) ) ;
88
+ } else if ( flags [ "--format" ] === "json-compact" ) {
89
+ console . log ( JSON . stringify ( delta ) ) ;
90
+ } else if ( flags [ "--format" ] === "jsonpatch" ) {
91
+ jsonpatchFormatter . log ( delta ) ;
92
+ } else {
93
+ consoleFormatter . log ( delta ) ;
94
+ }
93
95
}
94
96
95
97
function getJson ( path ) {
@@ -104,32 +106,29 @@ const jsondiffpatch = createInstance();
104
106
105
107
if ( files . length !== 2 || flags . includes ( "--help" ) ) {
106
108
console . log ( usage ( ) ) ;
107
- const delta = jsondiffpatch . diff ( {
108
- property : "before" ,
109
- list : [
110
- { id : 1 , } ,
111
- { id : 2 , } ,
112
- { id : 3 , name : "item removed" } ,
113
- ] ,
114
- longText : 'when a text is very 🦕 long, diff-match-patch is used to create a text diff that only captures the changes, comparing each characther' ,
115
- } , {
116
- property : "after" ,
117
- newProperty : "added" ,
118
- list : [
119
- { id : 2 , } ,
120
- { id : 1 , } ,
121
- { id : 4 , name : "item added" } ,
122
- ] ,
123
- longText : 'when a text a bit long, diff-match-patch creates a text diff that captures the changes, comparing each characther' ,
124
- } ) ;
125
- printDiff ( delta ) ;
109
+ const delta = jsondiffpatch . diff (
110
+ {
111
+ property : "before" ,
112
+ list : [ { id : 1 } , { id : 2 } , { id : 3 , name : "item removed" } ] ,
113
+ longText :
114
+ "when a text is very 🦕 long, diff-match-patch is used to create a text diff that only captures the changes, comparing each characther" ,
115
+ } ,
116
+ {
117
+ property : "after" ,
118
+ newProperty : "added" ,
119
+ list : [ { id : 2 } , { id : 1 } , { id : 4 , name : "item added" } ] ,
120
+ longText :
121
+ "when a text a bit long, diff-match-patch creates a text diff that captures the changes, comparing each characther" ,
122
+ } ,
123
+ ) ;
124
+ printDiff ( delta ) ;
126
125
} else {
127
126
Promise . all ( [ files [ 0 ] , files [ 1 ] ] . map ( getJson ) ) . then ( ( [ left , right ] ) => {
128
127
const delta = jsondiffpatch . diff ( left , right ) ;
129
128
if ( delta === undefined ) {
130
129
process . exit ( 0 ) ;
131
130
} else {
132
- printDiff ( delta ) ;
131
+ printDiff ( delta ) ;
133
132
// exit code 1 to be consistent with GNU diff
134
133
process . exit ( 1 ) ;
135
134
}
0 commit comments