This repository has been archived by the owner on Aug 15, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
registerFileLifecycle.js
129 lines (113 loc) · 3.71 KB
/
registerFileLifecycle.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import { dirname, basename } from "path"
import { fileSystemNodeToTypeOrNull } from "./internal/fileSystemNodeToTypeOrNull.js"
import { createWatcher } from "./internal/createWatcher.js"
import { trackRessources } from "./internal/trackRessources.js"
import { assertAndNormalizeFileUrl } from "./assertAndNormalizeFileUrl.js"
import { urlToFileSystemPath } from "./urlToFileSystemPath.js"
export const registerFileLifecycle = (
source,
{ added, updated, removed, notifyExistent = false, keepProcessAlive = true },
) => {
const sourceUrl = assertAndNormalizeFileUrl(source)
if (!undefinedOrFunction(added)) {
throw new TypeError(`added must be a function or undefined, got ${added}`)
}
if (!undefinedOrFunction(updated)) {
throw new TypeError(`updated must be a function or undefined, got ${updated}`)
}
if (!undefinedOrFunction(removed)) {
throw new TypeError(`removed must be a function or undefined, got ${removed}`)
}
const tracker = trackRessources()
const handleFileFound = ({ existent }) => {
const fileMutationStopWatching = watchFileMutation(sourceUrl, {
updated,
removed: () => {
fileMutationStopTracking()
watchFileAdded()
if (removed) {
removed()
}
},
keepProcessAlive,
})
const fileMutationStopTracking = tracker.registerCleanupCallback(fileMutationStopWatching)
if (added) {
if (existent) {
if (notifyExistent) {
added({ existent: true })
}
} else {
added({})
}
}
}
const watchFileAdded = () => {
const fileCreationStopWatching = watchFileCreation(
sourceUrl,
() => {
fileCreationgStopTracking()
handleFileFound({ existent: false })
},
keepProcessAlive,
)
const fileCreationgStopTracking = tracker.registerCleanupCallback(fileCreationStopWatching)
}
const sourceType = fileSystemNodeToTypeOrNull(sourceUrl)
if (sourceType === null) {
if (added) {
watchFileAdded()
} else {
throw new Error(`${urlToFileSystemPath(sourceUrl)} must lead to a file, found nothing`)
}
} else if (sourceType === "file") {
handleFileFound({ existent: true })
} else {
throw new Error(`${urlToFileSystemPath(sourceUrl)} must lead to a file, type found instead`)
}
return tracker.cleanup
}
const undefinedOrFunction = (value) => typeof value === "undefined" || typeof value === "function"
const watchFileCreation = (source, callback, keepProcessAlive) => {
const sourcePath = urlToFileSystemPath(source)
const sourceFilename = basename(sourcePath)
const directoryPath = dirname(sourcePath)
let directoryWatcher = createWatcher(directoryPath, { persistent: keepProcessAlive })
directoryWatcher.on("change", (eventType, filename) => {
if (filename && filename !== sourceFilename) return
const type = fileSystemNodeToTypeOrNull(source)
// ignore if something else with that name gets created
// we are only interested into files
if (type !== "file") return
directoryWatcher.close()
directoryWatcher = undefined
callback()
})
return () => {
if (directoryWatcher) {
directoryWatcher.close()
}
}
}
const watchFileMutation = (sourceUrl, { updated, removed, keepProcessAlive }) => {
let watcher = createWatcher(urlToFileSystemPath(sourceUrl), { persistent: keepProcessAlive })
watcher.on("change", () => {
const sourceType = fileSystemNodeToTypeOrNull(sourceUrl)
if (sourceType === null) {
watcher.close()
watcher = undefined
if (removed) {
removed()
}
} else if (sourceType === "file") {
if (updated) {
updated()
}
}
})
return () => {
if (watcher) {
watcher.close()
}
}
}