11import { exec , ChildProcess } from "child_process" ;
22import { createServer , ViteDevServer } from "vite" ;
33import { isTs , mkdir } from "./utils" ;
4+ import chokidar from "chokidar" ;
45import fs from "fs" ;
56import { readConfig , UserConfig } from "./config" ;
67import { buildTs , installAssets } from "./build" ;
78
89const cwd = process . cwd ( ) ;
9- const watchers : fs . FSWatcher [ ] = [ ] ;
10+ const watchers : chokidar . FSWatcher [ ] = [ ] ;
1011
1112let printLine = 0 ;
1213
@@ -58,36 +59,44 @@ export async function runApp(electronArgs: string[]): Promise<void> {
5859 const electron = runElectron ( electronArgs ) ;
5960
6061 const restartVite = async ( ) => {
62+ console . info ( "Restart vite dev server" ) ;
6163 await viteServer ?. restart ( ) ;
6264 } ;
6365
6466 const closeElectron = async ( ) => {
65- await viteServer ?. close ( ) ;
66- unwatchAll ( ) ;
67-
68- electron . removeAllListeners ( ) ;
67+ console . info ( "Close electron process" ) ;
68+ if ( viteServer . httpServer ?. listening ) {
69+ await viteServer ?. close ( ) ;
70+ }
71+ await unwatchAll ( ) ;
6972 electron . kill ( ) ;
7073 } ;
7174
72- electron . on ( "close" , closeElectron ) ;
73-
7475 const restartElectron = async ( ) => {
76+ console . info ( "Restart electron process" ) ;
77+ electron . once ( "close" , ( ) => {
78+ runApp ( electronArgs ) ;
79+ } ) ;
7580 await closeElectron ( ) ;
76- await runApp ( electronArgs ) ;
7781 } ;
7882
83+ electron . on ( "exit" , ( ) => {
84+ closeElectron ( ) ;
85+ } ) ;
86+
7987 watch ( "src" , restartElectron ) ;
8088 watch ( "voxer.config.js" , restartElectron ) ;
8189 watch ( "voxer.config.ts" , restartElectron ) ;
8290 watch ( "package.json" , restartElectron ) ;
8391 watch ( "tsconfig.json" , restartElectron ) ;
84- watch ( "view" , restartVite ) ;
8592}
8693
87- export function watch ( path : string , callback : ( ) => void | Promise < void > ) : fs . FSWatcher | null {
94+ export function watch ( path : string , callback : ( ... args : any [ ] ) => void ) : fs . FSWatcher | null {
8895 path = cwd + "/" + path ;
96+
8997 if ( fs . existsSync ( path ) ) {
90- const watcher = fs . watch ( path , callback ) ;
98+ const watcher = chokidar . watch ( path ) ;
99+ watcher . on ( "change" , callback ) ;
91100 watchers . push ( watcher ) ;
92101
93102 return watcher ;
@@ -96,8 +105,10 @@ export function watch(path: string, callback: () => void | Promise<void>): fs.FS
96105 return null ;
97106}
98107
99- export function unwatchAll ( ) {
108+ export async function unwatchAll ( ) {
100109 for ( const watcher of watchers ) {
101- watcher ?. close ( ) ;
110+ await watcher ?. close ( ) ;
102111 }
112+
113+ watchers . length = 0 ;
103114}
0 commit comments