bigint-mod-arith/build/testing/mocha/builders/RollupBuilder.cjs

155 lines
4.5 KiB
JavaScript
Raw Normal View History

const EventEmitter = require('events')
const fs = require('fs')
const path = require('path')
const rollup = require('rollup')
const loadAndParseConfigFile = require('rollup/loadConfigFile').loadConfigFile
2022-08-01 02:04:00 +00:00
const Builder = require('./Builder.cjs')
const rootDir = path.join(__dirname, '../../../../')
2022-10-03 15:35:35 +00:00
const pkgJson = require(path.join(rootDir, 'package.json'))
2022-10-03 15:35:35 +00:00
const mochaTsRelativeDir = pkgJson.directories['mocha-ts']
const mochaTsDir = path.join(rootDir, mochaTsRelativeDir)
class RollupBuilder extends Builder {
constructor ({ name, configPath, tempDir }) {
super(path.join(tempDir, 'semaphore'), name)
2022-10-03 15:35:35 +00:00
this.tempDir = tempDir
this.configPath = configPath
2022-10-03 15:35:35 +00:00
this.firstBuild = true
}
2022-10-03 15:35:35 +00:00
async start ({ watch = false, commonjs = false }) {
await super.start()
2022-10-03 15:35:35 +00:00
this.watch = watch
this.commonjs = commonjs
this.watchedModule = commonjs ? pkgJson.exports['.'].node.require.default : pkgJson.exports['.'].node.import.default
2022-10-03 15:35:35 +00:00
const { options } = await loadAndParseConfigFile(this.configPath)
2022-10-03 15:35:35 +00:00
// Instead of compiling all the outputs let us just take the one we are using with mocha (either cjs or esm)
const rollupOptions = options.filter(bundle => {
const file = (bundle.output[0].dir !== undefined)
? path.join(bundle.output[0].dir, bundle.output[0].entryFileNames)
: bundle.output[0].file
2022-10-03 15:35:35 +00:00
return file === path.join(rootDir, this.watchedModule)
})[0]
2022-10-03 15:35:35 +00:00
if (rollupOptions.output.length > 1) {
rollupOptions.output = rollupOptions.output[0]
}
2022-10-03 15:35:35 +00:00
this.builder = new RollupBundler({ rollupOptions, watch: this.watch, watchedModule: this.watchedModule })
this.builder.on('event', event => {
2022-10-03 15:35:35 +00:00
let updateSemaphore = true
switch (event.code) {
case 'START':
this.emit('busy')
if (this.firstBuild === true) {
this.emit('message', 'building your module...')
} else {
this.emit('message', 'file changes detected. Rebuilding module files...')
}
break
case 'BUNDLE_END':
if (event.result) event.result.close()
break
case 'END':
if (event.result) event.result.close()
2022-10-03 15:35:35 +00:00
// fs.mkdirSync(path.join(this.tempDir, path.dirname(this.watchedModule)), { recursive: true })
// // console.log(path.join(this.tempDir, path.dirname(this.watchedModule)))
// fs.copyFileSync(this.watchedModule, path.join(this.tempDir, this.watchedModule))
if (this.firstBuild) {
this.firstBuild = false
updateSemaphore = false
}
this.emit('ready', updateSemaphore)
break
case 'ERROR':
if (event.result) event.result.close()
this.emit('error', event.error)
2022-10-03 15:35:35 +00:00
fs.writeFileSync(path.join(rootDir, this.watchedModule), '', 'utf8')
// fs.writeFileSync(path.join(this.tempDir, this.watchedModule), '', 'utf8')
this.emit('ready')
break
default:
this.emit('busy')
break
}
})
this.builder.start()
return await this.ready()
}
async close () {
await super.close()
this.builder.close()
}
}
class RollupBundler extends EventEmitter {
2022-10-03 15:35:35 +00:00
constructor ({ rollupOptions, watchedModule, watch = false }) {
super()
this.rollupOptions = rollupOptions
this.watch = watch
2022-10-03 15:35:35 +00:00
this.watchedModule = watchedModule
}
async start () {
if (this.watch === true) {
this.watcher = rollup.watch(this.rollupOptions)
this.watcher.on('event', event => {
this.emit('event', event)
})
} else {
2022-10-03 15:35:35 +00:00
if (!fs.existsSync(path.join(rootDir, this.watchedModule))) {
await this._bundle()
} else {
this.emit('event', { code: 'END', noBuild: true })
}
}
}
async _bundle () {
this.emit('event', { code: 'START' })
for (const optionsObj of [].concat(this.rollupOptions)) {
try {
const bundle = await rollup.rollup(optionsObj)
try {
await Promise.all(optionsObj.output.map(bundle.write))
this.emit('event', { code: 'BUNDLE_END' })
} catch (error) {
this.emit('event', { code: 'ERROR', error })
}
} catch (error) {
this.emit('event', { code: 'ERROR', error })
}
}
this.emit('event', { code: 'END' })
}
close () {
if (this.watcher !== undefined) this.watcher.close()
}
}
2022-10-03 15:35:35 +00:00
exports.RollupBuilder = RollupBuilder
exports.rollupBuilder = new RollupBuilder({
name: 'rollup',
configPath: path.join(rootDir, pkgJson.directories.build, 'rollup.config.js'),
tempDir: mochaTsDir
})