@@ -2,8 +2,7 @@ import type { Context } from "../types/index.js";
22import { existsSync } from "node:fs" ;
33import { join } from "node:path" ;
44import process from "node:process" ;
5- import chokidar from "chokidar" ;
6- import { debounce } from "es-toolkit" ;
5+ import { watch } from "../utils/watcher.js" ;
76import { ZoteroRunner } from "../utils/zotero-runner.js" ;
87import { Base } from "./base.js" ;
98import Build from "./builder.js" ;
@@ -69,47 +68,32 @@ export default class Serve extends Base {
6968 async watch ( ) {
7069 const { source } = this . ctx ;
7170
72- const watcher = chokidar . watch ( source , {
73- ignored : / ( ^ | [ / \\ ] ) \. ./ , // ignore dotfiles
74- persistent : true ,
75- } ) ;
76-
77- const onChangeDebounced = debounce ( async ( path : string ) => {
78- await this . onChange ( path ) . catch ( ( err ) => {
79- // Do not abort the watcher when errors occur
80- // in builds triggered by the watcher.
81- this . logger . error ( err ) ;
82- } ) ;
83- } , 500 ) ;
84-
85- watcher
86- . on ( "ready" , async ( ) => {
87- await this . ctx . hooks . callHook ( "serve:ready" , this . ctx ) ;
88- this . logger . clear ( ) ;
89- this . logger . ready ( "Server Ready!" ) ;
90- } )
91- . on ( "change" , async ( path ) => {
92- this . logger . clear ( ) ;
93- this . logger . info ( `${ path } changed` ) ;
94- await onChangeDebounced ( path ) ;
95- } )
96- . on ( "error" , ( err ) => {
97- this . logger . fail ( "Server start failed!" ) ;
98- this . logger . error ( err ) ;
99- } ) ;
100- }
101-
102- async onChange ( path : string ) {
103- await this . ctx . hooks . callHook ( "serve:onChanged" , this . ctx , path ) ;
104-
105- if ( path . endsWith ( ".ts" ) || path . endsWith ( ".tsx" ) ) {
106- await this . builder . esbuild ( ) ;
107- }
108- else {
109- await this . builder . run ( ) ;
110- }
111-
112- await this . reload ( ) ;
71+ watch (
72+ source ,
73+ {
74+ onReady : async ( ) => {
75+ await this . ctx . hooks . callHook ( "serve:ready" , this . ctx ) ;
76+ this . logger . clear ( ) ;
77+ this . logger . ready ( "Server Ready!" ) ;
78+ } ,
79+ onChange : async ( path ) => {
80+ await this . ctx . hooks . callHook ( "serve:onChanged" , this . ctx , path ) ;
81+
82+ if ( path . endsWith ( ".ts" ) || path . endsWith ( ".tsx" ) ) {
83+ await this . builder . esbuild ( ) ;
84+ }
85+ else {
86+ await this . builder . run ( ) ;
87+ }
88+
89+ await this . reload ( ) ;
90+ } ,
91+ onError : ( err ) => {
92+ this . logger . fail ( "Server start failed!" ) ;
93+ this . logger . error ( err ) ;
94+ } ,
95+ } ,
96+ ) ;
11397 }
11498
11599 async reload ( ) {
0 commit comments