11import { invariant } from '@zenstackhq/common-helpers' ;
22import { ZModelLanguageMetaData } from '@zenstackhq/language' ;
3- import { isPlugin , isDataModel , type DataModel , LiteralExpr , Plugin , type Model } from '@zenstackhq/language/ast' ;
3+ import { type AbstractDeclaration , isPlugin , LiteralExpr , Plugin , type Model } from '@zenstackhq/language/ast' ;
44import { getLiteral , getLiteralArray } from '@zenstackhq/language/utils' ;
55import { type CliPlugin } from '@zenstackhq/sdk' ;
66import colors from 'colors' ;
@@ -32,26 +32,25 @@ export async function run(options: Options) {
3232 const logsEnabled = ! options . silent ;
3333
3434 if ( logsEnabled ) {
35- console . log ( colors . green ( `\nEnable watch mode!` ) ) ;
35+ console . log ( colors . green ( `\nEnabled watch mode!` ) ) ;
3636 }
3737
3838 const schemaExtensions = ZModelLanguageMetaData . fileExtensions ;
3939
4040 // Get real models file path (cuz its merged into single document -> we need use cst nodes)
41- const getModelAllPaths = ( model : Model ) => new Set (
41+ const getRootModelWatchPaths = ( model : Model ) => new Set < string > (
4242 (
4343 model . declarations . filter (
4444 ( v ) =>
45- isDataModel ( v ) &&
4645 v . $cstNode ?. parent ?. element . $type === 'Model' &&
4746 ! ! v . $cstNode . parent . element . $document ?. uri ?. fsPath ,
48- ) as DataModel [ ]
47+ ) as AbstractDeclaration [ ]
4948 ) . map ( ( v ) => v . $cstNode ! . parent ! . element . $document ! . uri ! . fsPath ) ,
5049 ) ;
5150
5251 const { watch } = await import ( 'chokidar' ) ;
5352
54- const watchedPaths = getModelAllPaths ( model ) ;
53+ const watchedPaths = getRootModelWatchPaths ( model ) ;
5554 let reGenerateSchemaTimeout : ReturnType < typeof setTimeout > | undefined ;
5655
5756 if ( logsEnabled ) {
@@ -77,18 +76,29 @@ export async function run(options: Options) {
7776
7877 try {
7978 const newModel = await pureGenerate ( options , true ) ;
80- const allModelsPaths = getModelAllPaths ( newModel ) ;
79+ const allModelsPaths = getRootModelWatchPaths ( newModel ) ;
8180 const newModelPaths = [ ...allModelsPaths ] . filter ( ( at ) => ! watchedPaths . has ( at ) ) ;
81+ const removeModelPaths = [ ...watchedPaths ] . filter ( ( at ) => ! allModelsPaths . has ( at ) ) ;
8282
8383 if ( newModelPaths . length ) {
8484 if ( logsEnabled ) {
8585 const logPaths = [ ...newModelPaths ] . map ( ( at ) => `- ${ at } ` ) . join ( '\n' ) ;
86- console . log ( `Add file(s) to watch:\n${ logPaths } ` ) ;
86+ console . log ( `Added file(s) to watch:\n${ logPaths } ` ) ;
8787 }
8888
8989 newModelPaths . forEach ( ( at ) => watchedPaths . add ( at ) ) ;
9090 watcher . add ( newModelPaths ) ;
9191 }
92+
93+ if ( removeModelPaths . length ) {
94+ if ( logsEnabled ) {
95+ const logPaths = [ ...removeModelPaths ] . map ( ( at ) => `- ${ at } ` ) . join ( '\n' ) ;
96+ console . log ( `Added file(s) to watch:\n${ logPaths } ` ) ;
97+ }
98+
99+ removeModelPaths . forEach ( ( at ) => watchedPaths . add ( at ) ) ;
100+ watcher . add ( removeModelPaths ) ;
101+ }
92102 } catch ( e ) {
93103 console . error ( e ) ;
94104 }
@@ -97,7 +107,7 @@ export async function run(options: Options) {
97107
98108 watcher . on ( 'unlink' , ( pathAt ) => {
99109 if ( logsEnabled ) {
100- console . log ( `Remove file from watch: ${ pathAt } ` ) ;
110+ console . log ( `Removed file from watch: ${ pathAt } ` ) ;
101111 }
102112
103113 watchedPaths . delete ( pathAt ) ;
0 commit comments