Skip to content

Commit

Permalink
chore: fix formatSchema
Browse files Browse the repository at this point in the history
  • Loading branch information
jkomyno committed Apr 11, 2024
1 parent bc10741 commit 009e2a0
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions packages/internals/src/engine-commands/formatSchema.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import fs from 'node:fs/promises'
import fs from 'node:fs'
import { promisify } from 'node:util'

import { match } from 'ts-pattern'

Expand All @@ -8,6 +9,9 @@ import { type Datamodel, schemaToStringDebug } from '../utils/datamodel'
import { prismaSchemaWasm } from '../wasm'
import { getLintWarningsAsText, lintSchema } from './lintSchema'

const readFile = promisify(fs.readFile)
const exists = promisify(fs.exists)

type FormatSchemaParams = { schema: Datamodel; schemaPath?: never } | { schema?: never; schemaPath: string }

function isSchemaOnly(schemaParams: FormatSchemaParams): schemaParams is { schema: Datamodel; schemaPath?: never } {
Expand Down Expand Up @@ -42,11 +46,11 @@ export async function formatSchema(
const schemaContent = await match({ schema, schemaPath } as FormatSchemaParams)
.when(isSchemaOnly, async ({ schema: _schema }) => await Promise.resolve(_schema))
.when(isSchemaPathOnly, async ({ schemaPath: _schemaPath }) => {
if (!(await fs.exists(_schemaPath))) {
if (!(await exists(_schemaPath))) {
throw new Error(`Schema at ${schemaPath} does not exist.`)
}

const _schema = await fs.readFile(_schemaPath, { encoding: 'utf8' })
const _schema = await readFile(_schemaPath, { encoding: 'utf8' })
return _schema
})
.exhaustive()
Expand Down

0 comments on commit 009e2a0

Please sign in to comment.