-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjob.ts
48 lines (43 loc) · 1.32 KB
/
job.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import {yaml} from "./deps.ts";
export type Batch = {
watch: {
folder: string;
files: RegExp;
};
edits?: Edit[];
chapters?: string[];
};
export type Edit = {
edit: string;
set: Map<string, string>;
};
export default class Job {
readonly file: string;
readonly batches: Batch[];
/**
* Creates a Job by loading data from an `automkv.yml` file. This will
* throw an InvalidJobException if the data is incorrectly formatted.
*
* @param file An `automkv.yml` file
*/
constructor(file: string) {
this.file = file;
const yml = (yaml.parse(Deno.readTextFileSync(file)) as { batch: Batch[] }).batch;
if (!yml)
throw new Job.InvalidJobException("Improper YAML file: expected root element to be 'batch'");
// While functionally a map, we need to make it official
for (const batch of yml) {
batch.watch.files = new RegExp(batch.watch.files);
if (batch.edits)
for (const edit of batch.edits)
edit.set = new Map(Object.entries(edit.set));
}
this.batches = yml;
}
static InvalidJobException = class extends Error {
constructor(message: string) {
super(message);
this.name = this.constructor.name;
}
}
}