-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
44 lines (35 loc) · 1.39 KB
/
index.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
const path = require('path')
, fs = require('fs-extra')
, {Gaze} = require('gaze')
, watch = (directory, eventHandler) => (new Gaze(directory + '/**')).on('all', eventHandler)
, getToStaticDirectory = require('refo-directory-to-other-directory')
var fileSources = {}
module.exports.watchedFileSource = {}
module.exports.watchedFileSource.init = filePath => fileSources[filePath] = []
module.exports.watchedFileSource.add = (filePath, sourceFilePath) => {
if (!fileSources[filePath].includes(sourceFilePath))
fileSources[filePath].push(sourceFilePath)
}
module.exports.watch = (handlers, assetDirectory, siteDirectory, staticDirectory) => {
String.prototype.toStaticDirectory = getToStaticDirectory(siteDirectory, staticDirectory)
const getHandler = filePath => handlers[path.extname(filePath)]
, handleSourceChange = filePath =>
Object.entries(fileSources).forEach(source => {
if (source[1].includes(filePath))
getHandler(source[0])(source[0])
})
watch(assetDirectory, (event, filePath) => handleSourceChange(filePath))
watch(siteDirectory, (event, filePath) => {
switch (event) {
case 'added': case 'changed': case 'renamed':
const handle = getHandler(filePath)
if (handle)
handle(filePath)
else
fs.copy(filePath, filePath.toStaticDirectory())
handleSourceChange(filePath)
break;
case 'deleted': fs.unlink(filePath.toStaticDirectory()); break;
}
})
}