From 10dc39837a6d47e9b37d464922859e564946dd85 Mon Sep 17 00:00:00 2001 From: Anton Medvedev Date: Wed, 10 Jul 2024 23:01:56 +0200 Subject: [PATCH] Update generate.ts --- package.json | 8 ++++++-- src/cli.ts | 28 ++++++++++++++++++---------- src/generate.ts | 4 ++-- 3 files changed, 26 insertions(+), 14 deletions(-) diff --git a/package.json b/package.json index ef42524..505e547 100644 --- a/package.json +++ b/package.json @@ -1,15 +1,19 @@ { "name": "megaera", - "version": "0.0.0", + "version": "0.0.2", "description": "GraphQL TypeScript Generator", "type": "module", "main": "./dist/index.js", "types": "./dist/index.d.ts", + "bin": { + "megaera": "./dist/cli.js" + }, "scripts": { "fmt": "prettier --write .", "fmt:check": "prettier --check .", "build": "tsc", - "test": "vitest" + "test": "vitest", + "prepublishOnly": "npm run build" }, "dependencies": { "graphql": "^16.0.0" diff --git a/src/cli.ts b/src/cli.ts index 7807c20..906e12a 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -8,9 +8,9 @@ import { buildSchema } from 'graphql/utilities/index.js' import { transpile } from './index.js' import { Source } from 'graphql/language/index.js' -void async function main() { +void (async function main() { let schemaFileOrUrl: string | undefined - const inputFiles: string[] = [] + let inputFiles: string[] = [] const args = process.argv.slice(2) if (args.length === 0) { @@ -23,7 +23,7 @@ void async function main() { usage() return } else if (arg == '--schema') { - schemaFileOrUrl = process.argv[++i] + schemaFileOrUrl = args[++i] } else if (arg.startsWith('--schema=')) { schemaFileOrUrl = arg.slice('--schema='.length) } else if (arg.startsWith('--')) { @@ -36,28 +36,36 @@ void async function main() { } if (schemaFileOrUrl === undefined) { - console.error(`Missing schema file or URL. Use --schema= flag to specify it.`) + console.error( + `Missing schema file or URL. Use --schema= flag to specify it.`, + ) process.exitCode = 1 return } if (inputFiles.length === 0) { - console.error(`Missing input files. Specify input files ./src/**/*.graphql to generate types.`) + console.error( + `Missing input files. Specify input files ./src/**/*.graphql to generate types.`, + ) process.exitCode = 1 return } - const schema = buildSchema(fs.readFileSync(homeDirExpand(schemaFileOrUrl), 'utf-8')) + schemaFileOrUrl = homeDirExpand(schemaFileOrUrl) + inputFiles = inputFiles.map((f) => homeDirExpand(f)) + + const schema = buildSchema(fs.readFileSync(schemaFileOrUrl, 'utf-8')) for (let inputFile of inputFiles) { console.log(`Processing ${inputFile}...`) - inputFile = homeDirExpand(inputFile) const dirName = path.dirname(inputFile) const fileName = path.basename(inputFile) const source = new Source(fs.readFileSync(inputFile, 'utf-8'), fileName) let code = transpile(schema, source) - code = '// DO NOT EDIT. This file was generated by megaera.\n\n' + code - fs.writeFileSync(path.join(dirName, fileName.replace(/\.(graphql|gql)$/, '.ts')), code) + code = + `// DO NOT EDIT. Instead of this file, edit "${fileName}" and rerun megaera".\n\n` + + code + fs.writeFileSync(path.join(dirName, fileName + '.ts'), code) } -}() +})() function usage() { console.log(`Usage: megaera [options] `) diff --git a/src/generate.ts b/src/generate.ts index 06895fd..9ee012a 100644 --- a/src/generate.ts +++ b/src/generate.ts @@ -32,7 +32,7 @@ export type ${f.name} = ${generateSelector(f, 0, true)} code.push(`export const ${q.name} = \`#graphql ${querySource}\` as string & ${q.name} -export type ${q.name} = (${generateVariables(q.variables)}) => ${generateSelector(q)} +export type ${q.name} = (${generateVariables(q.variables)}) => ${generateSelector(q, 0, true)} `) } @@ -106,7 +106,7 @@ function generateInlineFragments(s: Selector, depth: number) { let code = '' let nullable = false for (const fragment of s.inlineFragments) { - code += ' | ' + generateSelector(fragment, depth + 1, true) + code += ' & ' + generateSelector(fragment, depth + 1, true) nullable ||= isNullableType(fragment.type) } return code + (nullable ? ' | null' : '')