2022-08-01 17:10:00 +00:00
|
|
|
|
import EventEmitter from 'events'
|
|
|
|
|
import { mkdirSync, writeFileSync, rmSync } from 'fs'
|
|
|
|
|
import { dirname } from 'path'
|
2021-03-25 12:40:04 +00:00
|
|
|
|
|
2022-08-01 17:10:00 +00:00
|
|
|
|
export default class Builder extends EventEmitter {
|
2021-03-25 12:40:04 +00:00
|
|
|
|
constructor (semaphoreFile, name = 'builder') {
|
|
|
|
|
super()
|
|
|
|
|
this.name = name
|
|
|
|
|
this.firstBuild = true
|
2022-08-01 17:10:00 +00:00
|
|
|
|
mkdirSync(dirname(semaphoreFile), { recursive: true })
|
2021-03-25 12:40:04 +00:00
|
|
|
|
|
|
|
|
|
this.semaphoreFile = semaphoreFile
|
|
|
|
|
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')
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
this.on('ready', () => {
|
|
|
|
|
if (this.firstBuild === false) {
|
2022-08-01 17:10:00 +00:00
|
|
|
|
writeFileSync(this.semaphoreFile, '', 'utf-8')
|
2021-03-25 12:40:04 +00:00
|
|
|
|
} else {
|
|
|
|
|
this.firstBuild = false
|
|
|
|
|
}
|
|
|
|
|
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-08-01 17:10:00 +00:00
|
|
|
|
rmSync(this.semaphoreFile, { force: true })
|
2021-03-25 12:40:04 +00:00
|
|
|
|
}
|
|
|
|
|
}
|