11import fs , { rm } from "node:fs/promises" ;
22import nodePath from "node:path" ;
33
4- import { dereference } from "@apidevtools/json-schema-ref-parser" ;
5- import { type FSWatcher , watch as watchFile } from "chokidar" ;
6- import createDebug from "debug" ;
74import { createHttpTerminator , type HttpTerminator } from "http-terminator" ;
85
96import { startRepl as startReplServer } from "./repl/repl.js" ;
107import type { Config } from "./server/config.js" ;
11- import { CHOKIDAR_OPTIONS } from "./server/constants.js" ;
128import { ContextRegistry } from "./server/context-registry.js" ;
139import { createKoaApp } from "./server/create-koa-app.js" ;
14- import {
15- Dispatcher ,
16- type DispatcherRequest ,
17- type OpenApiDocument ,
18- } from "./server/dispatcher.js" ;
10+ import { Dispatcher , type DispatcherRequest } from "./server/dispatcher.js" ;
1911import { koaMiddleware } from "./server/koa-middleware.js" ;
12+ import { loadOpenApiDocument } from "./server/load-openapi-document.js" ;
2013import { ModuleLoader } from "./server/module-loader.js" ;
14+ import { OpenApiWatcher } from "./server/openapi-watcher.js" ;
2115import { Registry } from "./server/registry.js" ;
2216import { Transpiler } from "./server/transpiler.js" ;
2317import { CodeGenerator } from "./typescript-generator/code-generator.js" ;
2418import { runtimeCanExecuteErasableTs } from "./util/runtime-can-execute-erasable-ts.js" ;
2519
26- const debug = createDebug ( "counterfact:app" ) ;
20+ export { loadOpenApiDocument } from "./server/load-openapi-document.js" ;
2721
2822type MswHandlerMap = {
2923 [ key : string ] : ( request : MockRequest ) => Promise < unknown > ;
@@ -41,19 +35,6 @@ const allowedMethods = [
4135
4236export type MockRequest = DispatcherRequest & { rawPath : string } ;
4337
44- export async function loadOpenApiDocument ( source : string ) {
45- try {
46- return ( await dereference ( source ) ) as OpenApiDocument ;
47- } catch ( error ) {
48- debug ( "could not load OpenAPI document from %s: %o" , source , error ) ;
49- const details = error instanceof Error ? error . message : String ( error ) ;
50- throw new Error (
51- `Could not load the OpenAPI spec from "${ source } ".\n${ details } ` ,
52- { cause : error } ,
53- ) ;
54- }
55- }
56-
5738const mswHandlers : MswHandlerMap = { } ;
5839
5940export async function handleMswRequest ( request : MockRequest ) {
@@ -167,6 +148,8 @@ export async function counterfact(config: Config) {
167148
168149 const koaApp = createKoaApp ( registry , middleware , config , contextRegistry ) ;
169150
151+ const openApiWatcher = new OpenApiWatcher ( config . openApiPath , dispatcher ) ;
152+
170153 async function start ( options : Config ) {
171154 const { generate, startServer, watch, buildCache } = options ;
172155
@@ -179,35 +162,10 @@ export async function counterfact(config: Config) {
179162 }
180163
181164 let httpTerminator : HttpTerminator | undefined ;
182- let openApiWatcher : FSWatcher | undefined ;
183-
184- if (
185- startServer &&
186- config . openApiPath !== "_" &&
187- ! config . openApiPath . startsWith ( "http" )
188- ) {
189- openApiWatcher = watchFile ( config . openApiPath , CHOKIDAR_OPTIONS ) . on (
190- "change" ,
191- ( ) => {
192- void ( async ( ) => {
193- try {
194- dispatcher . openApiDocument = await loadOpenApiDocument (
195- config . openApiPath ,
196- ) ;
197- debug ( "reloaded OpenAPI document from %s" , config . openApiPath ) ;
198- } catch ( error : unknown ) {
199- debug (
200- "failed to reload OpenAPI document from %s: %o" ,
201- config . openApiPath ,
202- error ,
203- ) ;
204- }
205- } ) ( ) ;
206- } ,
207- ) ;
208- }
209165
210166 if ( startServer ) {
167+ await openApiWatcher . watch ( ) ;
168+
211169 if ( ! nativeTs ) {
212170 await transpiler . watch ( ) ;
213171 }
@@ -232,7 +190,7 @@ export async function counterfact(config: Config) {
232190 await codeGenerator . stopWatching ( ) ;
233191 await transpiler . stopWatching ( ) ;
234192 await moduleLoader . stopWatching ( ) ;
235- await openApiWatcher ?. close ( ) ;
193+ await openApiWatcher . stopWatching ( ) ;
236194 await httpTerminator ?. terminate ( ) ;
237195 } ,
238196 } ;
0 commit comments