forked from Rishi-Bidani/CloudSync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
51 lines (45 loc) · 1.68 KB
/
main.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
const fs = require("fs");
const chokidar = require("chokidar");
const path = require("path");
const files = require("./js/fsWrap");
const upload = require("./js/upload");
const FSWrapper = require("./js/fsWrap");
const WATCHED = path.join(__dirname, "test");
const UNWATCHED = ["./node_modules/*", "./ignored/*"];
// Initialize watcher.
const watcher = chokidar.watch(path.resolve(WATCHED), {
ignored: /(^|[\/\\])\../, // ignore dotfiles
persistent: true,
});
function main() {
watcher
.unwatch(UNWATCHED)
.on("add", async (DocPath) => {
const size = await FSWrapper.fileSize(DocPath);
// Check file size and uplaod only if it is above 1kb
if (parseInt(size.split("-")[0]) >= 1) {
console.log("File", DocPath, "has been added");
if (await files.findFileExists(DocPath)) {
const form = await upload.createFormData(DocPath, DocPath.split(__dirname)[1]);
await upload.uploadfile(form);
}
}
})
.on("change", async (DocPath) => {
if (await files.findFileExists(DocPath)) {
const form = await upload.createFormData(DocPath, DocPath.split(__dirname)[1]);
await upload.uploadfile(form);
// console.log(form);
} else {
console.log("error");
}
console.log("File", DocPath, "has been changed");
})
.on("unlink", function (DocPath) {
console.log("File", DocPath, "has been removed");
})
.on("error", function (error) {
console.error("Error happened", error);
});
}
main();