-
Notifications
You must be signed in to change notification settings - Fork 10
/
watcher.js
269 lines (242 loc) · 7.81 KB
/
watcher.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
const path = require("path");
const fs = require("fs-extra");
const chokidar = require("chokidar");
const find_ = require("lodash/find");
const reject_ = require("lodash/reject");
const logger = require("corvid-local-logger");
const { isUnderPath, isSamePath } = require("./utils/fileUtils");
const { isPathRelatedToSite } = require("./sitePaths");
const { getMessage } = require("./messages");
const ensureWriteFile = async (path, content) => {
await fs.ensureFile(path);
await fs.writeFile(path, content);
};
const toPosixPath = winPath => winPath.replace(/\\/g, "/");
const watch = async givenPath => {
let ignoreBefore = 0;
let ignoreAll = false;
logger.verbose(getMessage("Watcher_Start_Log", { path: givenPath }));
const rootPath = fs.realpathSync(givenPath);
if (rootPath !== givenPath) {
logger.debug(getMessage("Watcher_Path_Resolved_Log", { path: rootPath }));
}
const fullPath = relativePath => path.join(rootPath, relativePath);
const isUnderRoot = pathToCheck =>
isUnderPath(rootPath, fullPath(pathToCheck));
const shouldIgnoreFile = watchPath => {
const isInterestingPath =
isSamePath(rootPath, watchPath) ||
(isUnderRoot(watchPath) && isPathRelatedToSite(rootPath, watchPath));
return !isInterestingPath;
};
const assertUnderRoot = pathToCheck => {
if (!isUnderRoot(pathToCheck)) {
throw new Error(getMessage("Watcher_Not_Under_Root_Error"));
}
};
const watcher = chokidar.watch(rootPath, {
ignored: shouldIgnoreFile,
persistent: true,
ignoreInitial: true,
cwd: rootPath,
awaitWriteFinish: {
stabilityThreshold: 500
},
followSymlinks: false,
disableGlobbing: true,
alwaysStat: true
});
await new Promise((resolve, reject) => {
watcher.on("ready", () => resolve());
watcher.on("error", () => reject());
});
let actionsToIgnore = [];
const ignoreAction = (type, path) => {
actionsToIgnore.push({ type, path });
};
const removeFromIgnoredActions = (type, path) => {
actionsToIgnore = reject_(actionsToIgnore, { type, path });
};
const isIgnoredAction = (type, path, mtimeMs = Date.now()) =>
ignoreAll ||
mtimeMs < ignoreBefore ||
!!find_(actionsToIgnore, { type, path });
return {
close: () => watcher.close(),
onAdd: callback => {
watcher.on("add", async (relativePath, stats) => {
const posixRelativePath = toPosixPath(relativePath);
if (!isIgnoredAction("write", posixRelativePath, stats.mtimeMs)) {
logger.debug(
getMessage("Watcher_Add_Reporting_Log", { path: posixRelativePath })
);
callback(
posixRelativePath,
await fs.readFile(fullPath(posixRelativePath), "utf8")
);
} else {
logger.debug(
getMessage("Watcher_Add_Ignoring_Log", { path: posixRelativePath })
);
removeFromIgnoredActions("write", posixRelativePath);
}
});
},
onChange: callback => {
watcher.on("change", async (relativePath, stats) => {
const posixRelativePath = toPosixPath(relativePath);
if (!isIgnoredAction("write", posixRelativePath, stats.mtimeMs)) {
logger.debug(
getMessage("Watcher_Change_Reporting_Log", {
path: posixRelativePath
})
);
callback(
posixRelativePath,
await fs.readFile(fullPath(posixRelativePath), "utf8")
);
} else {
logger.debug(
getMessage("Watcher_Change_Ignoring_Log", {
path: posixRelativePath
})
);
removeFromIgnoredActions("write", posixRelativePath);
}
});
},
onDelete: callback => {
watcher.on("unlink", async relativePath => {
const posixRelativePath = toPosixPath(relativePath);
if (!isIgnoredAction("delete", posixRelativePath)) {
logger.debug(
getMessage("Watcher_Delete_Reporting_Log", {
path: posixRelativePath
})
);
callback(posixRelativePath);
} else {
logger.debug(
getMessage("Watcher_Delete_Ignoring_Log", {
path: posixRelativePath
})
);
removeFromIgnoredActions("delete", posixRelativePath);
}
});
},
ignoredWriteFile: async (relativePath, content) => {
logger.silly(
getMessage("Watcher_Ignored_Write_Log", { path: relativePath })
);
try {
ignoreAction("write", relativePath);
assertUnderRoot(relativePath);
await ensureWriteFile(fullPath(relativePath), content);
} catch (err) {
logger.info(
`Failed writing file to [${relativePath}] failed - ${err.message}`
);
logger.error(err);
removeFromIgnoredActions("write", relativePath);
throw err;
}
},
ignoredEnsureFile: async relativePath => {
logger.silly(
getMessage("Watcher_Ignored_Ensure_Log", { path: relativePath })
);
const fullPathFile = fullPath(relativePath);
if (await fs.exists(fullPathFile)) return;
try {
ignoreAction("write", relativePath);
assertUnderRoot(relativePath);
await fs.ensureFile(fullPathFile);
} catch (err) {
logger.info(
`Failed ensuring file at [${relativePath}] failed - ${err.message}`
);
logger.error(err);
removeFromIgnoredActions("write", relativePath);
throw err;
}
},
ignoredDeleteFile: async relativePath => {
logger.silly(
getMessage("Watcher_Ignored_Delete_Log", { path: relativePath })
);
try {
ignoreAction("delete", relativePath);
assertUnderRoot(relativePath);
await fs.remove(fullPath(relativePath));
} catch (err) {
logger.info(
`Failed deleting file at [${relativePath}] failed - ${err.message}`
);
logger.error(err);
removeFromIgnoredActions("delete", relativePath);
throw err;
}
},
ignoredCopyFile: async (relativeSourcePath, relativeTargetPath) => {
logger.silly(
getMessage("Watcher_Ignored_Copy_Log", {
sourcePath: relativeSourcePath,
targetPath: relativeTargetPath
})
);
try {
ignoreAction("write", relativeTargetPath);
assertUnderRoot(relativeSourcePath);
assertUnderRoot(relativeTargetPath);
await fs.copy(
fullPath(relativeSourcePath),
fullPath(relativeTargetPath)
);
} catch (err) {
logger.info(
`Failed copying file from [${relativeSourcePath}] to [${relativeTargetPath}] failed - ${
err.message
}`
);
logger.error(err);
removeFromIgnoredActions("write", relativeTargetPath);
throw err;
}
},
ignoredMoveFile: async (relativeSourcePath, relativeTargetPath) => {
logger.silly(
getMessage("Watcher_Ignored_Move_Log", {
sourcePath: relativeSourcePath,
targetPath: relativeTargetPath
})
);
try {
ignoreAction("delete", relativeSourcePath);
ignoreAction("write", relativeTargetPath);
await fs.move(
fullPath(relativeSourcePath),
fullPath(relativeTargetPath)
);
} catch (err) {
logger.info(
`Failed moving file from [${relativeSourcePath}] to [${relativeTargetPath}] failed - ${
err.message
}`
);
logger.error(err);
removeFromIgnoredActions("delete", relativeSourcePath);
removeFromIgnoredActions("write", relativeTargetPath);
throw err;
}
},
pause: () => {
ignoreAll = true;
},
resume: () => {
ignoreAll = false;
ignoreBefore = Date.now();
}
};
};
module.exports = watch;