@enspirit/emb
Version:
A replacement for our Makefile-for-monorepos
42 lines (41 loc) • 1.38 kB
JavaScript
import { stat } from 'node:fs/promises';
import pMap from 'p-map';
export class FilePrerequisitePlugin {
async diff(component, prerequisites, previous, _actual) {
const stats = await this.getStats(component, prerequisites);
const changes = stats
.filter((s) => {
return s.mtimeMs > Number.parseInt(previous, 10);
})
.map((s) => ({
path: s.path,
type: s.type,
}));
return changes.length > 0 ? changes : null;
}
async meta(component, prerequisites, mode) {
switch (mode) {
case 'post': {
return Date.now().toString();
}
case 'pre': {
const stats = await this.getStats(component, prerequisites);
const max = stats.reduce((minimum, stat) => {
return Math.max(minimum, stat.mtimeMs);
}, 0);
return max.toFixed(0).toString();
}
default: {
throw new Error(`Invalid mode passed to 'meta()': ${mode}`);
}
}
}
async getStats(component, prerequisites) {
return pMap(prerequisites, async (file) => {
return {
...file,
...(await stat(component.join(file.path))),
};
}, { concurrency: 30 });
}
}