3
3
import meow from 'meow' ;
4
4
import chalk from 'chalk' ;
5
5
import { createHtmlOutput } from './html' ;
6
- import { getDiff } from '.' ;
6
+ import { getDiff , Headers } from './diff ' ;
7
7
8
8
const cli = meow (
9
9
`
@@ -15,9 +15,13 @@ const cli = meow(
15
15
--ignore-breaking-changes Do not exit with error on breaking changes
16
16
--create-html-output Creates an HTML file containing the diff
17
17
--html-output-directory Directory where the HTML file should be stored (Default: './schemaDiff')
18
+ --header, -H Header to send to all remote schema sources
19
+ --left-schema-header Header to send to left remote schema source
20
+ --right-schema-header Header to send to right remote schema source
18
21
19
22
Examples
20
23
$ graphql-schema-diff https://example.com/graphql schema.graphql
24
+ $ graphql-schema-diff https://example.com/graphql schema.graphql -H 'Authorization: Bearer 123'
21
25
` ,
22
26
{
23
27
flags : {
@@ -33,18 +37,63 @@ const cli = meow(
33
37
'html-output-directory' : {
34
38
type : 'string' ,
35
39
default : 'schemaDiff'
40
+ } ,
41
+ header : {
42
+ type : 'string' ,
43
+ alias : 'H'
44
+ } ,
45
+ 'left-schema-header' : {
46
+ type : 'string'
47
+ } ,
48
+ 'right-schema-header' : {
49
+ type : 'string'
36
50
}
37
51
}
38
52
}
39
53
) ;
40
54
41
- const [ leftSchemaLocation , rightSchemaLocation ] = cli . input ;
55
+ function parseHeaders ( headerInput ?: string | string [ ] ) : Headers | undefined {
56
+ let headers : string [ ] [ ] ;
57
+ const parseHeader = ( header : string ) : string [ ] =>
58
+ header . split ( ':' ) . map ( ( val : string ) => val . trim ( ) ) ;
59
+
60
+ if ( ! headerInput ) return ;
61
+
62
+ if ( Array . isArray ( headerInput ) ) {
63
+ headers = headerInput . map ( parseHeader ) ;
64
+ } else {
65
+ headers = [ parseHeader ( headerInput ) ] ;
66
+ }
67
+
68
+ return headers
69
+ . filter ( ( [ key ] ) => key != null && key !== '' )
70
+ . reduce ( ( result , [ key , value ] ) => ( { ...result , [ key && key ] : value } ) , { } ) ;
71
+ }
72
+
73
+ const [ leftSchemaLocation , rightSchemaLocation ] : string [ ] = cli . input ;
74
+ const {
75
+ header ,
76
+ leftSchemaHeader ,
77
+ rightSchemaHeader
78
+ } : {
79
+ header ?: string | string [ ] ;
80
+ leftSchemaHeader ?: string | string [ ] ;
81
+ rightSchemaHeader ?: string | string [ ] ;
82
+ } = cli . flags ;
42
83
43
84
if ( ! leftSchemaLocation || ! rightSchemaLocation ) {
44
85
throw new Error ( 'Schema locations missing!' ) ;
45
86
}
46
87
47
- getDiff ( leftSchemaLocation , rightSchemaLocation )
88
+ getDiff ( leftSchemaLocation , rightSchemaLocation , {
89
+ headers : parseHeaders ( header ) ,
90
+ leftSchema : {
91
+ headers : parseHeaders ( leftSchemaHeader )
92
+ } ,
93
+ rightSchema : {
94
+ headers : parseHeaders ( rightSchemaHeader )
95
+ }
96
+ } )
48
97
. then ( async result => {
49
98
if ( result === undefined ) {
50
99
console . log ( chalk . green ( '✔ No changes' ) ) ;
0 commit comments