Skip to content

Commit

Permalink
examples: Add compile-async task to the examples/dynamic-tasks.ts
Browse files Browse the repository at this point in the history
… example Drakefile to asynchronous execute dynamic file tasks.
  • Loading branch information
srackham committed Sep 5, 2020
1 parent fa95d16 commit b229755
Showing 1 changed file with 40 additions and 10 deletions.
50 changes: 40 additions & 10 deletions examples/dynamic-tasks.ts
@@ -1,20 +1,50 @@
import * as path from "https://deno.land/std@0.67.0/path/mod.ts";
import { env } from "../lib/env.ts";
import { desc, glob, run, sh, task } from "../mod.ts";
import * as path from "https://deno.land/std@0.68.0/path/mod.ts";
import { desc, env, execute, glob, run, sh, task } from "../mod.ts";

const tasks: string[] = [];
const mdfiles = env("mdfiles") || "*.md";
const outdir = env("outdir") || path.dirname(mdfiles);

for (const prereq of glob("*.md")) {
const target = `${path.basename(prereq, ".md")}.html`;
desc(`compile "${target}"`);
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");
task("compile-md", tasks);
env("--default-task", "compile-md");
desc("command-line usage");
task("help", [], function () {
console.log(`
Example Drakefile illustrating dynamic task creation.
run();
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)'.
`);
});

desc("compile markdown files synchronously");
task("compile-sync", tasks);

desc("compile markdown files asynchronously");
task("compile-async", [], async function () {
await execute(...tasks);
});

await run();

0 comments on commit b229755

Please sign in to comment.