1+ /// <reference path="../typings/index.d.ts" />
2+ import * as File from 'fs' ;
3+ import * as Chokidar from 'chokidar' ;
4+ import * as Path from 'path' ;
5+ import { WatcherExceptions } from './Exceptions' ;
6+
7+
8+ export class WatchManager {
9+ mainPath : string ;
10+ WatchList : Array < ISiteWatcher > ;
11+ Watcher : Chokidar . IFSWatcher ;
12+ constructor ( path ) {
13+ this . mainPath = path ;
14+ this . WatchList = [ ] ;
15+
16+ this . RootWatcher ( ) ;
17+ }
18+ RootWatcher ( ) {
19+ this . Watcher = Chokidar . watch ( this . mainPath )
20+ this . Watcher . on ( 'addDir' , ( path : string , details : IChokidarDetails ) => {
21+ let paths = path . split ( '\\' ) ;
22+ if ( paths . length == 2 )
23+ this . WatchList [ this . WatchList . length ] = {
24+ SiteName : path [ 1 ] ,
25+ isSubDirctory : ( paths . length > 2 ? true : false ) ,
26+ Path : path ,
27+ Watcher : new Watch ( path ) ,
28+ subDirectoryName : ''
29+ }
30+ if ( paths . length > 2 && false )
31+ this . WatchList [ this . WatchList . length - 1 ] . subDirectoryName = paths [ 2 ] ;
32+ } ) ;
33+ }
34+ }
35+
36+
37+ class Watch {
38+ Path : string ;
39+ Watcher : Chokidar . IFSWatcher ;
40+ Watching : boolean ;
41+ Exception : IWatcherException ;
42+ Event : any ;
43+ constructor ( path : string = '' , start :boolean = true ) {
44+ this . Exception = {
45+ Status : 'FAILD'
46+ } ;
47+ if ( ! path ) {
48+ this . Exception . Message = WatcherExceptions . InvalidPath ;
49+ throw this . Exception ;
50+ }
51+ this . Path = path ;
52+ if ( start )
53+ this . startWatch ( ) ;
54+ }
55+ startWatch ( ) {
56+ let ths = this ;
57+ this . Watcher = Chokidar . watch ( this . Path , { } ) ;
58+ this . Watcher . on ( 'raw' , ( event , path , details ) => {
59+ if ( this . Event )
60+ clearTimeout ( this . Event ) ;
61+ this . Event = setTimeout ( function ( ) {
62+ console . log ( event ) ;
63+ console . log ( path ) ;
64+ console . log ( details ) ;
65+ } , 50 ) ;
66+
67+ } ) ;
68+ this . Watching = true ;
69+ }
70+ stopWatch ( ) {
71+ this . Watcher . unwatch ( this . Path ) ;
72+ this . Watching = false ;
73+ }
74+ }
75+
76+ ///############################################################### Interfaces
77+
78+ interface ISiteWatcher {
79+ SiteName : string ;
80+ Path : string ;
81+ Watcher : Watch ;
82+ isSubDirctory : boolean ;
83+ subDirectoryName : string ;
84+ }
85+ interface ISubDirectoryWatcher {
86+ Path : string ;
87+ Watcher : Watch ;
88+ }
89+
90+ type WatcherExceptionStatus = 'OK' | 'FAILD' ;
91+ interface IWatcherException {
92+ Status : WatcherExceptionStatus ;
93+ Message ?: string ;
94+ }
95+
96+ interface IChokidarDetails {
97+ dev : number ;
98+ mode : number ;
99+ nlink : number ;
100+ uid : number ;
101+ gid : number ;
102+ rdev : number ;
103+ blksize : any ;
104+ ino : number ;
105+ size : number ;
106+ blocks : any ;
107+ atime : Date ;
108+ mtime : Date ;
109+ ctime : Date ;
110+ birthtime : Date ;
111+ }
0 commit comments