@@ -3,6 +3,7 @@ import react from '@vitejs/plugin-react';
33import { existsSync , readFileSync , writeFileSync , mkdirSync , readdirSync , statSync } from 'fs' ;
44import { join , resolve , relative , basename , dirname } from 'path' ;
55import chalk from 'chalk' ;
6+ import * as yaml from 'js-yaml' ;
67
78interface ServeOptions {
89 port : string ;
@@ -55,10 +56,9 @@ export async function serve(schemaPath: string, options: ServeOptions) {
5556
5657 // Read and validate schema
5758 try {
58- const schemaContent = readFileSync ( fullSchemaPath , 'utf-8' ) ;
59- schema = JSON . parse ( schemaContent ) ;
59+ schema = parseSchemaFile ( fullSchemaPath ) ;
6060 } catch ( error ) {
61- throw new Error ( `Invalid JSON in schema file: ${ error instanceof Error ? error . message : error } ` ) ;
61+ throw new Error ( `Invalid schema file: ${ error instanceof Error ? error . message : error } ` ) ;
6262 }
6363 }
6464
@@ -362,6 +362,37 @@ export default {
362362 writeFileSync ( join ( tmpDir , 'tsconfig.json' ) , JSON . stringify ( tsconfig , null , 2 ) ) ;
363363}
364364
365+ // Helper function to check if a file is a supported schema file
366+ function isSupportedSchemaFile ( filename : string ) : boolean {
367+ return filename . endsWith ( '.schema.json' ) ||
368+ filename . endsWith ( '.page.json' ) ||
369+ filename . endsWith ( '.schema.yml' ) ||
370+ filename . endsWith ( '.schema.yaml' ) ||
371+ filename . endsWith ( '.page.yml' ) ||
372+ filename . endsWith ( '.page.yaml' ) ;
373+ }
374+
375+ // Helper function to extract the base filename without extension
376+ function getBaseFileName ( filename : string ) : string {
377+ // Remove supported extensions
378+ return filename
379+ . replace ( / \. s c h e m a \. ( j s o n | y m l | y a m l ) $ / , '' )
380+ . replace ( / \. p a g e \. ( j s o n | y m l | y a m l ) $ / , '' ) ;
381+ }
382+
383+ // Helper function to parse schema file (JSON or YAML)
384+ function parseSchemaFile ( filePath : string ) : any {
385+ const content = readFileSync ( filePath , 'utf-8' ) ;
386+
387+ if ( filePath . endsWith ( '.json' ) ) {
388+ return JSON . parse ( content ) ;
389+ } else if ( filePath . endsWith ( '.yml' ) || filePath . endsWith ( '.yaml' ) ) {
390+ return yaml . load ( content ) ;
391+ }
392+
393+ throw new Error ( `Unsupported file format: ${ filePath } ` ) ;
394+ }
395+
365396function scanPagesDirectory ( pagesDir : string ) : RouteInfo [ ] {
366397 const routes : RouteInfo [ ] = [ ] ;
367398
@@ -376,15 +407,15 @@ function scanPagesDirectory(pagesDir: string): RouteInfo[] {
376407 // Recursively scan subdirectories
377408 const newPrefix = routePrefix + '/' + entry ;
378409 scanDir ( fullPath , newPrefix ) ;
379- } else if ( entry . endsWith ( '.schema.json' ) ) {
410+ } else if ( isSupportedSchemaFile ( entry ) ) {
380411 // Process schema file
381- const fileName = basename ( entry , '.schema.json' ) ;
412+ const fileName = getBaseFileName ( entry ) ;
382413 let routePath : string ;
383414 let isDynamic = false ;
384415 let paramName : string | undefined ;
385416
386417 if ( fileName === 'index' ) {
387- // index.schema.json maps to the directory path
418+ // index.schema.json or index.page.json maps to the directory path
388419 routePath = routePrefix || '/' ;
389420 } else if ( fileName . startsWith ( '[' ) && fileName . endsWith ( ']' ) ) {
390421 // Dynamic route: [id].schema.json -> /:id
@@ -398,8 +429,7 @@ function scanPagesDirectory(pagesDir: string): RouteInfo[] {
398429
399430 // Read and parse schema
400431 try {
401- const schemaContent = readFileSync ( fullPath , 'utf-8' ) ;
402- const schema = JSON . parse ( schemaContent ) ;
432+ const schema = parseSchemaFile ( fullPath ) ;
403433
404434 routes . push ( {
405435 path : routePath ,
@@ -409,7 +439,7 @@ function scanPagesDirectory(pagesDir: string): RouteInfo[] {
409439 paramName,
410440 } ) ;
411441 } catch ( error ) {
412- console . warn ( chalk . yellow ( `⚠ Warning: Failed to parse ${ fullPath } ` ) ) ;
442+ console . warn ( chalk . yellow ( `⚠ Warning: Failed to parse ${ fullPath } : ${ error instanceof Error ? error . message : error } ` ) ) ;
413443 }
414444 }
415445 }
0 commit comments