@kmamal/watcher 0.0.1
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/LICENSE +19 -0
- package/README.md +6 -0
- package/package.json +25 -0
- package/src/index.js +138 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
Copyright (c) 2023 Konstantin M
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
5
|
+
in the Software without restriction, including without limitation the rights
|
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
8
|
+
furnished to do so, subject to the following conditions:
|
|
9
|
+
|
|
10
|
+
The above copyright notice and this permission notice shall be included in all
|
|
11
|
+
copies or substantial portions of the Software.
|
|
12
|
+
|
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
19
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
# @kmamal/watcher
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@kmamal/watcher)
|
|
4
|
+
[](https://libraries.io/npm/@kmamal%2Fwatcher)
|
|
5
|
+
[](https://bundlephobia.com/package/@kmamal/watcher)
|
|
6
|
+
[](https://opensource.org/licenses/MIT)
|
package/package.json
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": "0.0.1",
|
|
3
|
+
"name": "@kmamal/watcher",
|
|
4
|
+
"description": "watcher",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "git@github.com:kmamal/watcher.git"
|
|
8
|
+
},
|
|
9
|
+
"license": "MIT",
|
|
10
|
+
"main": "./src/index.js",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": "./src/index.js"
|
|
13
|
+
},
|
|
14
|
+
"scripts": {
|
|
15
|
+
"test": "npx @kmamal/testing",
|
|
16
|
+
"update-exports": "node ./scripts/update-exports.mjs"
|
|
17
|
+
},
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"@kmamal/globs": "^0.0.1",
|
|
20
|
+
"@kmamal/util": "^0.1.13"
|
|
21
|
+
},
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"@kmamal/testing": "^0.0.24"
|
|
24
|
+
}
|
|
25
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
const Fs = require('fs')
|
|
2
|
+
const Path = require('path')
|
|
3
|
+
const { EventEmitter } = require('events')
|
|
4
|
+
const { Matcher } = require('@kmamal/globs/matcher')
|
|
5
|
+
const { comm } = require('@kmamal/util/array/comm')
|
|
6
|
+
|
|
7
|
+
class Watcher extends EventEmitter {
|
|
8
|
+
constructor (glob, options) {
|
|
9
|
+
super()
|
|
10
|
+
this._cwd = options?.cwd ? Path.resolve(options.cwd) : process.cwd()
|
|
11
|
+
this._throttling = options?.throttling ?? 100
|
|
12
|
+
|
|
13
|
+
this._matcher = new Matcher(glob, options)
|
|
14
|
+
this._cache = new Map()
|
|
15
|
+
this._scheduled = new Set()
|
|
16
|
+
|
|
17
|
+
const addEntry = async (fsPath, props) => {
|
|
18
|
+
if (this._cache.has(fsPath)) { return }
|
|
19
|
+
const stats = props?.stats ?? await Fs.promises.stats()
|
|
20
|
+
const isDir = props?.isDir ?? stats.isDirectory()
|
|
21
|
+
const path = props?.path ?? (isDir ? `${fsPath}/` : fsPath)
|
|
22
|
+
|
|
23
|
+
const watcher = Fs.watch(fsPath, (event, relPath) => {
|
|
24
|
+
handleChange(isDir ? Path.join(fsPath, relPath) : fsPath)
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
let contents
|
|
28
|
+
if (isDir) {
|
|
29
|
+
contents = await Fs.promises.readdir(fsPath)
|
|
30
|
+
contents.sort()
|
|
31
|
+
}
|
|
32
|
+
this._cache.set(fsPath, {
|
|
33
|
+
watcher,
|
|
34
|
+
ino: stats.ino,
|
|
35
|
+
mtimeMs: stats.mtimeMs,
|
|
36
|
+
contents,
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
const type = isDir ? 'addDir' : 'addFile'
|
|
40
|
+
this.emit('change', type, path, stats)
|
|
41
|
+
|
|
42
|
+
if (isDir && !props?.exhaustive) {
|
|
43
|
+
const fileIterator = this._matcher.getFiles({
|
|
44
|
+
cwd: fsPath,
|
|
45
|
+
includeDirs: true,
|
|
46
|
+
})
|
|
47
|
+
for await (const entry of fileIterator) {
|
|
48
|
+
const { path: childPath, stats: childStats } = entry
|
|
49
|
+
const childIsDir = childStats.isDirectory()
|
|
50
|
+
const childFsPath = childIsDir ? childPath.slice(0, -1) : childPath
|
|
51
|
+
await addEntry(childFsPath, {
|
|
52
|
+
stats: childStats,
|
|
53
|
+
path: childPath,
|
|
54
|
+
isDir: childIsDir,
|
|
55
|
+
exhaustive: true,
|
|
56
|
+
})
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const removeEntry = (fsPath, props) => {
|
|
62
|
+
const entry = props?.entry ?? this._cache.get(fsPath)
|
|
63
|
+
if (!entry) { return }
|
|
64
|
+
const isDir = props?.isDir ?? Boolean(entry.contents)
|
|
65
|
+
const path = props?.isDir ?? (isDir ? `${fsPath}/` : fsPath)
|
|
66
|
+
|
|
67
|
+
entry.watcher.close()
|
|
68
|
+
this._cache.delete(fsPath)
|
|
69
|
+
|
|
70
|
+
if (isDir && !props?.exhaustive) {
|
|
71
|
+
for (const name of entry.contents) {
|
|
72
|
+
removeEntry(Path.join(fsPath, name))
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const type = isDir ? 'delDir' : 'delFile'
|
|
77
|
+
this.emit('change', type, path)
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const handleChange = (fsPath) => {
|
|
81
|
+
if (this._scheduled.has(fsPath)) { return }
|
|
82
|
+
this._scheduled.add(fsPath)
|
|
83
|
+
setTimeout(() => {
|
|
84
|
+
this._scheduled.delete(fsPath)
|
|
85
|
+
_handleChange(fsPath)
|
|
86
|
+
}, this._throttling)
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const _handleChange = async (fsPath) => {
|
|
90
|
+
let entry = this._cache.get(fsPath)
|
|
91
|
+
|
|
92
|
+
try {
|
|
93
|
+
const newStats = await Fs.promises.stat(fsPath)
|
|
94
|
+
const isDir = newStats.isDirectory()
|
|
95
|
+
const newPath = isDir ? `${fsPath}/` : fsPath
|
|
96
|
+
|
|
97
|
+
const wasDir = Boolean(entry?.contents)
|
|
98
|
+
const oldPath = wasDir ? `${fsPath}/` : fsPath
|
|
99
|
+
|
|
100
|
+
if (!entry || wasDir !== isDir || entry.ino !== newStats.ino) {
|
|
101
|
+
if (entry) { removeEntry(fsPath, { entry, path: oldPath, isDir: wasDir }) }
|
|
102
|
+
entry = addEntry(fsPath, { stats: newStats, path: newPath, isDir })
|
|
103
|
+
return
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (entry.mtimeMs >= newStats.mtimeMs) { return }
|
|
107
|
+
|
|
108
|
+
if (!isDir) {
|
|
109
|
+
this.emit('change', 'change', newPath, newStats)
|
|
110
|
+
} else {
|
|
111
|
+
const newContents = await Fs.promises.readdir(fsPath)
|
|
112
|
+
newContents.sort()
|
|
113
|
+
|
|
114
|
+
const { a, b } = comm(entry.contents, newContents)
|
|
115
|
+
for (const name of a) {
|
|
116
|
+
removeEntry(Path.join(fsPath, name))
|
|
117
|
+
}
|
|
118
|
+
for (const name of b) {
|
|
119
|
+
addEntry(Path.join(fsPath, name))
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
} catch (error) {
|
|
123
|
+
if (entry) { removeEntry(fsPath, { entry }) }
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
;(async () => {
|
|
128
|
+
const fileIterator = this._matcher.getFiles({ includeDirs: true })
|
|
129
|
+
for await (const { path, stats } of fileIterator) {
|
|
130
|
+
const isDir = stats.isDirectory()
|
|
131
|
+
const fsPath = isDir ? path.slice(0, -1) : path
|
|
132
|
+
await addEntry(fsPath, { stats, path, isDir, exhaustive: true })
|
|
133
|
+
}
|
|
134
|
+
})()
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
module.exports = { Watcher }
|