@@ -5,8 +5,13 @@ import * as os from 'node:os'
5
5
import * as fs from 'node:fs'
6
6
import * as path from 'node:path'
7
7
import { buildSchema } from 'graphql/utilities/index.js'
8
- import { transpile } from './index.js'
9
8
import { Source } from 'graphql/language/index.js'
9
+ import { GraphQLSchema } from 'graphql/type/index.js'
10
+ import { GraphQLError } from 'graphql/error/index.js'
11
+ import { styleText } from 'node:util'
12
+ import { traverse } from './visitor.js'
13
+ import { generate } from './generate.js'
14
+ import { plural } from './utils.js'
10
15
11
16
void ( async function main ( ) {
12
17
let schemaFileOrUrl : string | undefined
@@ -53,19 +58,65 @@ void (async function main() {
53
58
schemaFileOrUrl = homeDirExpand ( schemaFileOrUrl )
54
59
inputFiles = inputFiles . map ( ( f ) => homeDirExpand ( f ) )
55
60
56
- const schema = buildSchema ( fs . readFileSync ( schemaFileOrUrl , 'utf-8' ) )
61
+ let schemaSource : string
62
+ if ( / h t t p s ? : \/ \/ / . test ( schemaFileOrUrl ) ) {
63
+ const headers = new Headers ( )
64
+ if ( process . env . GITHUB_TOKEN ) {
65
+ const token = process . env . GITHUB_TOKEN
66
+ headers . set ( 'Authorization' , `Bearer ${ token } ` )
67
+ }
68
+ const using = headers . has ( 'Authorization' ) ? ` using $GITHUB_TOKEN` : ``
69
+ console . log ( `Fetching schema from ${ schemaFileOrUrl } ${ using } .` )
70
+ schemaSource = await fetch ( schemaFileOrUrl , { headers } ) . then ( ( r ) =>
71
+ r . text ( ) ,
72
+ )
73
+ } else {
74
+ schemaSource = fs . readFileSync ( schemaFileOrUrl , 'utf-8' )
75
+ }
76
+
77
+ let schema : GraphQLSchema
78
+ try {
79
+ schema = buildSchema ( schemaSource )
80
+ } catch ( e ) {
81
+ console . error (
82
+ styleText ( [ 'bgRed' , 'whiteBright' , 'bold' ] , `Failed to parse schema` ) ,
83
+ )
84
+ throw e
85
+ }
86
+
57
87
for ( let inputFile of inputFiles ) {
58
- console . log ( `Processing ${ inputFile } ...` )
59
88
const dirName = path . dirname ( inputFile )
60
89
const fileName = path . basename ( inputFile )
90
+
91
+ console . log ( `Processing ${ inputFile } ` )
92
+
61
93
const source = new Source ( fs . readFileSync ( inputFile , 'utf-8' ) , fileName )
62
- let code = transpile ( schema , source )
63
- code =
64
- `// DO NOT EDIT. Instead of this file, edit "${ fileName } " and rerun megaera".\n\n` +
65
- code
66
- fs . writeFileSync ( path . join ( dirName , fileName + '.ts' ) , code )
94
+ const content = traverse ( schema , source )
95
+ const code = generate ( content )
96
+
97
+ const ops = plural (
98
+ content . operations . length ,
99
+ '%d operation' ,
100
+ '%d operations' ,
101
+ )
102
+ const frg = plural ( content . fragments . length , '%d fragment' , '%d fragments' )
103
+ console . log ( `> ${ styleText ( 'green' , 'done' ) } (${ ops } , ${ frg } )` )
104
+
105
+ const prefix = `// DO NOT EDIT. This is a generated file. Instead of this file, edit "${ fileName } ".\n\n`
106
+ fs . writeFileSync (
107
+ path . join ( dirName , fileName + '.ts' ) ,
108
+ prefix + code ,
109
+ 'utf-8' ,
110
+ )
111
+ }
112
+ } ) ( ) . catch ( ( e ) => {
113
+ if ( e instanceof GraphQLError ) {
114
+ console . error ( e . toString ( ) )
115
+ process . exitCode = 1
116
+ } else {
117
+ throw e
67
118
}
68
- } ) ( )
119
+ } )
69
120
70
121
function usage ( ) {
71
122
console . log ( `Usage: megaera [options] <input-files...>` )
0 commit comments