| 1 |
"use strict"; |
| 2 |
|
| 3 |
var promisify = require("deferred").promisify |
| 4 |
, ee = require("event-emitter") |
| 5 |
, fs = require("fs") |
| 6 |
, typeByStats = require("../type-by-stats") |
| 7 |
, lstatSync = fs.lstatSync |
| 8 |
, readdir = promisify(fs.readdir) |
| 9 |
, watchFile = fs.watchFile |
| 10 |
, unwatchFile = fs.unwatchFile |
| 11 |
, other |
| 12 |
, directory |
| 13 |
, opts = { persistent: false, interval: 1500 }; |
| 14 |
|
| 15 |
other = function (path, emitter, stats) { |
| 16 |
var end, close, type, listener; |
| 17 |
|
| 18 |
end = function (err) { |
| 19 |
if (emitter) { |
| 20 |
emitter.emit("end", err); |
| 21 |
close(); |
| 22 |
} |
| 23 |
}; |
| 24 |
|
| 25 |
close = function () { |
| 26 |
unwatchFile(path, listener); |
| 27 |
emitter = null; |
| 28 |
}; |
| 29 |
|
| 30 |
emitter.end = end; |
| 31 |
emitter.close = close; |
| 32 |
type = typeByStats(stats); |
| 33 |
watchFile( |
| 34 |
path, |
| 35 |
opts, |
| 36 |
(listener = function (nstats) { |
| 37 |
var nType, err; |
| 38 |
if (!emitter) return; |
| 39 |
if (!nstats.ctime.getTime() && !nstats.mode) { |
| 40 |
// It means that file doesn't exist enymore |
| 41 |
err = new Error("File doesn't exist"); |
| 42 |
err.code = "ENOENT"; |
| 43 |
end(err); |
| 44 |
return; |
| 45 |
} |
| 46 |
nType = typeByStats(nstats); |
| 47 |
if (type !== nType) { |
| 48 |
err = new Error("File type have changed"); |
| 49 |
err.code = "DIFFTYPE"; |
| 50 |
end(err); |
| 51 |
return; |
| 52 |
} |
| 53 |
if ( |
| 54 |
stats.ctime.valueOf() !== nstats.ctime.valueOf() || |
| 55 |
(stats.mtime.valueOf() !== nstats.mtime.valueOf() || stats.size !== nstats.size) |
| 56 |
) { |
| 57 |
emitter.emit("change"); |
| 58 |
} |
| 59 |
stats = nstats; |
| 60 |
}) |
| 61 |
); |
| 62 |
}; |
| 63 |
|
| 64 |
directory = function (path, emitter) { |
| 65 |
var end, close, data, compare, listener; |
| 66 |
|
| 67 |
end = function (err) { |
| 68 |
if (emitter) { |
| 69 |
emitter.emit("end", err); |
| 70 |
close(); |
| 71 |
} |
| 72 |
}; |
| 73 |
|
| 74 |
close = function () { |
| 75 |
if (emitter) { |
| 76 |
if (listener) unwatchFile(path, listener); |
| 77 |
emitter = null; |
| 78 |
} |
| 79 |
}; |
| 80 |
|
| 81 |
compare = function (file, index) { return data[index] === file; }; |
| 82 |
|
| 83 |
emitter.end = end; |
| 84 |
emitter.close = close; |
| 85 |
readdir(path).done(function (files) { |
| 86 |
data = files.sort(); |
| 87 |
watchFile( |
| 88 |
path, |
| 89 |
opts, |
| 90 |
(listener = function (stats) { |
| 91 |
var err; |
| 92 |
if (!emitter) return; |
| 93 |
if (!stats.ctime.getTime() && !stats.mode) { |
| 94 |
// It means that dir doesn't exist enymore |
| 95 |
err = new Error("Directory doesn't exist"); |
| 96 |
err.code = "ENOENT"; |
| 97 |
end(err); |
| 98 |
return; |
| 99 |
} |
| 100 |
readdir(path).done(function (nuFiles) { |
| 101 |
if (!emitter) return; |
| 102 |
if (nuFiles.length !== data.length || !nuFiles.sort().every(compare)) { |
| 103 |
data = nuFiles; |
| 104 |
emitter.emit("change"); |
| 105 |
} |
| 106 |
}, end); |
| 107 |
}) |
| 108 |
); |
| 109 |
}, end); |
| 110 |
}; |
| 111 |
|
| 112 |
module.exports = function (path, emitter) { |
| 113 |
var stats = lstatSync(path); |
| 114 |
if (!emitter) emitter = ee(); |
| 115 |
if (stats.isDirectory()) directory(path, emitter); |
| 116 |
else other(path, emitter, stats); |
| 117 |
return emitter; |
| 118 |
}; |