Skip to content

Commit

Permalink
add supportedFormatSchema
Browse files Browse the repository at this point in the history
  • Loading branch information
FunamaYukina committed Dec 4, 2024
1 parent 2cfde85 commit 3eb9d67
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 13 deletions.
27 changes: 16 additions & 11 deletions frontend/packages/cli/src/cli/runPreprocess.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import fs, { readFileSync } from 'node:fs'
import path from 'node:path'
import { type SupportedFormat, parse } from '@liam-hq/db-structure/parser'
import {
type SupportedFormat,
parse,
supportedFormatSchema,
} from '@liam-hq/db-structure/parser'
import * as v from 'valibot'

export async function runPreprocess(
inputPath: string,
Expand All @@ -13,21 +18,21 @@ export async function runPreprocess(

const input = readFileSync(inputPath, 'utf8')

let json = null
if (format === 'schemarb' || format === 'postgres') {
try {
json = await parse(input, format)
} catch (error) {
throw new Error(
`Failed to parse ${format} file: ${error instanceof Error ? error.message : String(error)}`,
)
}
} else {
if (!v.safeParse(supportedFormatSchema, format).success) {
throw new Error(
'--format is missing, invalid, or specifies an unsupported format. Please provide a valid format (e.g., "schemarb" or "postgres").',
)
}

let json = null
try {
json = await parse(input, format)
} catch (error) {
throw new Error(
`Failed to parse ${format} file: ${error instanceof Error ? error.message : String(error)}`,
)
}

const filePath = path.join(outputDir, 'schema.json')

if (!fs.existsSync(outputDir)) {
Expand Down
6 changes: 5 additions & 1 deletion frontend/packages/db-structure/src/parser.ts
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
export { parse, type SupportedFormat } from './parser/index.js'
export {
parse,
type SupportedFormat,
supportedFormatSchema,
} from './parser/index.js'
8 changes: 7 additions & 1 deletion frontend/packages/db-structure/src/parser/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import * as v from 'valibot'
import type { DBStructure } from '../schema/index.js'
import { processor as schemarbProcessor } from './schemarb/index.js'
import { processor as postgresqlProcessor } from './sql/index.js'

export type SupportedFormat = 'schemarb' | 'postgres'
export const supportedFormatSchema = v.union([
v.literal('schemarb'),
v.literal('postgres'),
])

export type SupportedFormat = v.InferOutput<typeof supportedFormatSchema>

// TODO: Add error handling and tests
export const parse = (
Expand Down

0 comments on commit 3eb9d67

Please sign in to comment.