This repository was archived by the owner on Aug 20, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocker-watcher.js
More file actions
72 lines (58 loc) · 1.46 KB
/
docker-watcher.js
File metadata and controls
72 lines (58 loc) · 1.46 KB
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
const path = require("path");
const exec = require("child_process").exec;
const chokidar = require("chokidar");
const debounce = require("debounce");
const config = require("./config.json");
const projects = Object.assign(
{},
config.PROJECTS.github,
config.PROJECTS.travis
);
const projectNames = Object.keys(projects).filter(projectName => {
return projects[projectName].type === "docker-compose";
});
const watching = projectNames.map(name => {
return path.resolve(projects[name].path);
});
watching.forEach(path => {
console.log("Watching " + path);
});
const watcher = chokidar.watch(watching, {
ignored: /(^|[\/\\])\../,
persistent: true
});
const onFileChange = (path, stats) => {
const project =
projects[
projectNames.filter(name => {
return path.startsWith(projects[name].path);
})[0]
];
const dockerBase =
"cd " +
project.path +
" && docker-compose" +
project.compose_files.reduce((a, b) => {
return a + " -f " + b;
}, "");
exec(dockerBase + " pull", (err, stdout, stderr) => {
if (err) {
return console.log(err);
}
exec(
dockerBase + " up -d " + project.service_name,
(err, stdout, stderr) => {
if (err) {
return console.log(err);
}
console.log(stdout);
console.log(stderr);
}
);
});
};
const debouncedOnFileChange = debounce(onFileChange, 1000 * 10);
watcher.on("change", (path, stats) => {
console.log(path + "changed, debouncing for 10s..");
debouncedOnFileChange(path, stats);
});