Skip to content

Commit

Permalink
feat: rename outputPath to outputFilePath & disable formatting by def…
Browse files Browse the repository at this point in the history
…ault
  • Loading branch information
greeeg committed Dec 26, 2023
1 parent fb0eab5 commit 09b59b0
Show file tree
Hide file tree
Showing 13 changed files with 87 additions and 41 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,8 @@ const run = () => {
return
}

generateTypeDefinitions(document, { outputPath: './client/generated/types.ts' })
generateTypeDefinitions(document, { outputPath: './api/generated/types.ts' })
generateTypeDefinitions(document, { outputFilePath: './client/generated/types.ts' })
generateTypeDefinitions(document, { outputFilePath: './api/generated/types.ts' })
}

run()
Expand Down
4 changes: 2 additions & 2 deletions packages/openapi-kit/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,8 @@ const run = () => {
return
}

generateTypeDefinitions(document, { outputPath: './client/generated/types.ts' })
generateTypeDefinitions(document, { outputPath: './api/generated/types.ts' })
generateTypeDefinitions(document, { outputFilePath: './client/generated/types.ts' })
generateTypeDefinitions(document, { outputFilePath: './api/generated/types.ts' })
}

run()
Expand Down
36 changes: 27 additions & 9 deletions packages/openapi-kit/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,14 @@ import { createDirectory, fileExists } from '../src/utils/fileSystem'
interface RunOptions {
openAPIFilePath: string
outputDirectoryPath: string
prettyOutput: boolean
}

const run = async ({ openAPIFilePath, outputDirectoryPath }: RunOptions) => {
const run = async ({
openAPIFilePath,
outputDirectoryPath,
prettyOutput,
}: RunOptions) => {
const filePath = path.resolve(process.cwd(), openAPIFilePath)
const document = await parseDocument(filePath)
if (!document) {
Expand All @@ -32,44 +37,50 @@ const run = async ({ openAPIFilePath, outputDirectoryPath }: RunOptions) => {
'.ts',
'',
)}`
const typeDefinitionsOutputPath = path.resolve(
const typeDefinitionsOutputFilePath = path.resolve(
process.cwd(),
outputDirectoryPath,
typeDefinitionsFileName,
)
const apiClientFileName = 'apiClient.ts'
const apiClientImportPath = `./${apiClientFileName.replace('.ts', '')}`
const apiClientOutputPath = path.resolve(
const apiClientOutputFilePath = path.resolve(
process.cwd(),
outputDirectoryPath,
apiClientFileName,
)
const mockDataFileName = 'mockData.ts'
const mockOutputPath = path.resolve(
const mockOutputFilePath = path.resolve(
process.cwd(),
outputDirectoryPath,
mockDataFileName,
)
const reactQueryHooksFileName = 'reactQuery.tsx'
const reactQueryHooksOutputPath = path.resolve(
const reactQueryHooksOutputFilePath = path.resolve(
process.cwd(),
outputDirectoryPath,
reactQueryHooksFileName,
)

generateTypeDefinitions(document, { outputPath: typeDefinitionsOutputPath })
generateTypeDefinitions(document, {
outputFilePath: typeDefinitionsOutputFilePath,
prettyOutput,
})
generateAPIClient(document, {
outputPath: apiClientOutputPath,
outputFilePath: apiClientOutputFilePath,
typeDefinitionsImportPath,
prettyOutput,
})
generateMockData(document, {
outputPath: mockOutputPath,
outputFilePath: mockOutputFilePath,
typeDefinitionsImportPath,
prettyOutput,
})
generateReactQueryHooks(document, {
outputPath: reactQueryHooksOutputPath,
outputFilePath: reactQueryHooksOutputFilePath,
typeDefinitionsImportPath,
apiClientImportPath,
prettyOutput,
})
}

Expand All @@ -90,6 +101,12 @@ yargs(hideBin(process.argv))
describe: 'Output directory',
demandOption: true,
},
prettyOutput: {
type: 'boolean',
default: false,
describe: 'Wether or not the output should be formatted using Prettier',
demandOption: false,
},
},
(argv) => {
if (!fileExists(argv.file)) {
Expand All @@ -108,6 +125,7 @@ yargs(hideBin(process.argv))
run({
openAPIFilePath: argv.file,
outputDirectoryPath: argv.outputDir,
prettyOutput: argv.prettyOutput,
}).catch(() => {
console.log('An error ocurred')
process.exit(1)
Expand Down
10 changes: 7 additions & 3 deletions packages/openapi-kit/src/generators/apiClient/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ import { APIClientGeneratorOptions } from './types'

export const generateAPIClient = async (
document: OpenAPIDocument,
{ outputPath, typeDefinitionsImportPath }: APIClientGeneratorOptions,
{
outputFilePath,
typeDefinitionsImportPath,
prettyOutput = false,
}: APIClientGeneratorOptions,
) => {
const lines: string[] = [...getHeaderLines(typeDefinitionsImportPath)]
const operations = getOperations(document)
Expand Down Expand Up @@ -43,6 +47,6 @@ export const generateAPIClient = async (
'export type APIClient = ReturnType<typeof getAPIClient>',
)

const fileContent = await formatOutput(lines.join('\n'))
writeFile(outputPath, fileContent)
const fileContent = await formatOutput(lines.join('\n'), prettyOutput)
writeFile(outputFilePath, fileContent)
}
5 changes: 3 additions & 2 deletions packages/openapi-kit/src/generators/apiClient/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export interface APIClientGeneratorOptions {
import { GeneratorOptions } from '../types'

export interface APIClientGeneratorOptions extends GeneratorOptions {
typeDefinitionsImportPath: string
outputPath: string
}
19 changes: 9 additions & 10 deletions packages/openapi-kit/src/generators/mockData/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import { capitalize } from 'lodash'

import { OpenAPIDocument } from '../../types'
import { writeFile } from '../../utils/fileSystem'
import { formatOutput } from '../../utils/format'
import { getOperations, isResponseObject } from '../../utils/openAPI'
import { toTypeName, toValidIdentifier } from '../../utils/typescript'
import { toTypeName } from '../../utils/typescript'
import { generateMock, logResolvedRefsCallStackExceeded } from './functions'
import { MockDataGeneratorOptions } from './types'

export const generateMockData = async (
document: OpenAPIDocument,
{ outputPath, typeDefinitionsImportPath }: MockDataGeneratorOptions,
{
outputFilePath,
typeDefinitionsImportPath,
prettyOutput = false,
}: MockDataGeneratorOptions,
) => {
const operations = getOperations(document)
const lines: string[] = [
Expand All @@ -32,10 +34,7 @@ export const generateMockData = async (
return
}

const mockName = `${pascalCaseOperationId}Response${capitalize(
toValidIdentifier(name),
)}`

const mockName = `${pascalCaseOperationId}Response${toTypeName(name)}`
const type = `Paths.${pascalCaseOperationId}.Responses.${toTypeName(
name,
)}`
Expand All @@ -48,7 +47,7 @@ export const generateMockData = async (
}
})

const fileContent = await formatOutput(lines.join('\n'))
writeFile(outputPath, fileContent)
const fileContent = await formatOutput(lines.join('\n'), prettyOutput)
writeFile(outputFilePath, fileContent)
logResolvedRefsCallStackExceeded(resolvedRefs)
}
5 changes: 3 additions & 2 deletions packages/openapi-kit/src/generators/mockData/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export interface MockDataGeneratorOptions {
import { GeneratorOptions } from '../types'

export interface MockDataGeneratorOptions extends GeneratorOptions {
typeDefinitionsImportPath: string
outputPath: string
}
7 changes: 4 additions & 3 deletions packages/openapi-kit/src/generators/reactQuery/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import { ReactQueryGeneratorOptions } from './types'
export const generateReactQueryHooks = async (
document: OpenAPIDocument,
{
outputPath,
outputFilePath,
typeDefinitionsImportPath,
apiClientImportPath,
prettyOutput = false,
}: ReactQueryGeneratorOptions,
) => {
const lines: string[] = [
Expand All @@ -30,7 +31,7 @@ export const generateReactQueryHooks = async (
lines.push(...buildMutation(operation))
})

const fileContent = await formatOutput(lines.join('\n'))
const fileContent = await formatOutput(lines.join('\n'), prettyOutput)

writeFile(outputPath, fileContent)
writeFile(outputFilePath, fileContent)
}
5 changes: 3 additions & 2 deletions packages/openapi-kit/src/generators/reactQuery/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export interface ReactQueryGeneratorOptions {
import { GeneratorOptions } from '../types'

export interface ReactQueryGeneratorOptions extends GeneratorOptions {
typeDefinitionsImportPath: string
apiClientImportPath: string
outputPath: string
}
5 changes: 3 additions & 2 deletions packages/openapi-kit/src/generators/typeDefinitions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { TypeDefinitionsGeneratorOptions } from './types'

export const generateTypeDefinitions = async (
document: OpenAPIDocument,
{ outputPath }: TypeDefinitionsGeneratorOptions,
{ outputFilePath, prettyOutput = false }: TypeDefinitionsGeneratorOptions,
) => {
const schema = parseSchema(document as JsonSchema)

Expand All @@ -24,7 +24,8 @@ export const generateTypeDefinitions = async (

const fileContent = await formatOutput(
result.replace(/declare namespace/g, 'export declare namespace'),
prettyOutput,
)

writeFile(outputPath, fileContent)
writeFile(outputFilePath, fileContent)
}
6 changes: 3 additions & 3 deletions packages/openapi-kit/src/generators/typeDefinitions/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export interface TypeDefinitionsGeneratorOptions {
outputPath: string
}
import { GeneratorOptions } from '../types'

export interface TypeDefinitionsGeneratorOptions extends GeneratorOptions {}
16 changes: 16 additions & 0 deletions packages/openapi-kit/src/generators/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export interface GeneratorOptions {
/**
* Where the generated file should be written to.
*/
outputFilePath: string
/**
* Whether the output should be formatted
* using Prettier or not.
*
* Disabling this option will result in
* faster generation.
*
* @default false
*/
prettyOutput?: boolean
}
6 changes: 5 additions & 1 deletion packages/openapi-kit/src/utils/format.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import * as prettier from 'prettier'

export const formatOutput = async (content: string) => {
export const formatOutput = async (content: string, shouldRun: boolean) => {
if (!shouldRun) {
return content
}

const formattedContent = await prettier.format(content, {
semi: false,
parser: 'babel-ts',
Expand Down

0 comments on commit 09b59b0

Please sign in to comment.