Skip to content

Commit

Permalink
fix: applies fileExtension to schemas generated files (#1473)
Browse files Browse the repository at this point in the history
* fix: applies fileExtension to schemas generated files

* formatted code
  • Loading branch information
DanielVenable authored Jun 20, 2024
1 parent 6859a07 commit 68fd4fb
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
27 changes: 20 additions & 7 deletions packages/core/src/writers/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ const getSchema = ({
return file;
};

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

export const writeModelInline = (acc: string, model: string): string =>
acc + `${model}\n`;
Expand All @@ -48,6 +48,7 @@ export const writeSchema = async ({
path,
schema,
target,
fileExtension,
specKey,
isRootKey,
specsName,
Expand All @@ -56,6 +57,7 @@ export const writeSchema = async ({
path: string;
schema: GeneratorSchema;
target: string;
fileExtension: string;
specKey: string;
isRootKey: boolean;
specsName: Record<string, string>;
Expand All @@ -65,7 +67,7 @@ export const writeSchema = async ({

try {
await fs.outputFile(
getPath(path, name),
getPath(path, name, fileExtension),
getSchema({ schema, target, isRootKey, specsName, header, specKey }),
);
} catch (e) {
Expand All @@ -77,6 +79,7 @@ export const writeSchemas = async ({
schemaPath,
schemas,
target,
fileExtension,
specKey,
isRootKey,
specsName,
Expand All @@ -86,6 +89,7 @@ export const writeSchemas = async ({
schemaPath: string;
schemas: GeneratorSchema[];
target: string;
fileExtension: string;
specKey: string;
isRootKey: boolean;
specsName: Record<string, string>;
Expand All @@ -98,6 +102,7 @@ export const writeSchemas = async ({
path: schemaPath,
schema,
target,
fileExtension,
specKey,
isRootKey,
specsName,
Expand All @@ -107,7 +112,7 @@ export const writeSchemas = async ({
);

if (indexFiles) {
const schemaFilePath = upath.join(schemaPath, '/index.ts');
const schemaFilePath = upath.join(schemaPath, `/index${fileExtension}`);
await fs.ensureFile(schemaFilePath);

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

const stringData = data.toString();

const ext = fileExtension.endsWith('.ts')
? fileExtension.slice(0, -3)
: fileExtension;

const importStatements = schemas
.filter((schema) => {
return (
!stringData.includes(`export * from './${camel(schema.name)}'`) &&
!stringData.includes(`export * from "./${camel(schema.name)}"`)
!stringData.includes(
`export * from './${camel(schema.name)}${ext}'`,
) &&
!stringData.includes(
`export * from "./${camel(schema.name)}${ext}"`,
)
);
})
.map((schema) => `export * from './${camel(schema.name)}';`);
.map((schema) => `export * from './${camel(schema.name)}${ext}';`);

const currentFileExports = (stringData
.match(/export \* from(.*)('|")/g)
Expand Down
5 changes: 5 additions & 0 deletions packages/orval/src/write-specs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ export const writeSpecs = async (
if (output.schemas) {
const rootSchemaPath = output.schemas;

const fileExtension = ['tags', 'tags-split', 'split'].includes(output.mode)
? '.ts'
: output.fileExtension ?? '.ts';

await Promise.all(
Object.entries(schemas).map(([specKey, schemas]) => {
const schemaPath = !isRootKey(specKey, target)
Expand All @@ -72,6 +76,7 @@ export const writeSpecs = async (
schemaPath,
schemas,
target,
fileExtension,
specsName,
specKey,
isRootKey: isRootKey(specKey, target),
Expand Down

0 comments on commit 68fd4fb

Please sign in to comment.