Skip to content

Commit 2ab4dbb

Browse files
Semih KEŞKEKSemih KEŞKEK
authored andcommitted
Complete Watch Progress
1 parent c70a755 commit 2ab4dbb

File tree

4 files changed

+123
-13
lines changed

4 files changed

+123
-13
lines changed

App.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import {Directory} from './App/Configuration';
2-
import {SitesWatcher} from './App/DirectoryListener';
2+
import {WatchManager} from './App/Listener';
33

4-
var siteW = new SitesWatcher(Directory.SitePath)
4+
5+
6+
var nn = new WatchManager(Directory.SitePath);

App/DirectoryListener.ts

Lines changed: 0 additions & 11 deletions
This file was deleted.

App/Exceptions.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
var WatcherExceptions = {
2+
InvalidPath: "Error Code: WE001IP \n"
3+
+ "Path must be a string."
4+
}
5+
6+
export {
7+
WatcherExceptions
8+
}

App/Listener.ts

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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

Comments
 (0)