-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
57 lines (52 loc) · 1.56 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
45
46
47
48
49
50
51
52
53
54
55
56
57
const { spawn, exec } = require('child_process')
const { promisify } = require('util')
const {
ERROR_SERVICE_REQUIRED,
ERROR_SERVICE_MUST_BE_STRING,
ERROR_MATCHERS_MUST_NOT_BE_EMPTY,
} = require('./constants')
const execPromise = promisify(exec)
const { onlyOnce, stop } = require('./helpers')
const wfb = (service, matchersMixed) =>
new Promise((resolve, reject) => {
let matchers = Array.isArray(matchersMixed)
? matchersMixed
: [matchersMixed]
matchers = matchers.filter(matcher => !!matcher)
if (typeof service !== 'string') {
reject(new Error(ERROR_SERVICE_MUST_BE_STRING))
return
}
if (matchers.length === 0) {
reject(new Error(ERROR_MATCHERS_MUST_NOT_BE_EMPTY))
return
}
const docker = spawn('docker-compose', ['up', service])
const once = onlyOnce()
docker.stdout.on('data', data => {
const line = data.toString()
matchers.some((match, i) => {
if (
(match instanceof RegExp && match.test(line)) ||
(typeof match === 'string' && line.includes(match))
) {
matchers.splice(i, 1)
return true
}
return false
})
if (matchers.length === 0) {
once(resolve, stop.bind(null, service, docker))
}
})
docker.on('error', once.bind(null, reject))
})
module.exports = (service, matchers) => {
if (service && matchers) {
return wfb(service, matchers)
}
if (service) {
return execPromise(`docker-compose run ${service}`)
}
return Promise.reject(new Error(ERROR_SERVICE_REQUIRED))
}