Skip to content

Commit b3db343

Browse files
Eric Butleranymaniax
Eric Butler
andauthoredSep 28, 2023
feat(core): option to disable creating index files (#915)
* feat(core): option to disable creating index files * change option to indexFiles --------- Co-authored-by: Victor <victor@emaniax.io>
1 parent 7ea9b85 commit b3db343

File tree

4 files changed

+51
-42
lines changed

4 files changed

+51
-42
lines changed
 

‎packages/core/src/types.ts

+2
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export type NormalizedOutputOptions = {
5151
tsconfig?: Tsconfig;
5252
packageJson?: PackageJson;
5353
headers: boolean;
54+
indexFiles: boolean;
5455
};
5556

5657
export type NormalizedOverrideOutput = {
@@ -163,6 +164,7 @@ export type OutputOptions = {
163164
tsconfig?: string | Tsconfig;
164165
packageJson?: string;
165166
headers?: boolean;
167+
indexFiles?: boolean;
166168
};
167169

168170
export type SwaggerParserOptions = Omit<SwaggerParser.Options, 'validate'> & {

‎packages/core/src/writers/schemas.ts

+27-24
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ export const writeSchemas = async ({
8282
isRootKey,
8383
specsName,
8484
header,
85+
indexFiles,
8586
}: {
8687
schemaPath: string;
8788
schemas: GeneratorSchema[];
@@ -90,10 +91,8 @@ export const writeSchemas = async ({
9091
isRootKey: boolean;
9192
specsName: Record<string, string>;
9293
header: string;
94+
indexFiles: boolean;
9395
}) => {
94-
const schemaFilePath = upath.join(schemaPath, '/index.ts');
95-
await fs.ensureFile(schemaFilePath);
96-
9796
await Promise.all(
9897
schemas.map((schema) =>
9998
writeSchema({
@@ -108,32 +107,36 @@ export const writeSchemas = async ({
108107
),
109108
);
110109

111-
try {
112-
const data = await fs.readFile(schemaFilePath);
110+
if (indexFiles) {
111+
const schemaFilePath = upath.join(schemaPath, '/index.ts');
112+
await fs.ensureFile(schemaFilePath);
113+
try {
114+
const data = await fs.readFile(schemaFilePath);
113115

114-
const stringData = data.toString();
116+
const stringData = data.toString();
115117

116-
const importStatements = schemas
117-
.filter((schema) => {
118-
return (
119-
!stringData.includes(`export * from './${camel(schema.name)}'`) &&
120-
!stringData.includes(`export * from "./${camel(schema.name)}"`)
121-
);
122-
})
123-
.map((schema) => `export * from './${camel(schema.name)}';`);
118+
const importStatements = schemas
119+
.filter((schema) => {
120+
return (
121+
!stringData.includes(`export * from './${camel(schema.name)}'`) &&
122+
!stringData.includes(`export * from "./${camel(schema.name)}"`)
123+
);
124+
})
125+
.map((schema) => `export * from './${camel(schema.name)}';`);
124126

125-
const currentFileExports = (stringData
126-
.match(/export \* from(.*)('|")/g)
127-
?.map((s) => s + ';') ?? []) as string[];
127+
const currentFileExports = (stringData
128+
.match(/export \* from(.*)('|")/g)
129+
?.map((s) => s + ';') ?? []) as string[];
128130

129-
const exports = [...currentFileExports, ...importStatements]
130-
.sort()
131-
.join('\n');
131+
const exports = [...currentFileExports, ...importStatements]
132+
.sort()
133+
.join('\n');
132134

133-
const fileContent = `${header}\n${exports}`;
135+
const fileContent = `${header}\n${exports}`;
134136

135-
await fs.writeFile(schemaFilePath, fileContent);
136-
} catch (e) {
137-
throw `Oups... 🍻. An Error occurred while writing schema index file ${schemaFilePath} => ${e}`;
137+
await fs.writeFile(schemaFilePath, fileContent);
138+
} catch (e) {
139+
throw `Oups... 🍻. An Error occurred while writing schema index file ${schemaFilePath} => ${e}`;
140+
}
138141
}
139142
};

‎packages/orval/src/utils/options.ts

+1
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ export const normalizeOptions = async (
120120
tsconfig,
121121
packageJson,
122122
headers: outputOptions.headers ?? false,
123+
indexFiles: outputOptions.indexFiles ?? true,
123124
override: {
124125
...outputOptions.override,
125126
mock: {

‎packages/orval/src/write-specs.ts

+21-18
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ export const writeSpecs = async (
7272
specKey,
7373
isRootKey: isRootKey(specKey, target),
7474
header,
75+
indexFiles: output.indexFiles,
7576
});
7677
}),
7778
);
@@ -108,24 +109,26 @@ export const writeSpecs = async (
108109
);
109110
}
110111

111-
const indexFile = upath.join(workspacePath, '/index.ts');
112-
113-
if (await fs.pathExists(indexFile)) {
114-
const data = await fs.readFile(indexFile, 'utf8');
115-
const importsNotDeclared = imports.filter((imp) => !data.includes(imp));
116-
await fs.appendFile(
117-
indexFile,
118-
uniq(importsNotDeclared)
119-
.map((imp) => `export * from '${imp}';`)
120-
.join('\n') + '\n',
121-
);
122-
} else {
123-
await fs.outputFile(
124-
indexFile,
125-
uniq(imports)
126-
.map((imp) => `export * from '${imp}';`)
127-
.join('\n') + '\n',
128-
);
112+
if (output.indexFiles) {
113+
const indexFile = upath.join(workspacePath, '/index.ts');
114+
115+
if (await fs.pathExists(indexFile)) {
116+
const data = await fs.readFile(indexFile, 'utf8');
117+
const importsNotDeclared = imports.filter((imp) => !data.includes(imp));
118+
await fs.appendFile(
119+
indexFile,
120+
uniq(importsNotDeclared)
121+
.map((imp) => `export * from '${imp}';`)
122+
.join('\n') + '\n',
123+
);
124+
} else {
125+
await fs.outputFile(
126+
indexFile,
127+
uniq(imports)
128+
.map((imp) => `export * from '${imp}';`)
129+
.join('\n') + '\n',
130+
);
131+
}
129132
}
130133

131134
implementationPaths = [indexFile, ...implementationPaths];

1 commit comments

Comments
 (1)

vercel[bot] commented on Sep 28, 2023

@vercel[bot]
Please sign in to comment.