-
Notifications
You must be signed in to change notification settings - Fork 3
Add recursive endpoints, docs improvements #18
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
raphjaph
merged 7 commits into
raphjaph:master
from
ordinalspractice:recursive-endpoints
Feb 6, 2025
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
108bad9
add recursive endpoints, docs improvements
ordinalspractice 31cf4b1
generator improvements
ordinalspractice 1052b36
amend
ordinalspractice 1d89248
amend
ordinalspractice c2e6ca6
Amend
raphjaph 5380468
fix github btn
ordinalspractice 4c88a1f
Merge branch 'recursive-endpoints' of github.com:ordinalspractice/ord…
ordinalspractice 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,8 @@ | ||
| docs/api-docs.json | ||
|
|
||
|
|
||
|
|
||
|
|
||
| # Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore | ||
|
|
||
| # Logs | ||
|
|
||
Binary file not shown.
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 |
|---|---|---|
| @@ -1,127 +1,75 @@ | ||
| import * as ts from 'typescript'; | ||
| import * as fs from 'fs'; | ||
| import * as path from 'path'; | ||
| import { CustomType, extractZodSchema } from './generateTypesDocs'; | ||
| import { getMethodDocs, type MethodDoc } from './generateMethodsDocs'; | ||
|
|
||
| interface Documentation { | ||
| classMethods: MethodDoc[]; | ||
| exportedTypes: CustomType[]; | ||
| } | ||
| import { Documentation, MethodDocumentation, TypeDocumentation } from './types'; | ||
| import { generateTypeDocs } from './generateTypesDocs'; | ||
| import { generateMethodDocs } from './generateMethodsDocs'; | ||
| import { | ||
| getAllFiles, | ||
| createTSProgram, | ||
| writeDocs | ||
| } from './shared-utils'; | ||
|
|
||
| function generateDocs(sourceFiles: string[]): Documentation { | ||
| const options: ts.CompilerOptions = { | ||
| target: ts.ScriptTarget.ESNext, | ||
| module: ts.ModuleKind.ESNext, | ||
| allowJs: true, | ||
| checkJs: true, | ||
| noEmit: true, | ||
| types: ['node'], | ||
| skipLibCheck: true, | ||
| }; | ||
|
|
||
| const program = ts.createProgram(sourceFiles, options); | ||
| const methods: MethodDoc[] = []; | ||
|
|
||
| // Sort source files by their base names | ||
| const sortedFiles = [...sourceFiles].sort((a, b) => | ||
| path.basename(a).localeCompare(path.basename(b)) | ||
| ); | ||
|
|
||
| // Use OrderedMap to maintain file ordering | ||
| const typesByFile = new Map<string, CustomType[]>(); | ||
| const program = createTSProgram(sourceFiles); | ||
| const methods: MethodDocumentation[] = []; | ||
| const typesByFile = new Map<string, TypeDocumentation[]>(); | ||
|
|
||
| // First pass: collect types maintaining file order | ||
| for (const sourceFile of sortedFiles) { | ||
| const fileName = sourceFile; | ||
|
|
||
| if (!fileName.endsWith('.ts') || fileName.includes('node_modules')) { | ||
| continue; | ||
| } | ||
|
|
||
| const content = fs.readFileSync(fileName, 'utf8'); | ||
|
|
||
| if (content.includes('z.object') || content.includes('z.enum')) { | ||
| const extracted = extractZodSchema(content, path.basename(fileName)); | ||
| if (extracted.length > 0) { | ||
| typesByFile.set(fileName, extracted); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Second pass: collect methods | ||
| // First pass: collect types | ||
| for (const sourceFile of program.getSourceFiles()) { | ||
| const fileName = sourceFile.fileName; | ||
|
|
||
| if (!fileName.endsWith('.ts') || fileName.includes('node_modules')) { | ||
| // Skip node_modules and declaration files | ||
| if (fileName.includes('node_modules') || fileName.endsWith('.d.ts')) { | ||
| continue; | ||
| } | ||
|
|
||
| function visit(node: ts.Node) { | ||
| if (ts.isClassDeclaration(node)) { | ||
|
|
||
| node.members.forEach(member => { | ||
| if (ts.isMethodDeclaration(member)) { | ||
| try { | ||
| const doc = getMethodDocs(member, sourceFile); | ||
| if (doc) { | ||
| methods.push(doc); | ||
| } | ||
| } catch (error) { | ||
| console.error('Error processing method:', error); | ||
| } | ||
| } | ||
| }); | ||
| // Look for Zod schemas | ||
| if (sourceFile.getText().includes('z.object') || | ||
| sourceFile.getText().includes('z.enum')) { | ||
| const types = generateTypeDocs(sourceFile, program); | ||
| if (types.length > 0) { | ||
| typesByFile.set(fileName, types); | ||
| } | ||
|
|
||
| ts.forEachChild(node, visit); | ||
| } | ||
|
|
||
| visit(sourceFile); | ||
| // Also collect methods from this file | ||
| const fileMethods = generateMethodDocs(sourceFile, program); | ||
| if (fileMethods.length > 0) { | ||
| methods.push(...fileMethods); | ||
| } | ||
| } | ||
|
|
||
| // Combine types in file order | ||
| const allTypes: CustomType[] = []; | ||
| for (const [file, types] of typesByFile.entries()) { | ||
| allTypes.push(...types); | ||
| } | ||
| // Combine all types | ||
| const allTypes = Array.from(typesByFile.values()).flat(); | ||
|
|
||
| return { | ||
| classMethods: methods, | ||
| exportedTypes: allTypes | ||
| methods: methods.sort((a, b) => a.name.localeCompare(b.name)), | ||
| types: allTypes.sort((a, b) => a.name.localeCompare(b.name)) | ||
| }; | ||
| } | ||
|
|
||
| // Ensure the docs directory exists | ||
| const docsDir = path.join(process.cwd(), 'docs'); | ||
| if (!fs.existsSync(docsDir)) { | ||
| fs.mkdirSync(docsDir); | ||
| } | ||
| // Main execution | ||
| const SOURCE_DIR = './src'; | ||
| const DOCS_DIR = path.join(process.cwd(), 'docs'); | ||
|
|
||
| // Generate documentation | ||
| const sourceDir = './src'; | ||
| const sourceFiles = [ | ||
| ...fs.readdirSync(sourceDir) | ||
| .filter(file => file.endsWith('.ts')) | ||
| .map(file => path.join(sourceDir, file)), | ||
| ...getAllFiles(path.join(sourceDir, 'schemas')) | ||
| .filter(file => file.endsWith('.ts')) | ||
| ].sort((a, b) => path.basename(a).localeCompare(path.basename(b))); // Sort all source files | ||
| // Get source files | ||
| const sourceFiles = getAllFiles({ | ||
| sourceDir: SOURCE_DIR, | ||
| includeExtensions: ['.ts'], | ||
| excludePatterns: [ | ||
| /\.test\.ts$/, | ||
| /\.spec\.ts$/, | ||
| /\.d\.ts$/, | ||
| /\/dist\//, | ||
| /\/build\//, | ||
| /\/node_modules\// | ||
| ] | ||
| }); | ||
|
|
||
| // Generate and write documentation | ||
| const docs = generateDocs(sourceFiles); | ||
|
|
||
| fs.writeFileSync( | ||
| path.join(docsDir, 'api-docs.json'), | ||
| JSON.stringify(docs, null, 2) | ||
| writeDocs( | ||
| path.join(DOCS_DIR, 'api-docs.json'), | ||
| docs | ||
| ); | ||
|
|
||
| function getAllFiles(dir: string): string[] { | ||
| if (!fs.existsSync(dir)) { | ||
| return []; | ||
| } | ||
| const files = fs.readdirSync(dir); | ||
| return files.flatMap(file => { | ||
| const fullPath = path.join(dir, file); | ||
| return fs.statSync(fullPath).isDirectory() ? getAllFiles(fullPath) : fullPath; | ||
| }); | ||
| } | ||
| console.log(`Documentation generated from ${sourceFiles.length} files at docs/api-docs.json`); |
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.
Uh oh!
There was an error while loading. Please reload this page.