-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
122 lines (99 loc) · 2.64 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
const cp = require('child_process')
const fs = require('fs')
const rimraf = require('rimraf')
const merge = require('@alexlafroscia/yaml-merge')
const homedir = require('os').homedir()
const path = require('path')
// Following code extracted from https://github.com/chalk/ansi-styles/blob/master/index.js
const colors = {
magenta: [35, 39],
red: [31, 39],
green: [32, 39],
yellow: [33, 39]
}
function colorize(colorName, ...args) {
const color = colors[colorName]
return `\u001B[${color[0]}m${args.join(' ')}\u001B[${color[1]}m`
}
const arrow = colorize('magenta', '>>')
// Log helpers
function info(...args) {
console.log(colorize('green', 'info'), arrow, ...args)
}
function warn(...args) {
console.log(colorize('yellow', 'warn'), arrow, ...args)
}
function error(...args) {
console.error(colorize('red', 'error'), arrow, ...args)
}
function showHelp() {
info('Usage: docker-blend [stack|compose] [...arguments]')
}
function generateConfig() {
// NODE_ENV verification
const { NODE_ENV } = process.env
if (!NODE_ENV) {
error('Please set your NODE_ENV environment')
process.exit(1)
}
// Apped configuration override if present
const tempFile = `.docker-compose.blend.yml`
process.on('exit', () => rimraf.sync(tempFile))
const override = `docker-compose/${NODE_ENV}.yml`
try {
fs.accessSync(override)
info('configuration', override, 'found, loading')
fs.writeFileSync(tempFile, merge('docker-compose.yml', override))
} catch (error) {
warn('configuration', override, 'does not exist, skipping')
fs.copyFileSync('docker-compose.yml', tempFile)
}
return tempFile
}
function getCommand(argv) {
const name = argv[2]
const args = argv.slice(3)
const configFile = generateConfig()
switch (name) {
case 'compose':
return {
command: 'docker-compose',
args: [
'-f',
configFile
].concat(args)
}
break
case 'stack':
return {
command: 'docker',
args: args.concat([
'stack',
`--compose-file=${tempFile}`
])
}
break
default:
showHelp()
process.exit(1)
}
}
// Command and base arguments
const { command, args } = getCommand(process.argv)
// Run process and pipe it
info('running ' + command)
const proc = cp.spawn(command, args, {
stdio: [process.stdin, process.stdout, process.stderr]
})
proc.on('exit', function (code) {
info(command + ' exited with code:', code)
if (code === 2) {
code = 0 // Ignore SIGINT
}
process.exit(code)
})
function onSignal(signal) {
process.on(signal, () => proc.kill(signal))
}
onSignal('SIGTERM')
onSignal('SIGINT')