-
Notifications
You must be signed in to change notification settings - Fork 0
/
tasks.ts
70 lines (57 loc) · 1.69 KB
/
tasks.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import { parse } from "./parse.ts";
import { getDeps } from "./dependencies.ts";
import { porcelain } from "https://deno.land/x/[email protected]/mod.ts";
const { run } = porcelain;
const getTasks = async (file: string, selector: string) => {
const content = await Deno.readTextFile(file);
const tasks = parse(content).querySelectorAll(selector);
if (tasks.some((t) => t.type !== "heading")) {
console.error(
"Some of the selected tasks are not markdown heading. Got:",
tasks,
);
Deno.exit();
}
const taskList = tasks.map(
// @ts-ignore type inference doesn't work
(task) => [task.value, task.querySelector("code").value],
);
return Object.fromEntries(taskList);
};
type ListOptions = {
file: string;
selector: string;
};
export const listTasks = async ({ file, selector }: ListOptions) => {
const tasksMap = await getTasks(file, selector);
console.log(tasksMap);
};
type RunOptions = {
file: string;
tasksSelector: string;
depsSelector: string;
};
export const runTask = async (
taskName: string,
{ file, tasksSelector, depsSelector }: RunOptions,
) => {
const tasksMap = await getTasks(file, tasksSelector);
if (!(taskName in tasksMap)) {
console.log(
"Requested task was not found",
taskName,
Object.keys(tasksMap),
);
Deno.exit();
}
const task = `#!/usr/bin/env bash\n\n${tasksMap[taskName]}`;
const deps = await getDeps(file, depsSelector);
const fileName = await Deno.makeTempFile();
try {
await Deno.writeTextFile(fileName, task);
const depsStr = deps.map(([dep, ver]) => `+${dep}@${ver}`).join(' ')
await run(`pkgx ${depsStr} ${fileName}`);
} finally {
await Deno.remove(fileName);
}
};