Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: applies fileExtension to schemas generated files #1473

Merged
merged 2 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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