-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
103 lines (95 loc) · 2.65 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
#!/usr/bin/env node
const { spawn } = require('child_process')
const chalk = require('chalk')
const onExit = require('death')
const kill = require('tree-kill')
const IOSplit = require('iosplit')
const apps = require(process.argv[2])
/*
* Handle input commands and split the screen
*/
const iosplit = new IOSplit()
iosplit.on('line', line => {
try {
if (line.startsWith('kill ')) {
const name = line.replace('kill ', '')
console.log(chalk.yellow('Sending kill to', name, ' ...'))
kill(childs[name].pid)
} else if (line.startsWith('start ')) {
const name = line.replace('start ', '')
run(apps.find(app => app.name === name))
} else if (line.startsWith('restart ')) {
const name = line.replace('restart ', '')
console.log(chalk.yellow('Sending kill to', name, ' ...'))
kill(childs[name].pid)
const interval = setInterval(() => {
if (!(name in childs)) {
run(apps.find(app => app.name === name))
clearInterval(interval)
}
}, 100)
}
} catch (err) {
console.error(err)
}
})
iosplit.start()
/*
* Run the apps
*/
const childs = {}
const run = app => {
const count = 'count' in app ? app.count : 1
const output = 'output' in app ? app.output : true
for (let i = 0; i < count; i++) {
childs[app.name] = app.child = spawn(app.command, app.args, app.options)
console.log(
chalk.green(app.name) +
' - Spawning "' + app.command + ' ' +
app.args.join(' ') + '" ' +
(!output ? 'IN SILENT MODE ' : ' ') +
'...'
)
if (output) {
childs[app.name].stdout.on('data', data => {
data.toString().split(/\r?\n/).forEach(line => {
line = line.replace(/\s+$/, '')
if (line !== '') console.log(chalk.blue(app.name) + ' - ' + line)
})
})
childs[app.name].stderr.on('data', data => {
data.toString().split(/\r?\n/).forEach(line => {
line = line.replace(/~+$/, '')
if (line !== '') console.log(chalk.red(app.name) + ' - ' + line)
})
})
}
childs[app.name].on('close', code => {
console.log(chalk.red(app.name) + ' - CLOSED with code ' + code)
delete childs[app.name]
})
}
}
apps.forEach(app => {
if ('every' in app) {
setInterval(() => {
run(app)
}, app.every * 1000)
}
run(app)
})
/*
* Register exit process cleanups
*/
onExit(() => {
setInterval(() => {
const childCount = Object.entries(childs).length
if (childCount === 0) {
process.exit()
}
}, 100)
for (const [name, child] of Object.entries(childs)) {
console.log(chalk.yellow('Sending kill to', name, ' ...'))
kill(child.pid)
}
})