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

feat(config): use c12 for loading config files #242

Merged
merged 2 commits into from
Apr 4, 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
17 changes: 15 additions & 2 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,20 @@ description: Configure openapi-ts.

# Configuration

`openapi-ts` supports loading configuration from a file inside your project root directory. You can either create a `openapi-ts.config.cjs` file
`openapi-ts` supports loading configuration from a file inside your project root directory. Your configuration can be in many formats (as supported by [c12](https://github.com/unjs/c12)). Here are some example configs:

`openapi-ts.config.ts`

```ts
import { defineConfig } from '@hey-api/openapi-ts'

export default defineConfig({
input: 'path/to/openapi.json',
output: 'src/client',
})
```

`openapi-ts.config.cjs`

```js
/** @type {import('@hey-api/openapi-ts').UserConfig} */
Expand All @@ -15,7 +28,7 @@ module.exports = {
}
```

or `openapi-ts.config.mjs`
`openapi-ts.config.mjs`

```js
/** @type {import('@hey-api/openapi-ts').UserConfig} */
Expand Down
1 change: 1 addition & 0 deletions packages/openapi-ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
},
"dependencies": {
"@apidevtools/json-schema-ref-parser": "11.5.4",
"c12": "1.10.0",
"camelcase": "8.0.0",
"commander": "12.0.0",
"handlebars": "4.7.8"
Expand Down
28 changes: 10 additions & 18 deletions packages/openapi-ts/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { existsSync, readFileSync } from 'node:fs';
import { readFileSync } from 'node:fs';
import path from 'node:path';
import { pathToFileURL } from 'node:url';

import { loadConfig } from 'c12';
import { sync } from 'cross-spawn';

import { parse } from './openApi';
Expand All @@ -15,9 +15,6 @@ import { writeClient } from './utils/write/client';

type Dependencies = Record<string, unknown>;

// TODO: add support for `openapi-ts.config.ts`
const configFiles = ['openapi-ts.config.js', 'openapi-ts.config.cjs', 'openapi-ts.config.mjs'];

// Mapping of all dependencies used in each client. These should be installed in the generated client package
const clientDependencies: Record<Config['client'], string[]> = {
angular: ['@angular/common', '@angular/core', 'rxjs'],
Expand Down Expand Up @@ -78,20 +75,15 @@ const logMissingDependenciesWarning = (client: Config['client'], dependencies: D
}
};

const getConfigFromFile = async (): Promise<UserConfig | undefined> => {
const configPath = configFiles
.map(file => pathToFileURL(path.resolve(process.cwd(), file)))
.find(filePath => existsSync(filePath));
if (!configPath) {
return;
}
// @ts-ignore
const exported = await import(configPath);
return exported.default as UserConfig;
};

const getConfig = async (userConfig: UserConfig, dependencies: Dependencies) => {
const userConfigFromFile = await getConfigFromFile();
const { config: userConfigFromFile } = await loadConfig<UserConfig>({
jitiOptions: {
esmResolve: true,
},
name: 'openapi-ts',
overrides: userConfig,
});

if (userConfigFromFile) {
userConfig = { ...userConfigFromFile, ...userConfig };
}
Expand Down
Loading