Skip to content

Commit 913ac00

Browse files
soartec-labmelloware
andauthoredApr 19, 2024··
feat: added output.fileExtension option to possible specifying the extension for the generated file. (#1320)
* feat: add `output.extension` option * feat: use `output.extension` in each mode * docs: add document for `extension` option * chore: change option name from `extension` to `fileExtension` * Update output.md --------- Co-authored-by: Melloware <mellowaredev@gmail.com>
1 parent 21c503c commit 913ac00

File tree

7 files changed

+59
-5
lines changed

7 files changed

+59
-5
lines changed
 

‎docs/src/pages/reference/configuration/output.md

+30-1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,36 @@ module.exports = {
5757
};
5858
```
5959

60+
### fileExtension
61+
62+
Type: `String`.
63+
64+
Default Value: `.ts`.
65+
66+
Specify the file extension for files generated automatically. Modes such as `tags`, `tags-split`, and `split` do not alter `schema` files; they only pertain to `client` files.
67+
68+
```js
69+
module.exports = {
70+
petstore: {
71+
output: {
72+
mode: 'split'
73+
target: './/gen/endpoints',
74+
schemas: './gen/model',
75+
fileExtension: '.gen.ts'
76+
},
77+
},
78+
};
79+
```
80+
81+
```
82+
src/gen/
83+
├── endpoints
84+
│ └── swaggerPetstore.gen.ts
85+
└── model
86+
├── listPetsParams.ts
87+
└── pets.ts
88+
```
89+
6090
### workspace
6191

6292
Type: `String`.
@@ -803,7 +833,6 @@ Default Value: `Detect from package json`.
803833

804834
Use to specify a version for the generated hooks. This is useful if you want to force a version for the hooks.
805835

806-
807836
#### angular
808837

809838
Type: `Object`.

‎packages/core/src/types.ts

+2
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export type NormalizedOutputOptions = {
4343
workspace?: string;
4444
target?: string;
4545
schemas?: string;
46+
fileExtension: string;
4647
mode: OutputMode;
4748
mock?: GlobalMockOptions | ClientMockBuilder;
4849
override: NormalizedOverrideOutput;
@@ -161,6 +162,7 @@ export type OutputOptions = {
161162
workspace?: string;
162163
target?: string;
163164
schemas?: string;
165+
fileExtension?: string;
164166
mode?: OutputMode;
165167
// If mock is a boolean, it will use the default mock options (type: msw)
166168
mock?: boolean | GlobalMockOptions | ClientMockBuilder;

‎packages/core/src/writers/single-mode.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export const writeSingleMode = async ({
2121
try {
2222
const { path, dirname } = getFileInfo(output.target, {
2323
backupFilename: camel(builder.info.title),
24+
extension: output.fileExtension,
2425
});
2526

2627
const {
@@ -38,7 +39,11 @@ export const writeSingleMode = async ({
3839
let data = header;
3940

4041
const schemasPath = output.schemas
41-
? upath.relativeSafe(dirname, getFileInfo(output.schemas).dirname)
42+
? upath.relativeSafe(
43+
dirname,
44+
getFileInfo(output.schemas, { extension: output.fileExtension })
45+
.dirname,
46+
)
4247
: undefined;
4348

4449
const isAllowSyntheticDefaultImports = isSyntheticDefaultImportsAllow(

‎packages/core/src/writers/split-mode.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export const writeSplitMode = async ({
2222
try {
2323
const { filename, dirname, extension } = getFileInfo(output.target, {
2424
backupFilename: camel(builder.info.title),
25+
extension: output.fileExtension,
2526
});
2627

2728
const {
@@ -40,7 +41,11 @@ export const writeSplitMode = async ({
4041
let mockData = header;
4142

4243
const relativeSchemasPath = output.schemas
43-
? upath.relativeSafe(dirname, getFileInfo(output.schemas).dirname)
44+
? upath.relativeSafe(
45+
dirname,
46+
getFileInfo(output.schemas, { extension: output.fileExtension })
47+
.dirname,
48+
)
4449
: './' + filename + '.schemas';
4550

4651
const isAllowSyntheticDefaultImports = isSyntheticDefaultImportsAllow(

‎packages/core/src/writers/split-tags-mode.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export const writeSplitTagsMode = async ({
2222
}: WriteModeProps): Promise<string[]> => {
2323
const { filename, dirname, extension } = getFileInfo(output.target, {
2424
backupFilename: camel(builder.info.title),
25+
extension: output.fileExtension,
2526
});
2627

2728
const target = generateTargetForTags(builder, output);
@@ -50,7 +51,11 @@ export const writeSplitTagsMode = async ({
5051

5152
const relativeSchemasPath = output.schemas
5253
? '../' +
53-
upath.relativeSafe(dirname, getFileInfo(output.schemas).dirname)
54+
upath.relativeSafe(
55+
dirname,
56+
getFileInfo(output.schemas, { extension: output.fileExtension })
57+
.dirname,
58+
)
5459
: '../' + filename + '.schemas';
5560

5661
const importsForBuilder =

‎packages/core/src/writers/tags-mode.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export const writeTagsMode = async ({
2121
}: WriteModeProps): Promise<string[]> => {
2222
const { filename, dirname, extension } = getFileInfo(output.target, {
2323
backupFilename: camel(builder.info.title),
24+
extension: output.fileExtension,
2425
});
2526

2627
const target = generateTargetForTags(builder, output);
@@ -47,7 +48,11 @@ export const writeTagsMode = async ({
4748
let data = header;
4849

4950
const schemasPathRelative = output.schemas
50-
? upath.relativeSafe(dirname, getFileInfo(output.schemas).dirname)
51+
? upath.relativeSafe(
52+
dirname,
53+
getFileInfo(output.schemas, { extension: output.fileExtension })
54+
.dirname,
55+
)
5156
: './' + filename + '.schemas';
5257

5358
const importsForBuilder = [

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

+3
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ export const normalizeOptions = async (
102102
};
103103
}
104104

105+
const defaultFileExtension = '.ts';
106+
105107
const normalizedOptions: NormalizedOptions = {
106108
input: {
107109
target: globalOptions.input
@@ -126,6 +128,7 @@ export const normalizeOptions = async (
126128
? normalizePath(globalOptions.output, process.cwd())
127129
: normalizePath(outputOptions.target, outputWorkspace),
128130
schemas: normalizePath(outputOptions.schemas, outputWorkspace),
131+
fileExtension: outputOptions.fileExtension || defaultFileExtension,
129132
workspace: outputOptions.workspace ? outputWorkspace : undefined,
130133
client: outputOptions.client ?? client ?? OutputClient.AXIOS_FUNCTIONS,
131134
mode: normalizeOutputMode(outputOptions.mode ?? mode),

0 commit comments

Comments
 (0)
Please sign in to comment.