-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdynamic-tasks.ts
66 lines (52 loc) · 1.85 KB
/
dynamic-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
import * as path from "https://deno.land/std@0.214.0/path/mod.ts";
import {
desc,
env,
execute,
glob,
run,
sh,
task,
} from "https://deno.land/x/drake@v1.7.0/mod.ts";
// } from "../mod.ts";
desc("command-line usage");
task("help", [], function () {
console.log(`
Example Drakefile illustrating dynamic task creation.
Dynamically create file tasks to compile markdown source files to HTML.
Execute them synchronously and asynchronously with 'compile-sync' and
'compile-async' tasks respectively.
Example usage:
deno run -A dynamic-tasks.ts compile-async mdfiles=~/doc/test/*.md outdir=/tmp
Environment variables:
mdfiles=<markdown source files glob> (default: "*.md")
outdir=<output directory for compiled HTML files> (default: mdfiles directory)
- The 'compile-async' task runs up to 5 times faster than 'compile-sync'.
- Increase the maximum number of open file descriptors with ulimit(1) if
there are 'Too many open files (os error 24)'.
- Test results compiling 5000 markdown files:
compile-async: 42.5s (5000 files updated)
compile-async: 0.8s (0 files updated)
compile-sync: 311s (5000 files updated)
compile-sync: 0.8s (0 files updated)
`);
});
const tasks: string[] = [];
const mdfiles = env("mdfiles") || "*.md";
const outdir = env("outdir") || path.dirname(mdfiles);
for (const prereq of glob(mdfiles)) {
let target = `${prereq.replace(/\.[^/.]+$/, "")}.html`;
target = path.join(outdir, path.basename(target));
// Create a file task to compile the markdown source file.
task(target, [prereq], async function () {
await sh(`marked "${prereq}" > "${target}"`);
});
tasks.push(target);
}
desc("compile markdown files synchronously");
task("compile-sync", tasks);
desc("compile markdown files asynchronously");
task("compile-async", [], async function () {
await execute(...tasks);
});
await run();