2022-08-31 12:42:53 +00:00
|
|
|
import {createHash} from 'crypto';
|
|
|
|
import {existsSync, Stats} from 'fs';
|
|
|
|
import {mkdir, readFile, stat, writeFile} from 'fs/promises';
|
|
|
|
import {tmpdir} from 'os';
|
2022-10-05 12:17:03 +00:00
|
|
|
import {dirname, join} from 'path';
|
2022-08-17 12:39:41 +00:00
|
|
|
|
2023-06-07 11:17:31 +00:00
|
|
|
import {hasMagic, globSync} from 'glob';
|
2023-02-15 23:09:31 +00:00
|
|
|
|
2022-08-17 12:39:41 +00:00
|
|
|
interface JobContext {
|
|
|
|
name: string;
|
|
|
|
inputs: string[];
|
|
|
|
outputs: string[];
|
|
|
|
}
|
|
|
|
|
|
|
|
class JobBuilder {
|
|
|
|
#inputs: string[] = [];
|
|
|
|
#outputs: string[] = [];
|
|
|
|
#callback: (ctx: JobContext) => Promise<void>;
|
|
|
|
#name: string;
|
2022-08-31 12:42:53 +00:00
|
|
|
#value = '';
|
|
|
|
#force = false;
|
2022-08-17 12:39:41 +00:00
|
|
|
|
|
|
|
constructor(name: string, callback: (ctx: JobContext) => Promise<void>) {
|
|
|
|
this.#name = name;
|
|
|
|
this.#callback = callback;
|
|
|
|
}
|
|
|
|
|
2022-08-31 12:42:53 +00:00
|
|
|
get jobHash(): string {
|
|
|
|
return createHash('sha256').update(this.#name).digest('hex');
|
|
|
|
}
|
|
|
|
|
|
|
|
force() {
|
|
|
|
this.#force = true;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
value(value: string) {
|
|
|
|
this.#value = value;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2022-08-17 12:39:41 +00:00
|
|
|
inputs(inputs: string[]): JobBuilder {
|
|
|
|
this.#inputs = inputs.flatMap(value => {
|
2023-06-07 11:17:31 +00:00
|
|
|
if (hasMagic(value)) {
|
|
|
|
return globSync(value);
|
2022-08-31 12:42:53 +00:00
|
|
|
}
|
2022-10-05 12:17:03 +00:00
|
|
|
return value;
|
2022-08-17 12:39:41 +00:00
|
|
|
});
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
outputs(outputs: string[]): JobBuilder {
|
|
|
|
if (!this.#name) {
|
2022-08-31 12:42:53 +00:00
|
|
|
this.#name = outputs.join(' and ');
|
2022-08-17 12:39:41 +00:00
|
|
|
}
|
|
|
|
|
2022-10-05 12:17:03 +00:00
|
|
|
this.#outputs = outputs;
|
2022-08-17 12:39:41 +00:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
async build(): Promise<void> {
|
|
|
|
console.log(`Running job ${this.#name}...`);
|
2022-08-31 12:42:53 +00:00
|
|
|
// For debugging.
|
|
|
|
if (this.#force) {
|
|
|
|
return this.#run();
|
|
|
|
}
|
|
|
|
// In case we deleted an output file on purpose.
|
|
|
|
if (!this.getOutputStats()) {
|
|
|
|
return this.#run();
|
|
|
|
}
|
|
|
|
// Run if the job has a value, but it changes.
|
|
|
|
if (this.#value) {
|
|
|
|
if (!(await this.isValueDifferent())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return this.#run();
|
|
|
|
}
|
|
|
|
// Always run when there is no output.
|
|
|
|
if (!this.#outputs.length) {
|
|
|
|
return this.#run();
|
|
|
|
}
|
|
|
|
// Make-like comparator.
|
|
|
|
if (!(await this.areInputsNewer())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return this.#run();
|
|
|
|
}
|
2022-08-17 12:39:41 +00:00
|
|
|
|
2022-08-31 12:42:53 +00:00
|
|
|
async isValueDifferent(): Promise<boolean> {
|
|
|
|
const file = join(tmpdir(), `puppeteer/${this.jobHash}.txt`);
|
|
|
|
await mkdir(dirname(file), {recursive: true});
|
|
|
|
if (!existsSync(file)) {
|
|
|
|
await writeFile(file, this.#value);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return this.#value !== (await readFile(file, 'utf8'));
|
|
|
|
}
|
2022-08-17 12:39:41 +00:00
|
|
|
|
2022-08-31 12:42:53 +00:00
|
|
|
#outputStats?: Stats[];
|
|
|
|
async getOutputStats(): Promise<Stats[] | undefined> {
|
|
|
|
if (this.#outputStats) {
|
|
|
|
return this.#outputStats;
|
|
|
|
}
|
2022-08-17 12:39:41 +00:00
|
|
|
try {
|
2022-08-31 12:42:53 +00:00
|
|
|
this.#outputStats = await Promise.all(
|
2022-08-17 12:39:41 +00:00
|
|
|
this.#outputs.map(output => {
|
|
|
|
return stat(output);
|
|
|
|
})
|
|
|
|
);
|
|
|
|
} catch {}
|
2022-08-31 12:42:53 +00:00
|
|
|
return this.#outputStats;
|
|
|
|
}
|
2022-08-17 12:39:41 +00:00
|
|
|
|
2022-08-31 12:42:53 +00:00
|
|
|
async areInputsNewer(): Promise<boolean> {
|
|
|
|
const inputStats = await Promise.all(
|
|
|
|
this.#inputs.map(input => {
|
|
|
|
return stat(input);
|
|
|
|
})
|
|
|
|
);
|
|
|
|
const outputStats = await this.getOutputStats();
|
|
|
|
if (
|
|
|
|
outputStats &&
|
|
|
|
outputStats.reduce(reduceMinTime, Infinity) >
|
|
|
|
inputStats.reduce(reduceMaxTime, 0)
|
|
|
|
) {
|
|
|
|
return false;
|
2022-08-17 12:39:41 +00:00
|
|
|
}
|
2022-08-31 12:42:53 +00:00
|
|
|
return true;
|
2022-08-17 12:39:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#run(): Promise<void> {
|
|
|
|
return this.#callback({
|
|
|
|
name: this.#name,
|
|
|
|
inputs: this.#inputs,
|
|
|
|
outputs: this.#outputs,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const job = (
|
|
|
|
name: string,
|
|
|
|
callback: (ctx: JobContext) => Promise<void>
|
|
|
|
): JobBuilder => {
|
|
|
|
return new JobBuilder(name, callback);
|
|
|
|
};
|
|
|
|
|
|
|
|
const reduceMaxTime = (time: number, stat: Stats) => {
|
|
|
|
return time < stat.mtimeMs ? stat.mtimeMs : time;
|
|
|
|
};
|
|
|
|
|
|
|
|
const reduceMinTime = (time: number, stat: Stats) => {
|
|
|
|
return time > stat.mtimeMs ? stat.mtimeMs : time;
|
|
|
|
};
|