ether-code 0.1.8 → 0.1.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/cli/watcher.js DELETED
@@ -1,106 +0,0 @@
1
- const fs = require('fs')
2
- const path = require('path')
3
- const EventEmitter = require('events')
4
-
5
- class Watcher extends EventEmitter {
6
- constructor(dir, options = {}) {
7
- super()
8
- this.dir = dir
9
- this.options = {
10
- ignored: ['node_modules', '.git', 'dist'],
11
- debounce: 100,
12
- ...options
13
- }
14
- this.watchers = new Map()
15
- this.debounceTimers = new Map()
16
- }
17
-
18
- start() {
19
- this.watchDir(this.dir)
20
- }
21
-
22
- stop() {
23
- for (const watcher of this.watchers.values()) {
24
- watcher.close()
25
- }
26
- this.watchers.clear()
27
-
28
- for (const timer of this.debounceTimers.values()) {
29
- clearTimeout(timer)
30
- }
31
- this.debounceTimers.clear()
32
- }
33
-
34
- watchDir(dir) {
35
- if (this.isIgnored(dir)) return
36
-
37
- try {
38
- const watcher = fs.watch(dir, { persistent: true }, (eventType, filename) => {
39
- if (!filename) return
40
-
41
- const fullPath = path.join(dir, filename)
42
- this.handleEvent(eventType, fullPath)
43
- })
44
-
45
- watcher.on('error', (err) => {
46
- this.emit('error', err)
47
- })
48
-
49
- this.watchers.set(dir, watcher)
50
-
51
- const entries = fs.readdirSync(dir, { withFileTypes: true })
52
- for (const entry of entries) {
53
- if (entry.isDirectory() && !this.isIgnored(entry.name)) {
54
- this.watchDir(path.join(dir, entry.name))
55
- }
56
- }
57
- } catch (err) {
58
- this.emit('error', err)
59
- }
60
- }
61
-
62
- handleEvent(eventType, filePath) {
63
- const existing = this.debounceTimers.get(filePath)
64
- if (existing) {
65
- clearTimeout(existing)
66
- }
67
-
68
- const timer = setTimeout(() => {
69
- this.debounceTimers.delete(filePath)
70
- this.processEvent(filePath)
71
- }, this.options.debounce)
72
-
73
- this.debounceTimers.set(filePath, timer)
74
- }
75
-
76
- processEvent(filePath) {
77
- try {
78
- const exists = fs.existsSync(filePath)
79
-
80
- if (!exists) {
81
- this.emit('unlink', filePath)
82
- return
83
- }
84
-
85
- const stat = fs.statSync(filePath)
86
-
87
- if (stat.isDirectory()) {
88
- if (!this.watchers.has(filePath)) {
89
- this.watchDir(filePath)
90
- this.emit('addDir', filePath)
91
- }
92
- } else {
93
- this.emit('change', filePath)
94
- }
95
- } catch (err) {
96
- this.emit('error', err)
97
- }
98
- }
99
-
100
- isIgnored(name) {
101
- const baseName = path.basename(name)
102
- return this.options.ignored.includes(baseName) || baseName.startsWith('.')
103
- }
104
- }
105
-
106
- module.exports = { Watcher }