2022-10-03 17:57:08 +00:00
|
|
|
|
const EventEmitter = require('events')
|
|
|
|
|
const fs = require('fs')
|
|
|
|
|
const path = require('path')
|
2021-03-25 12:40:04 +00:00
|
|
|
|
|
2022-10-03 17:57:08 +00:00
|
|
|
|
module.exports = class Builder extends EventEmitter {
|
2021-03-25 12:40:04 +00:00
|
|
|
|
constructor (semaphoreFile, name = 'builder') {
|
|
|
|
|
super()
|
|
|
|
|
this.name = name
|
2022-10-03 17:57:08 +00:00
|
|
|
|
fs.mkdirSync(path.dirname(semaphoreFile), { recursive: true })
|
2021-03-25 12:40:04 +00:00
|
|
|
|
|
|
|
|
|
this.semaphoreFile = semaphoreFile
|
2022-10-03 17:57:08 +00:00
|
|
|
|
if (!fs.existsSync(this.semaphoreFile)) {
|
|
|
|
|
fs.writeFileSync(this.semaphoreFile, '', { encoding: 'utf8' })
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-25 12:40:04 +00:00
|
|
|
|
this._ready = false
|
|
|
|
|
|
|
|
|
|
this.on('message', (...message) => {
|
|
|
|
|
if (message !== undefined) {
|
|
|
|
|
console.log(`\x1b[33mℹ [${this.name}]`, ...message, '\x1b[0m')
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
this.on('error', (...error) => {
|
|
|
|
|
if (error !== undefined) {
|
|
|
|
|
console.error(`\x1b[31m❗ [${this.name}]`, ...error, '\x1b[0m')
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
2022-10-03 17:57:08 +00:00
|
|
|
|
this.on('ready', (updateSemaphore = true) => {
|
|
|
|
|
const now = Date.now()
|
|
|
|
|
if (updateSemaphore) {
|
|
|
|
|
fs.utimesSync(this.semaphoreFile, now, now)
|
2021-03-25 12:40:04 +00:00
|
|
|
|
}
|
2022-10-03 17:57:08 +00:00
|
|
|
|
|
2021-03-25 12:40:04 +00:00
|
|
|
|
this._ready = true
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
this.on('busy', () => {
|
|
|
|
|
this._ready = false
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ready () {
|
|
|
|
|
return new Promise(resolve => {
|
|
|
|
|
if (this._ready === true) return resolve()
|
|
|
|
|
this.once('ready', () => {
|
|
|
|
|
resolve()
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async start () {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async close () {}
|
|
|
|
|
|
|
|
|
|
clean () {
|
2022-10-03 17:57:08 +00:00
|
|
|
|
fs.rmSync(this.semaphoreFile, { force: true })
|
2021-03-25 12:40:04 +00:00
|
|
|
|
}
|
|
|
|
|
}
|