Skip to content

Commit 3e21286

Browse files
committed
refactor: use Babel AST for root barrel generation
Refactored the barrel file generation to use Babel AST via the new generateRootBarrel() function in barrel.ts, for consistency with the rest of the codebase.
1 parent 52508bd commit 3e21286

File tree

2 files changed

+45
-17
lines changed

2 files changed

+45
-17
lines changed

graphql/codegen/src/core/codegen/barrel.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,45 @@ export function generateMainBarrel(
192192
return generateCode(statements);
193193
}
194194

195+
// ============================================================================
196+
// Root barrel for unified output
197+
// ============================================================================
198+
199+
export interface RootBarrelOptions {
200+
hasTypes?: boolean;
201+
hasHooks?: boolean;
202+
hasOrm?: boolean;
203+
}
204+
205+
/**
206+
* Generate the root index.ts barrel file for the output directory.
207+
* Re-exports from subdirectories based on which generators are enabled.
208+
*/
209+
export function generateRootBarrel(options: RootBarrelOptions = {}): string {
210+
const { hasTypes = false, hasHooks = false, hasOrm = false } = options;
211+
const statements: t.Statement[] = [];
212+
213+
if (hasTypes) {
214+
statements.push(exportAllFrom('./types'));
215+
}
216+
if (hasHooks) {
217+
statements.push(exportAllFrom('./hooks'));
218+
}
219+
if (hasOrm) {
220+
statements.push(exportAllFrom('./orm'));
221+
}
222+
223+
// Add file header as leading comment on first statement
224+
if (statements.length > 0) {
225+
addJSDocComment(statements[0], [
226+
'Generated SDK - auto-generated, do not edit',
227+
'@generated by @constructive-io/graphql-codegen',
228+
]);
229+
}
230+
231+
return generateCode(statements);
232+
}
233+
195234
// ============================================================================
196235
// Custom operation barrels (includes both table and custom hooks)
197236
// ============================================================================

graphql/codegen/src/core/generate.ts

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import path from 'path';
99
import { createSchemaSource, validateSourceOptions } from './introspect';
1010
import { runCodegenPipeline, validateTablesFound } from './pipeline';
1111
import { generate as generateReactQueryFiles } from './codegen';
12+
import { generateRootBarrel } from './codegen/barrel';
1213
import { generateOrm as generateOrmFiles } from './codegen/orm';
1314
import { generateSharedTypes } from './codegen/shared';
1415
import { writeGeneratedFiles } from './output';
@@ -197,23 +198,11 @@ export async function generate(options: GenerateOptions = {}): Promise<GenerateR
197198
// Generate barrel file at output root
198199
// This re-exports from the appropriate subdirectories based on which generators are enabled
199200
if (!options.dryRun) {
200-
const exports: string[] = [];
201-
if (bothEnabled) {
202-
exports.push("export * from './types';");
203-
}
204-
if (runReactQuery) {
205-
exports.push("export * from './hooks';");
206-
}
207-
if (runOrm) {
208-
exports.push("export * from './orm';");
209-
}
210-
211-
const barrelContent = `/**
212-
* Generated SDK - auto-generated, do not edit
213-
* @generated by @constructive-io/graphql-codegen
214-
*/
215-
${exports.join('\n')}
216-
`;
201+
const barrelContent = generateRootBarrel({
202+
hasTypes: bothEnabled,
203+
hasHooks: runReactQuery,
204+
hasOrm: runOrm,
205+
});
217206
await writeGeneratedFiles([{ path: 'index.ts', content: barrelContent }], outputRoot, []);
218207
}
219208

0 commit comments

Comments
 (0)