import { debounce } from './utils.js' export type BuildAndRestartOptions = { rebuild?: boolean clean?: boolean } class BuildManager { private shouldRebuild: boolean private shouldClean: boolean private debouncedBuild: ReturnType private buildFn: (options: BuildAndRestartOptions) => Promise constructor(buildFn: (options: BuildAndRestartOptions) => Promise) { this.shouldRebuild = true this.shouldClean = false this.buildFn = buildFn // TODO: Remove process.env.RWJS_DELAY_RESTART in next major release if (process.env.RWJS_DELAY_RESTART) { console.warn( '[DEPRECATED] RWJS_DELAY_RESTART is deprecated and will be removed in the next major release. ' + 'Please rename it to CEDAR_DELAY_API_RESTART in your .env file.', ) } const delay = process.env.CEDAR_DELAY_API_RESTART || process.env.RWJS_DELAY_RESTART this.debouncedBuild = debounce( async (options: BuildAndRestartOptions) => { // Use flags with higher precedence to determine if we should rebuild or clean try { await this.buildFn({ ...options, rebuild: this.shouldRebuild, clean: this.shouldClean, }) } finally { this.shouldRebuild = true this.shouldClean = false } }, // We want to delay execution when multiple files are modified on the // filesystem. This usually happens when running Cedar generator commands. // Local writes are very fast, but writes in e2e environments are not, so // allow the default to be adjusted with an env-var. delay ? parseInt(delay, 10) : 500, ) } // Wrapper method to handle options and set precedence flags. // If we ever see a `rebuild: false` option while debouncing, we never want to rebuild. // If we ever see a `clean: true` option, we always want to clean. async run(options: BuildAndRestartOptions) { if (options.rebuild === false) { this.shouldRebuild = false } if (options.clean) { this.shouldClean = true } await this.debouncedBuild(options) } cancelScheduledBuild() { this.debouncedBuild.cancel() } } export { BuildManager }