Skip to content
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
4 changes: 2 additions & 2 deletions packages/mock/src/msw/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const getMockPropertiesWithoutFunc = (properties: any, spec: OpenAPIObject) =>
: stringify(value as string)!;

acc[key] = implementation?.replaceAll(
/import_faker.defaults|import_faker.faker/g,
/import_faker\.defaults|import_faker\.faker|_faker\.faker/g,
'faker',
);
return acc;
Expand Down Expand Up @@ -284,7 +284,7 @@ export const getMockOptionsDataOverride = (
: stringify(responseOverride);

return implementation?.replaceAll(
/import_faker.defaults|import_faker.faker/g,
/import_faker\.defaults|import_faker\.faker|_faker\.faker/g,
'faker',
);
};
1 change: 1 addition & 0 deletions packages/orval/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
"execa": "^9.6.0",
"find-up": "8.0.0",
"fs-extra": "^11.3.2",
"jiti": "^2.6.1",
"js-yaml": "4.1.0",
"openapi3-ts": "4.5.0",
"remeda": "^2.32.0",
Expand Down
33 changes: 22 additions & 11 deletions packages/orval/src/generate.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import fs from 'node:fs';
import path from 'node:path';
import process from 'node:process';
import url from 'node:url';

import {
asyncReduce,
Expand All @@ -17,6 +16,7 @@ import {
type NormalizedOptions,
removeFilesAndEmptyFolders,
} from '@orval/core';
import { createJiti } from 'jiti';

import { importSpecs } from './import-specs';
import { normalizeOptions } from './utils/options';
Expand Down Expand Up @@ -95,10 +95,14 @@ export const generateSpecs = async (

function findConfigFile(configFilePath?: string) {
if (configFilePath) {
if (!fs.existsSync(configFilePath))
const absolutePath = path.isAbsolute(configFilePath)
? configFilePath
: path.resolve(process.cwd(), configFilePath);

if (!fs.existsSync(absolutePath))
throw new Error(`Config file ${configFilePath} does not exist`);

return configFilePath;
return absolutePath;
}

const root = process.cwd();
Expand All @@ -113,21 +117,28 @@ function findConfigFile(configFilePath?: string) {
throw new Error(`No config file found in ${root}`);
}

async function loadConfigFile(configFilePath: string): Promise<ConfigExternal> {
const jiti = createJiti(import.meta.url, {
interopDefault: true,
});

const module = await jiti.import(configFilePath, { default: true });

if (module === undefined) {
throw new Error(`${configFilePath} doesn't have a default export`);
}

return await Promise.resolve(module as ConfigExternal);
}

export const generateConfig = async (
configFile?: string,
options?: GlobalOptions,
) => {
const configFilePath = findConfigFile(configFile);
let configExternal: ConfigExternal;
try {
const importPath = url.pathToFileURL(configFilePath).href;
const importedModule = (await import(importPath)) as {
default?: ConfigExternal;
};
if (importedModule.default === undefined) {
throw new Error(`${configFilePath} doesn't have a default export`);
}
configExternal = importedModule.default;
configExternal = await loadConfigFile(configFilePath);
} catch (error) {
const errorMsg = error instanceof Error ? error.message : 'unknown error';
throw new Error(`failed to load from ${configFilePath} => ${errorMsg}`);
Expand Down
8 changes: 8 additions & 0 deletions tests/configs/multi-file.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
/* eslint-disable simple-import-sort/exports */
/* eslint-disable simple-import-sort/imports */
/* eslint-disable unicorn/prefer-export-from */
import { defineConfig } from 'orval';

// test esm module import/export
import { MY_CONST, ANOTHER_CONST } from './my-module';
console.log('TESTING ES MODULE IMPORT/EXPORT', MY_CONST, ANOTHER_CONST);
export { MY_CONST, ANOTHER_CONST };

export default defineConfig({
api: {
input: '../specifications/multi-files/api.yaml',
Expand Down
2 changes: 2 additions & 0 deletions tests/configs/my-module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const MY_CONST = 42;
export const ANOTHER_CONST = 'Hello, World!';
10 changes: 10 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -15989,6 +15989,15 @@ __metadata:
languageName: node
linkType: hard

"jiti@npm:^2.6.1":
version: 2.6.1
resolution: "jiti@npm:2.6.1"
bin:
jiti: lib/jiti-cli.mjs
checksum: 10c0/79b2e96a8e623f66c1b703b98ec1b8be4500e1d217e09b09e343471bbb9c105381b83edbb979d01cef18318cc45ce6e153571b6c83122170eefa531c64b6789b
languageName: node
linkType: hard

"js-beautify@npm:^1.14.9":
version: 1.15.4
resolution: "js-beautify@npm:1.15.4"
Expand Down Expand Up @@ -18551,6 +18560,7 @@ __metadata:
execa: "npm:^9.6.0"
find-up: "npm:8.0.0"
fs-extra: "npm:^11.3.2"
jiti: "npm:^2.6.1"
js-yaml: "npm:4.1.0"
openapi-types: "npm:^12.1.3"
openapi3-ts: "npm:4.5.0"
Expand Down