60 lines
1.7 KiB
JavaScript
60 lines
1.7 KiB
JavaScript
import p from 'path'
|
|
import split2 from 'split2'
|
|
import { Readable } from 'streamx'
|
|
import { spawn } from 'child_process'
|
|
import { fileURLToPath } from 'url';
|
|
|
|
const __dirname = p.dirname(fileURLToPath(import.meta.url))
|
|
|
|
import { action, STREAM_STATUS, LOG_MESSAGE } from './state.mjs'
|
|
|
|
export class Darkice {
|
|
constructor (opts = {}) {
|
|
this.command = opts.command || 'darkice'
|
|
this.config = opts.config || p.join(__dirname, 'etc', '..', 'darkice.cfg')
|
|
this.logLevel = 5
|
|
this.stream = new Readable()
|
|
this.log = []
|
|
this.stream.on('data', row => this.log.push(row))
|
|
this.id = 'darkice'
|
|
}
|
|
|
|
restart () {
|
|
if (!this.process) return this.start()
|
|
this.process.once('close', () => {
|
|
process.nextTick(() => {
|
|
this.start()
|
|
})
|
|
})
|
|
this.stop()
|
|
}
|
|
|
|
stop () {
|
|
this.process.kill()
|
|
}
|
|
|
|
start () {
|
|
if (this.process) return
|
|
const args = ['-c', this.config, '-v', this.logLevel]
|
|
this.process = spawn(this.command, args)
|
|
this.process.stdout.pipe(split2()).on('data', message => {
|
|
this.stream.push(action(LOG_MESSAGE, { id: this.id, type: 'stdout', message }))
|
|
// this.stream.push({ type: 'stdout', data })
|
|
})
|
|
this.process.stderr.pipe(split2()).on('data', message => {
|
|
this.stream.push(action(LOG_MESSAGE, { id: this.id, type: 'stdout', message }))
|
|
// this.stream.push({ type: 'stderr', data })
|
|
})
|
|
|
|
this.process.on('close', () => {
|
|
this.stream.push(action(STREAM_STATUS, { id: this.id, status: 'stopped' }))
|
|
this.process = null
|
|
})
|
|
|
|
this.stream.push(action(STREAM_STATUS, { id: this.id, status: 'started' }))
|
|
|
|
return new Promise((resolve, reject) => {
|
|
resolve()
|
|
})
|
|
}
|
|
}
|