Skip to content

Commit 68fd4fb

Browse files
authoredJun 20, 2024
fix: applies fileExtension to schemas generated files (#1473)
* fix: applies fileExtension to schemas generated files * formatted code
1 parent 6859a07 commit 68fd4fb

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed
 

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

+20-7
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ const getSchema = ({
3535
return file;
3636
};
3737

38-
const getPath = (path: string, name: string): string =>
39-
upath.join(path, `/${name}.ts`);
38+
const getPath = (path: string, name: string, fileExtension: string): string =>
39+
upath.join(path, `/${name}${fileExtension}`);
4040

4141
export const writeModelInline = (acc: string, model: string): string =>
4242
acc + `${model}\n`;
@@ -48,6 +48,7 @@ export const writeSchema = async ({
4848
path,
4949
schema,
5050
target,
51+
fileExtension,
5152
specKey,
5253
isRootKey,
5354
specsName,
@@ -56,6 +57,7 @@ export const writeSchema = async ({
5657
path: string;
5758
schema: GeneratorSchema;
5859
target: string;
60+
fileExtension: string;
5961
specKey: string;
6062
isRootKey: boolean;
6163
specsName: Record<string, string>;
@@ -65,7 +67,7 @@ export const writeSchema = async ({
6567

6668
try {
6769
await fs.outputFile(
68-
getPath(path, name),
70+
getPath(path, name, fileExtension),
6971
getSchema({ schema, target, isRootKey, specsName, header, specKey }),
7072
);
7173
} catch (e) {
@@ -77,6 +79,7 @@ export const writeSchemas = async ({
7779
schemaPath,
7880
schemas,
7981
target,
82+
fileExtension,
8083
specKey,
8184
isRootKey,
8285
specsName,
@@ -86,6 +89,7 @@ export const writeSchemas = async ({
8689
schemaPath: string;
8790
schemas: GeneratorSchema[];
8891
target: string;
92+
fileExtension: string;
8993
specKey: string;
9094
isRootKey: boolean;
9195
specsName: Record<string, string>;
@@ -98,6 +102,7 @@ export const writeSchemas = async ({
98102
path: schemaPath,
99103
schema,
100104
target,
105+
fileExtension,
101106
specKey,
102107
isRootKey,
103108
specsName,
@@ -107,7 +112,7 @@ export const writeSchemas = async ({
107112
);
108113

109114
if (indexFiles) {
110-
const schemaFilePath = upath.join(schemaPath, '/index.ts');
115+
const schemaFilePath = upath.join(schemaPath, `/index${fileExtension}`);
111116
await fs.ensureFile(schemaFilePath);
112117

113118
// Ensure separate files are used for parallel schema writing.
@@ -139,14 +144,22 @@ export const writeSchemas = async ({
139144

140145
const stringData = data.toString();
141146

147+
const ext = fileExtension.endsWith('.ts')
148+
? fileExtension.slice(0, -3)
149+
: fileExtension;
150+
142151
const importStatements = schemas
143152
.filter((schema) => {
144153
return (
145-
!stringData.includes(`export * from './${camel(schema.name)}'`) &&
146-
!stringData.includes(`export * from "./${camel(schema.name)}"`)
154+
!stringData.includes(
155+
`export * from './${camel(schema.name)}${ext}'`,
156+
) &&
157+
!stringData.includes(
158+
`export * from "./${camel(schema.name)}${ext}"`,
159+
)
147160
);
148161
})
149-
.map((schema) => `export * from './${camel(schema.name)}';`);
162+
.map((schema) => `export * from './${camel(schema.name)}${ext}';`);
150163

151164
const currentFileExports = (stringData
152165
.match(/export \* from(.*)('|")/g)

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

+5
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ export const writeSpecs = async (
6262
if (output.schemas) {
6363
const rootSchemaPath = output.schemas;
6464

65+
const fileExtension = ['tags', 'tags-split', 'split'].includes(output.mode)
66+
? '.ts'
67+
: output.fileExtension ?? '.ts';
68+
6569
await Promise.all(
6670
Object.entries(schemas).map(([specKey, schemas]) => {
6771
const schemaPath = !isRootKey(specKey, target)
@@ -72,6 +76,7 @@ export const writeSpecs = async (
7276
schemaPath,
7377
schemas,
7478
target,
79+
fileExtension,
7580
specsName,
7681
specKey,
7782
isRootKey: isRootKey(specKey, target),

0 commit comments

Comments
 (0)
Please sign in to comment.