Skip to content

Commit

Permalink
fix: restructure commands and update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
rintoj committed Jun 15, 2024
1 parent 523d385 commit b15ccfc
Show file tree
Hide file tree
Showing 15 changed files with 213 additions and 71 deletions.
71 changes: 63 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,19 +172,22 @@ export enum UserStatus {
registerEnumType(UserStatus, { name: 'UserStatus' })
```

## Run
# Command: gql-assist

# gql-assist

Assists in generating models, resolvers, field resolvers, and GraphQL types efficiently.
GQL Assist is a powerful tool designed to streamline the development of GraphQL APIs in a NestJS
environment. By automatically converting TypeScript classes, resolvers, and enums into their
corresponding GraphQL definitions, GQL Assist significantly reduces the amount of boilerplate code
you need to write.

```sh

gql-assist <generate> [--help] [--doc] [--version]
gql-assist <generate|create> [--help] [--doc] [--version]

COMMANDS

generate
generate GraphQL Assist converts GraphQL queries, mutations or subscriptions

create Create a module

COMMON

Expand All @@ -198,13 +201,65 @@ COMMON

## gql-assist generate

GraphQL Assist converts GraphQL queries, mutations or subscriptions

```sh

gql-assist generate <hook|decorator>

COMMANDS

hook GraphQL Assist converts GraphQL queries into TypeScript code compatible with @apollo/client
or similar library, making query writing for Apollo Client easier and less error-prone.

decorator Automatically converts TypeScript classes, resolvers, methods, and enums to their
respective NestJS GraphQL or Type GraphQL counterparts with appropriate decorators.

```

> ### gql-assist generate hook
>
> GraphQL Assist converts GraphQL queries into TypeScript code compatible with @apollo/client or
> similar library, making query writing for Apollo Client easier and less error-prone.
>
> ```sh
>
> gql-assist generate hook --schema=<string> --file=<string>
>
> OPTIONS
>
> --schema=<string> [Required] Schema file
>
> --file=<string> [Required] The source file to inspect and generate
>
> ```
>
> ### gql-assist generate decorator
>
> Automatically converts TypeScript classes, resolvers, methods, and enums to their respective
> NestJS GraphQL or Type GraphQL counterparts with appropriate decorators.
>
> ```sh
>
> gql-assist generate decorator --file=<string>
>
> OPTIONS
>
> --file=<string> [Required] The source file to inspect and generate
>
> ```
## gql-assist create
Create a module
```sh
gql-assist generate <file>
gql-assist create <name>
ARGUMENTS
file The source file to inspect and generate
name Name of the module
```
Expand Down
2 changes: 1 addition & 1 deletion .bin/cli → bin/cli
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const { spawn } = require('child_process')
const { resolve } = require('path')
const cli = resolve(__dirname, '..', 'dist', 'index.js')
const cli = resolve(__dirname, '..', 'dist', 'cli.js')
const args = process.argv.slice(2).join(' ')
const cmd = `${cli} ${args}`
spawn('node', cmd.split(' '), { detached: true, stdio: 'inherit', cwd: process.cwd() })
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "dist/index.js",
"types": "dist/index.d.ts",
"bin": {
"gql-assist": ".bin/cli"
"gql-assist": "bin/cli"
},
"private": false,
"scripts": {
Expand All @@ -14,7 +14,7 @@
"prettier": "prettier --write \"**/src/**/*.{ts,tsx}\" -c",
"build": "rimraf dist && tsc -p .",
"semantic-release": "semantic-release",
"cli": "ts-node src/index.ts",
"cli": "ts-node src/.ts",
"start": "rimraf dist && tsc -p . --watch"
},
"keywords": [
Expand All @@ -37,8 +37,10 @@
"graphql": "^16.8.2",
"minimum-edit-distance": "^1.1.7",
"name-util": "^1.3.0",
"prettier": "^3.3.0",
"shelljs": "^0.8.5",
"tsds-tools": "^1.2.1"
"tsds-tools": "^1.2.1",
"typescript": "^5.4.5"
},
"devDependencies": {
"@types/chalk": "^2.2.0",
Expand All @@ -51,11 +53,9 @@
"eslint-plugin-jest": "^27.0.4",
"eslint-plugin-prettier": "^4.2.1",
"jest": "^29.5.0",
"prettier": "^3.3.0",
"rimraf": "^5.0.7",
"semantic-release": "^24.0.0",
"ts-jest": "^29.0.0",
"ts-node": "^10.9.2",
"typescript": "^5.4.5"
"ts-node": "^10.9.2"
}
}
13 changes: 13 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { cli, runCli } from 'clifer'
import create from './create/create-command'
import generate from './generate/generate-command'

const command = cli('gql-assist')
.description(
'GQL Assist is a powerful tool designed to streamline the development of GraphQL APIs in a NestJS environment. By automatically converting TypeScript classes, resolvers, and enums into their corresponding GraphQL definitions, GQL Assist significantly reduces the amount of boilerplate code you need to write.',
)
.version('1.0')
.command(generate)
.command(create)

runCli(command)
47 changes: 8 additions & 39 deletions src/generate/generate-command.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,8 @@
import { command, input } from 'clifer'
import { writeFile } from 'fs-extra'
import { reduceAsync } from 'tsds-tools'
import ts from 'typescript'
import { GQLAssistConfig, config } from '../config'
import { generateEnum } from '../generator/enum/enum-generator'
import { generateInput } from '../generator/input/input-generator'
import { generateModel } from '../generator/model/model-generator'
import { generateResolver } from '../generator/resolver/resolver-generator'
import { readTSFile } from '../ts/parse-ts'
import { prettify } from '../ts/prettify'
import { printTS } from '../ts/print-ts'

interface GenerateProps {
file: string
}

const plugins = [generateModel, generateInput, generateResolver, generateEnum]

export async function generate(sourceFile: ts.SourceFile, config: GQLAssistConfig) {
return await reduceAsync(
plugins,
(sourceFile, runPlugin) => runPlugin(sourceFile, config),
sourceFile,
)
}

async function run({ file }: GenerateProps) {
const sourceFile = readTSFile(file)
const output = await prettify(printTS(await generate(sourceFile, config), undefined))
await writeFile(file, output)
}

export default command<GenerateProps>('generate')
.description('Generate models and resolvers')
.argument(
input('file').description('The source file to inspect and generate').string().required(),
)
.handle(run)
import { command } from 'clifer'
import generateDecoratorCommand from './generate-decorator-command'
import generateHookCommand from './generate-hook-command'

export default command('generate')
.description('GraphQL Assist converts GraphQL queries, mutations or subscriptions')
.command(generateHookCommand)
.command(generateDecoratorCommand)
39 changes: 39 additions & 0 deletions src/generate/generate-decorator-command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { command, input } from 'clifer'
import { writeFile } from 'fs-extra'
import { reduceAsync } from 'tsds-tools'
import ts from 'typescript'
import { GQLAssistConfig, config } from '../config'
import { generateEnum } from '../generator/enum/enum-generator'
import { generateInput } from '../generator/input/input-generator'
import { generateModel } from '../generator/model/model-generator'
import { generateResolver } from '../generator/resolver/resolver-generator'
import { readTSFile } from '../ts/parse-ts'
import { prettify } from '../ts/prettify'
import { printTS } from '../ts/print-ts'

interface GenerateProps {
file: string
}

const plugins = [generateModel, generateInput, generateResolver, generateEnum]

export async function generate(sourceFile: ts.SourceFile, config: GQLAssistConfig) {
return await reduceAsync(
plugins,
(sourceFile, runPlugin) => runPlugin(sourceFile, config),
sourceFile,
)
}

async function run({ file }: GenerateProps) {
const sourceFile = readTSFile(file)
const output = await prettify(printTS(await generate(sourceFile, config), undefined))
await writeFile(file, output)
}

export default command<GenerateProps>('decorator')
.description(
'Automatically converts TypeScript classes, resolvers, methods, and enums to their respective NestJS GraphQL or Type GraphQL counterparts with appropriate decorators.',
)
.option(input('file').description('The source file to inspect and generate').string().required())
.handle(run)
26 changes: 26 additions & 0 deletions src/generate/generate-hook-command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { command, input } from 'clifer'
import { writeFile } from 'fs-extra'
import { config } from '../config'
import { generateHook } from '../generator'
import { loadSchema } from '../generator/hook/graphql-util'
import { prettify, printTS, readTSFile } from '../ts'

interface Props {
file: string
schema: string
}

async function run(props: Props) {
const schema = loadSchema(props.schema)
const sourceFile = readTSFile(props.file)
const output = await prettify(printTS(await generateHook(sourceFile, schema, config), undefined))
await writeFile(props.file, output)
}

export default command<Props>('hook')
.description(
'GraphQL Assist converts GraphQL queries into TypeScript code compatible with @apollo/client or similar library, making query writing for Apollo Client easier and less error-prone.',
)
.option(input('schema').description('Schema file').string().required())
.option(input('file').description('The source file to inspect and generate').string().required())
.handle(run)
2 changes: 2 additions & 0 deletions src/generate/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './generate-decorator-command'
export * from './generate-hook-command'
1 change: 1 addition & 0 deletions src/generator/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './context'
export * from './enum'
export * from './hook'
export * from './input'
Expand Down
17 changes: 3 additions & 14 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@
export * from './diff'
export * from './generate'
export * from './generator'

import { cli, runCli } from 'clifer'
import generate from './generate/generate-command'
import create from './create/create-command'

const command = cli('gql-assist')
.description(
'Assists in generating models, resolvers, field resolvers, and GraphQL types efficiently.',
)
.version('1.0')
.command(generate)
.command(create)

runCli(command)
export * from './ts'
export * from './util'
2 changes: 1 addition & 1 deletion src/ts/create-scalar-decorator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import ts, { factory } from 'typescript'
import { Context } from '../gql/context'
import { Context } from '../generator/context'
import { createImport } from './create-import'

export function createScalarDecorator(node: ts.ClassDeclaration, context: Context) {
Expand Down
45 changes: 45 additions & 0 deletions src/ts/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
export * from './add-decorator'
export * from './add-export'
export * from './add-imports'
export * from './add-new-line'
export * from './add-nullability'
export * from './conditional'
export * from './convert-to-method'
export * from './create-args-decorator'
export * from './create-class-decorator'
export * from './create-context-decorator'
export * from './create-enum'
export * from './create-graphql-query'
export * from './create-import'
export * from './create-interface'
export * from './create-parent-decorator'
export * from './create-property-or-method-decorator'
export * from './create-resolver-decorator'
export * from './create-scalar-decorator'
export * from './create-type'
export * from './create-union'
export * from './find-node'
export * from './get-all-types'
export * from './get-comment'
export * from './get-comment-from-decorator'
export * from './get-decorator'
export * from './get-implementation-by-name'
export * from './get-method-declaration'
export * from './get-name'
export * from './get-parameter'
export * from './get-parameter-by-type'
export * from './get-property-declaration'
export * from './get-type'
export * from './is-async'
export * from './is-nullable'
export * from './is-nullable-from-decorator'
export * from './is-private'
export * from './organize-imports'
export * from './parse-ts'
export * from './prettify'
export * from './print-ts'
export * from './remove-nullability'
export * from './transform-name'
export * from './traverse-syntax-tree'
export * from './update-statements'
export * from './with-default-type'
2 changes: 1 addition & 1 deletion src/ts/is-private.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import ts from 'typescript'
export function isPrivate(node: ts.Node) {
return (
(node as any)?.modifiers?.find(
modifier =>
(modifier: ts.Node) =>
[ts.SyntaxKind.PrivateKeyword, ts.SyntaxKind.ProtectedKeyword].indexOf(modifier.kind) >= 0,
) != undefined
)
Expand Down
3 changes: 3 additions & 0 deletions src/util/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './common'
export * from './run-command'
export * from './test-util'
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,6 @@
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
"skipLibCheck": true /* Skip type checking all .d.ts files. */
},
"include": ["src/index.ts", "src/custom.d.ts"],
"include": ["src/index.ts", "src/cli.ts", "src/custom.d.ts"],
"exclude": ["build", "dist"]
}

0 comments on commit b15ccfc

Please sign in to comment.