Skip to content

Commit 0107525

Browse files
committed
Improve CLI
1 parent 7633112 commit 0107525

File tree

3 files changed

+65
-10
lines changed

3 files changed

+65
-10
lines changed

src/cli.ts

Lines changed: 60 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,13 @@ import * as os from 'node:os'
55
import * as fs from 'node:fs'
66
import * as path from 'node:path'
77
import { buildSchema } from 'graphql/utilities/index.js'
8-
import { transpile } from './index.js'
98
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'
1015

1116
void (async function main() {
1217
let schemaFileOrUrl: string | undefined
@@ -53,19 +58,65 @@ void (async function main() {
5358
schemaFileOrUrl = homeDirExpand(schemaFileOrUrl)
5459
inputFiles = inputFiles.map((f) => homeDirExpand(f))
5560

56-
const schema = buildSchema(fs.readFileSync(schemaFileOrUrl, 'utf-8'))
61+
let schemaSource: string
62+
if (/https?:\/\//.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+
5787
for (let inputFile of inputFiles) {
58-
console.log(`Processing ${inputFile}...`)
5988
const dirName = path.dirname(inputFile)
6089
const fileName = path.basename(inputFile)
90+
91+
console.log(`Processing ${inputFile}`)
92+
6193
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
67118
}
68-
})()
119+
})
69120

70121
function usage() {
71122
console.log(`Usage: megaera [options] <input-files...>`)

src/generate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export type ${q.name} = (${generateVariables(q.variables)}) => ${generateSelecto
3636
`)
3737
}
3838

39-
return code.join('\n\n')
39+
return code.join('\n')
4040
}
4141

4242
function generateVariables(variables?: Variable[]) {

src/utils.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
export function firstLetterUpper(string: string) {
22
return string.charAt(0).toUpperCase() + string.slice(1)
33
}
4+
5+
export function plural(count: number, singular: string, plural: string) {
6+
return (count === 1 ? singular : plural).replace('%d', count.toString())
7+
}

0 commit comments

Comments
 (0)