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: support custom config file path #737

Merged
merged 1 commit into from
Jul 1, 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
5 changes: 5 additions & 0 deletions .changeset/heavy-melons-fry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hey-api/openapi-ts': patch
---

fix: support custom config file path
4 changes: 4 additions & 0 deletions packages/openapi-ts/bin/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const params = program
.option('-d, --debug', 'Run in debug mode?')
.option('--dry-run [value]', 'Skip writing files to disk?')
.option('--exportCore [value]', 'Write core files to disk')
.option('-f, --file [value]', 'Path to the config file')
.option(
'-i, --input <value>',
'OpenAPI specification (path, url, or string content)',
Expand Down Expand Up @@ -56,6 +57,9 @@ const processParams = (obj, booleanKeys) => {
obj[key] = parsedValue;
}
}
if (obj.file) {
obj.configFile = obj.file;
}
return obj;
};

Expand Down
9 changes: 9 additions & 0 deletions packages/openapi-ts/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,14 @@
};

const initConfigs = async (userConfig: UserConfig): Promise<Config[]> => {
let configurationFile: string | undefined = undefined;
if (userConfig.configFile) {
const parts = userConfig.configFile.split('.');
configurationFile = parts.slice(0, parts.length - 1).join('.');
}

Check warning on line 178 in packages/openapi-ts/src/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/index.ts#L176-L178

Added lines #L176 - L178 were not covered by tests

const { config: configFromFile } = await loadConfig<UserConfig>({
configFile: configurationFile,
jitiOptions: {
esmResolve: true,
},
Expand All @@ -191,6 +198,7 @@
const {
base,
client = 'fetch',
configFile = '',
debug = false,
dryRun = false,
exportCore = true,
Expand Down Expand Up @@ -233,6 +241,7 @@
return setConfig({
base,
client,
configFile,
debug,
dryRun,
exportCore: isStandaloneClient(client) ? false : exportCore,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { getOperationName, parseResponseStatusCode } from '../operation';
describe('getOperationName', () => {
const options1: Parameters<typeof setConfig>[0] = {
client: 'fetch',
configFile: '',
debug: false,
dryRun: true,
exportCore: false,
Expand All @@ -29,6 +30,7 @@ describe('getOperationName', () => {

const options2: Parameters<typeof setConfig>[0] = {
client: 'fetch',
configFile: '',
debug: false,
dryRun: true,
exportCore: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ describe('getServices', () => {
it('should create a unnamed service if tags are empty', () => {
setConfig({
client: 'fetch',
configFile: '',
debug: false,
dryRun: true,
exportCore: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ describe('getServices', () => {
it('should create a unnamed service if tags are empty', () => {
setConfig({
client: 'fetch',
configFile: '',
debug: false,
dryRun: true,
exportCore: true,
Expand Down
5 changes: 4 additions & 1 deletion packages/openapi-ts/src/types/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ export interface Client {
server: string;
services: Service[];
/**
* Map of generated types, keys are type names
* Map of generated types where type names are keys. This is used to track
* uniquely generated types as we may want to deduplicate if there are
* multiple definitions with the same name but different value, or if we
* want to transform names.
*/
types: Record<string, ModelMeta>;
version: string;
Expand Down
5 changes: 5 additions & 0 deletions packages/openapi-ts/src/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ export interface ClientConfig {
| 'fetch'
| 'node'
| 'xhr';
/**
* Path to the config file. Set this value if you don't use the default
* config file name, or it's not located in the project root.
*/
configFile?: string;
/**
* Run in debug mode?
* @default false
Expand Down
2 changes: 2 additions & 0 deletions packages/openapi-ts/src/utils/__tests__/handlebars.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ describe('registerHandlebarHelpers', () => {
it('should register the helpers', () => {
setConfig({
client: 'fetch',
configFile: '',
debug: false,
dryRun: false,
exportCore: true,
Expand Down Expand Up @@ -40,6 +41,7 @@ describe('registerHandlebarTemplates', () => {
it('should return correct templates', () => {
setConfig({
client: 'fetch',
configFile: '',
debug: false,
dryRun: false,
exportCore: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ describe('writeClientClass', () => {
it('writes to filesystem', async () => {
setConfig({
client: 'fetch',
configFile: '',
debug: false,
dryRun: false,
exportCore: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ describe('writeClient', () => {
it('writes to filesystem', async () => {
setConfig({
client: 'fetch',
configFile: '',
debug: false,
dryRun: false,
exportCore: true,
Expand Down
3 changes: 3 additions & 0 deletions packages/openapi-ts/src/utils/write/__tests__/core.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ describe('writeCore', () => {

setConfig({
client: 'fetch',
configFile: '',
debug: false,
dryRun: false,
exportCore: true,
Expand Down Expand Up @@ -81,6 +82,7 @@ describe('writeCore', () => {

const config = setConfig({
client: 'fetch',
configFile: '',
debug: false,
dryRun: false,
exportCore: true,
Expand Down Expand Up @@ -119,6 +121,7 @@ describe('writeCore', () => {
const config = setConfig({
base: 'foo',
client: 'fetch',
configFile: '',
debug: false,
dryRun: false,
exportCore: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ describe('processIndex', () => {
it('writes to filesystem', async () => {
setConfig({
client: 'fetch',
configFile: '',
debug: false,
dryRun: false,
exportCore: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ describe('processTypes', () => {
it('writes to filesystem', async () => {
setConfig({
client: 'fetch',
configFile: '',
debug: false,
dryRun: false,
exportCore: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ describe('processSchemas', () => {
it('writes to filesystem', async () => {
setConfig({
client: 'fetch',
configFile: '',
debug: false,
dryRun: false,
exportCore: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ describe('processServices', () => {
it('writes to filesystem', async () => {
setConfig({
client: 'fetch',
configFile: '',
debug: false,
dryRun: false,
exportCore: true,
Expand Down Expand Up @@ -107,6 +108,7 @@ describe('methodNameBuilder', () => {
it('use default name', async () => {
setConfig({
client: 'fetch',
configFile: '',
debug: false,
dryRun: false,
exportCore: true,
Expand Down Expand Up @@ -145,6 +147,7 @@ describe('methodNameBuilder', () => {

setConfig({
client: 'fetch',
configFile: '',
debug: false,
dryRun: false,
exportCore: true,
Expand Down