-
Notifications
You must be signed in to change notification settings - Fork 6
feat(codegen): add CLI command generator with agent-ready docs from GraphQL schema #725
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
be959f7
feat(codegen): add CLI command generator from GraphQL schema
pyramation 922c718
test(codegen): add CLI generator snapshot tests
pyramation fde91f9
feat(codegen): generate README.md and COMMANDS.md for CLI
pyramation 1fe5255
feat(codegen): add configurable docs generation (README, AGENTS.md, M…
pyramation File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| import * as t from '@babel/types'; | ||
|
|
||
| import type { CleanArgument, CleanTypeRef } from '../../../types/schema'; | ||
|
|
||
| function unwrapNonNull(typeRef: CleanTypeRef): { inner: CleanTypeRef; required: boolean } { | ||
| if (typeRef.kind === 'NON_NULL' && typeRef.ofType) { | ||
| return { inner: typeRef.ofType, required: true }; | ||
| } | ||
| return { inner: typeRef, required: false }; | ||
| } | ||
|
|
||
| function resolveBaseType(typeRef: CleanTypeRef): CleanTypeRef { | ||
| if ((typeRef.kind === 'NON_NULL' || typeRef.kind === 'LIST') && typeRef.ofType) { | ||
| return resolveBaseType(typeRef.ofType); | ||
| } | ||
| return typeRef; | ||
| } | ||
|
|
||
| export function buildQuestionObject(arg: CleanArgument): t.ObjectExpression { | ||
| const { inner, required } = unwrapNonNull(arg.type); | ||
| const base = resolveBaseType(arg.type); | ||
| const props: t.ObjectProperty[] = []; | ||
|
|
||
| if (base.kind === 'ENUM' && base.enumValues && base.enumValues.length > 0) { | ||
| props.push( | ||
| t.objectProperty(t.identifier('type'), t.stringLiteral('autocomplete')), | ||
| ); | ||
| props.push( | ||
| t.objectProperty(t.identifier('name'), t.stringLiteral(arg.name)), | ||
| ); | ||
| props.push( | ||
| t.objectProperty( | ||
| t.identifier('message'), | ||
| t.stringLiteral(arg.description || arg.name), | ||
| ), | ||
| ); | ||
| props.push( | ||
| t.objectProperty( | ||
| t.identifier('options'), | ||
| t.arrayExpression(base.enumValues.map((v) => t.stringLiteral(v))), | ||
| ), | ||
| ); | ||
| } else if (base.kind === 'SCALAR' && base.name === 'Boolean') { | ||
| props.push( | ||
| t.objectProperty(t.identifier('type'), t.stringLiteral('confirm')), | ||
| ); | ||
| props.push( | ||
| t.objectProperty(t.identifier('name'), t.stringLiteral(arg.name)), | ||
| ); | ||
| props.push( | ||
| t.objectProperty( | ||
| t.identifier('message'), | ||
| t.stringLiteral(arg.description || arg.name), | ||
| ), | ||
| ); | ||
| props.push( | ||
| t.objectProperty(t.identifier('default'), t.booleanLiteral(false)), | ||
| ); | ||
| } else if ( | ||
| base.kind === 'SCALAR' && | ||
| (base.name === 'Int' || base.name === 'Float') | ||
| ) { | ||
| props.push( | ||
| t.objectProperty(t.identifier('type'), t.stringLiteral('text')), | ||
| ); | ||
| props.push( | ||
| t.objectProperty(t.identifier('name'), t.stringLiteral(arg.name)), | ||
| ); | ||
| props.push( | ||
| t.objectProperty( | ||
| t.identifier('message'), | ||
| t.stringLiteral(arg.description || `${arg.name} (number)`), | ||
| ), | ||
| ); | ||
| } else if (inner.kind === 'INPUT_OBJECT' && inner.inputFields) { | ||
| return buildInputObjectQuestion(arg.name, inner, required); | ||
| } else { | ||
| props.push( | ||
| t.objectProperty(t.identifier('type'), t.stringLiteral('text')), | ||
| ); | ||
| props.push( | ||
| t.objectProperty(t.identifier('name'), t.stringLiteral(arg.name)), | ||
| ); | ||
| props.push( | ||
| t.objectProperty( | ||
| t.identifier('message'), | ||
| t.stringLiteral(arg.description || arg.name), | ||
| ), | ||
| ); | ||
| } | ||
|
|
||
| if (required) { | ||
| props.push( | ||
| t.objectProperty(t.identifier('required'), t.booleanLiteral(true)), | ||
| ); | ||
| } | ||
|
|
||
| return t.objectExpression(props); | ||
| } | ||
|
|
||
| function buildInputObjectQuestion( | ||
| _name: string, | ||
| typeRef: CleanTypeRef, | ||
| _required: boolean, | ||
| ): t.ObjectExpression { | ||
| if (typeRef.inputFields && typeRef.inputFields.length > 0) { | ||
| const firstField = typeRef.inputFields[0]; | ||
| return buildQuestionObject(firstField); | ||
| } | ||
| return t.objectExpression([ | ||
| t.objectProperty(t.identifier('type'), t.stringLiteral('text')), | ||
| t.objectProperty(t.identifier('name'), t.stringLiteral(_name)), | ||
| t.objectProperty( | ||
| t.identifier('message'), | ||
| t.stringLiteral(_name), | ||
| ), | ||
| ]); | ||
| } | ||
|
|
||
| export function buildQuestionsArray(args: CleanArgument[]): t.ArrayExpression { | ||
| const questions: t.Expression[] = []; | ||
| for (const arg of args) { | ||
| const base = resolveBaseType(arg.type); | ||
| const { inner } = unwrapNonNull(arg.type); | ||
|
|
||
| if (inner.kind === 'INPUT_OBJECT' && inner.inputFields) { | ||
| for (const field of inner.inputFields) { | ||
| questions.push(buildQuestionObject(field)); | ||
| } | ||
| } else if (base.kind === 'INPUT_OBJECT' && base.inputFields) { | ||
| for (const field of base.inputFields) { | ||
| questions.push(buildQuestionObject(field)); | ||
| } | ||
| } else { | ||
| questions.push(buildQuestionObject(arg)); | ||
| } | ||
| } | ||
| return t.arrayExpression(questions); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔴 No
cli/index.tsbarrel generated, causing root barrelexport * from './cli'to failThe root barrel (
barrel.ts:218-220) generatesexport * from './cli'whenhasCliis true, but the CLI generator never produces acli/index.tsfile. Module resolution will fail because there is no entry point for the./clidirectory.Root Cause
Looking at the files generated by
generateCliincli/index.ts:40-69, the output files are:executor.tscommands/context.tscommands/auth.tscommands/<table>.ts(per table)commands/<custom-op>.ts(per custom op)commands.tsNo
index.tsbarrel is generated for thecli/directory. Compare with the ORM generator, which producesorm/index.tsviaclient-generator.ts:338.When the root barrel emits
export * from './cli', TypeScript/Node will try to resolvecli/index.ts(orcli/index.js), which doesn't exist.Impact: The root
index.tsbarrel will fail to compile due to the unresolvable./clire-export.Prompt for agents
Was this helpful? React with 👍 or 👎 to provide feedback.