UNPKG

@enspirit/emb

Version:

A replacement for our Makefile-for-monorepos

47 lines (46 loc) 1.69 kB
import { AbstractResourceBuilder } from './AbstractResourceBuilder.js'; export class SentinelFileBasedBuilder extends AbstractResourceBuilder { lastSentinelFile; newSentinelData; /** * Checks wether or not the sentinel file is more recent * that the output of the builder's sentinel data */ async mustBuild(resource) { if (!this._mustBuild) { return; } this.lastSentinelFile = await this.readSentinel(); this.newSentinelData = await this._mustBuild(resource); if (!(this.lastSentinelFile && this.newSentinelData)) { return this.newSentinelData; } if (this.lastSentinelFile.mtime < this.newSentinelData.mtime) { return this.newSentinelData; } } async build(resource, out) { return this._build(resource, out); } get sentinelFileName() { const { monorepo, config } = this.context; return `sentinels/flavors/${monorepo.currentFlavor}/${config.component}/${config.name}.built`; } async storeSentinelData(data) { await this.context.monorepo.store.writeFile(this.sentinelFileName, JSON.stringify(data)); } async readSentinel() { const stats = await this.context.monorepo.store.stat(this.sentinelFileName, false); if (!stats) { return undefined; } const data = await this.context.monorepo.store.readFile(this.sentinelFileName, false); return { data: data ? JSON.parse(data) : data, mtime: stats.mtime.getTime(), }; } async _commit(_resource, _output, reason) { this.storeSentinelData(reason); } }