Skip to content
This repository has been archived by the owner on Jul 4, 2023. It is now read-only.

Commit

Permalink
fix(generate): fix generate command producing files with wrong name
Browse files Browse the repository at this point in the history
fix #535
  • Loading branch information
tamj0rd2 committed Oct 30, 2020
1 parent 4077ce6 commit 2e32feb
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/commands/generate/generate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe('Generate', () => {

await generate(mockSchemaGenerator, types, './out')

expect(mockedIo.writeJsonAsync).toBeCalledWith(schema1, `./out/${types[0]}.json`)
expect(mockedIo.writeJsonAsync).toBeCalledWith(schema2, `./out/${types[1]}.json`)
expect(mockedIo.writeJsonAsync).toBeCalledWith(`./out/${types[0].get()}.json`, schema1)
expect(mockedIo.writeJsonAsync).toBeCalledWith(`./out/${types[1].get()}.json`, schema2)
})
})
2 changes: 1 addition & 1 deletion src/commands/generate/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const generate = async (
await Promise.all(
types.map(async (type) => {
const schema = await schemaRetriever.load(type)
return writeJsonAsync(schema, `${outputPath}/${type}.json`)
return writeJsonAsync(`${outputPath}/${type.get()}.json`, schema)
}),
)
}
Expand Down
4 changes: 2 additions & 2 deletions src/io.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ describe('io', () => {
mockPath.resolve.mockReturnValueOnce(resolvedFilePath)
mockPath.dirname.mockReturnValueOnce(folderPath)

await writeJsonAsync({}, filePath)
await writeJsonAsync(filePath, {})

expect(mockPath.resolve).toBeCalledWith<Parameters<Resolve>>(filePath)
expect(mockPath.dirname).toBeCalledWith<Parameters<Dirname>>(resolvedFilePath)
Expand All @@ -114,7 +114,7 @@ describe('io', () => {
mockPath.resolve.mockReturnValueOnce(resolvedFilePath)

const data = { hello: 'world' }
await writeJsonAsync(data, filePath)
await writeJsonAsync(filePath, data)

expect(mockPath.resolve).toBeCalledWith<Parameters<Resolve>>(filePath)
expect(mockFs.writeFile).toBeCalledWith<Parameters<WriteFile>>(
Expand Down
7 changes: 5 additions & 2 deletions src/io.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { promises } from 'fs'
import { safeLoad } from 'js-yaml'
import { JSONSchema7 } from 'json-schema'
import { isAbsolute, resolve, dirname } from 'path'

const { writeFile: writeFileAsync, mkdir: mkdirAsync, readFile: readFileAsync } = promises
Expand Down Expand Up @@ -36,8 +37,10 @@ export const readFixture = (basePath: string, fixturePath: string): Promise<Data
}

// TODO: swap these args around
// eslint-disable-next-line @typescript-eslint/ban-types
export const writeJsonAsync = async (obj: object, path: string): Promise<void> => {
export const writeJsonAsync = async (
path: string,
obj: Record<string, unknown> | JSONSchema7,
): Promise<void> => {
const outputPath = resolve(path)
await mkdirAsync(dirname(outputPath), { recursive: true })

Expand Down

0 comments on commit 2e32feb

Please sign in to comment.