-
Notifications
You must be signed in to change notification settings - Fork 0
/
dockerRun.js
76 lines (60 loc) · 2.17 KB
/
dockerRun.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
const args = process.argv.slice(2);
const { execSync } = require('child_process');
/**
* @param {Object} options - configuration object
* @param {String} options.service - name of service where execute commands
* @param {Boolean} options.hasDb - if true, enable some db checks
*/
async function main({
service = "node-server",
hasDb = true
}) {
const command = args.shift();
var options = []
for (var i = 0; i < args.length; i++) {
if (args[i].startsWith("-")) {
options.push(args[i]);
} else {
break;
}
}
var cmd = "";
var scArgs = args.slice(i);
var upArgs = "", exitArgs = "true";
if (options.includes("--prod") || options.includes("-p")) {
upArgs = " -f docker-compose.override.yml -f docker-compose.prod.yml ";
}
if (options.includes("--rm") || options.includes("-r")) {
exitArgs = " docker-compose down"
} else if (options.includes("--stop") || options.includes("-s")) {
exitArgs = " docker-compose stop"
}
const dockerUp = "docker-compose up -d " + upArgs + (hasDb ? " && docker-compose exec db /apps/docker/waitForMySQL.sh" : "")
switch (command) {
case "docker:fg":
if (scArgs.length > 0) {
cmd = dockerUp + " && docker-compose exec " + service + " npm run " + scArgs + " || " + exitArgs
} else {
cmd = "docker-compose up " + upArgs + " || " + exitArgs
}
break;
case "docker:bg":
cmd = dockerUp + " && docker-compose exec " + service + " npm run " + scArgs + " || " + exitArgs
break;
case "docker:shell":
cmd = dockerUp + " && docker-compose exec " + service + " bash || " + exitArgs
break;
case "docker:remove":
cmd = "docker-compose down || true"
break;
case "docker:stop":
cmd = "docker-compose stop || true"
break;
default:
cmd = "npm run " + command + " " + args.join(" ") + " || true"
break;
}
console.log("Running: " + cmd)
execSync(cmd, { stdio: 'inherit' });
}
module.exports = main;