Skip to content

Commit

Permalink
fix: add ability to skip generating index file with output.indexFile
Browse files Browse the repository at this point in the history
  • Loading branch information
mrlubos committed Jan 15, 2025
1 parent 55f27bf commit 3e43937
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/wicked-apples-teach.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hey-api/openapi-ts': patch
---

fix: add ability to skip generating index file with output.indexFile
10 changes: 8 additions & 2 deletions packages/openapi-ts/src/generate/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,11 @@ export const generateOutput = async ({ context }: { context: IR.Context }) => {
continue;
}

if (!file.isEmpty() && file.exportFromIndex) {
if (
!file.isEmpty() &&
file.exportFromIndex &&
context.config.output.indexFile
) {
// TODO: parser - add export method for more granular control over
// what's exported so we can support named exports
indexFile.add(
Expand All @@ -156,6 +160,8 @@ export const generateOutput = async ({ context }: { context: IR.Context }) => {
file.write('\n\n');
}

indexFile.write();
if (context.config.output.indexFile) {
indexFile.write();
}
}
};
1 change: 1 addition & 0 deletions packages/openapi-ts/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ const getOutput = (userConfig: ClientConfig): Config['output'] => {
let output: Config['output'] = {
clean: true,
format: false,
indexFile: true,
lint: false,
path: '',
};
Expand Down
8 changes: 8 additions & 0 deletions packages/openapi-ts/src/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,14 @@ export interface ClientConfig {
* @default false
*/
format?: Formatters | false;
/**
* Should the exports from plugin files be re-exported in the index
* barrel file? By default, this is enabled and only default plugins
* are re-exported.
*
* @default true
*/
indexFile?: boolean;
/**
* Process output folder with linter?
*
Expand Down
21 changes: 19 additions & 2 deletions packages/openapi-ts/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ describe('OpenAPI v3', () => {
client: 'legacy/fetch',
exportCore: true,
input: '',
output: '',
output: {
path: '',
},
plugins: [
'@hey-api/sdk',
{
Expand Down Expand Up @@ -287,6 +289,18 @@ describe('OpenAPI v3', () => {
description: 'generate legacy positional arguments',
name: 'v3_legacy_positional_args',
},
{
config: createConfig({
client: '@hey-api/client-fetch',
output: {
indexFile: false,
path: '',
},
plugins: ['@hey-api/typescript'],
}),
description: 'generate output without index file',
name: 'v3_no_index',
},
{
config: createConfig({
plugins: [
Expand Down Expand Up @@ -473,7 +487,10 @@ describe('OpenAPI v3', () => {
await createClient({
...config,
input: V3_SPEC_PATH,
output,
output: {
...(typeof config.output === 'object' ? config.output : {}),
path: output,
},
});
sync(`${output}**/*.ts`).forEach((file) => {
const content = fs.readFileSync(file, 'utf8').toString();
Expand Down

0 comments on commit 3e43937

Please sign in to comment.