bigint-crypto-utils/build/testing/mocha/builders/Builder.js

60 lines
1.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import EventEmitter from 'events'
import { mkdirSync, writeFileSync, rmSync } from 'fs'
import { dirname } from 'path'
export default class Builder extends EventEmitter {
constructor (semaphoreFile, name = 'builder') {
super()
this.name = name
this.firstBuild = true
mkdirSync(dirname(semaphoreFile), { recursive: true })
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) {
writeFileSync(this.semaphoreFile, '', 'utf-8')
} 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 () {
rmSync(this.semaphoreFile, { force: true })
}
}