-
Notifications
You must be signed in to change notification settings - Fork 6
/
mod.ts
118 lines (106 loc) · 3.09 KB
/
mod.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Drake API.
export {
abort,
debug,
DrakeError,
env,
glob,
log,
outOfDate,
quote,
readFile,
sh,
shCapture,
ShCaptureOpts,
ShOpts,
ShOutput,
touch,
updateFile,
writeFile,
} from "./lib/utils.ts";
export { desc, execute, run, task, vers };
import { existsSync } from "https://deno.land/std@v0.41.0/fs/mod.ts";
import * as path from "https://deno.land/std@v0.41.0/path/mod.ts";
import { help } from "./lib/help.ts";
import { Action, TaskRegistry } from "./lib/tasks.ts";
import { abort, env, parseEnv } from "./lib/utils.ts";
const DRAKE_VERS = "0.41.0";
env("--abort-exits", true);
/** Global task registry. */
const taskRegistry = new TaskRegistry();
// Parse command-line options into Drake environment.
parseEnv(Deno.args.slice(), env);
if (env("--help")) {
help();
} else if (env("--version")) {
console.log(vers());
} else {
// Caclulate drakefile path relative to cwd prior to processing --directory option.
let drakefile = env("--drakefile") ?? "Drakefile.ts";
if (!path.isAbsolute(drakefile)) {
drakefile = path.join(Deno.cwd(), drakefile);
}
env("--drakefile", drakefile);
if (env("--directory")) {
const dir = env("--directory");
if (!existsSync(dir) || !Deno.statSync(dir).isDirectory) {
abort(`--directory missing or not a directory: ${dir}`);
}
Deno.chdir(dir);
}
}
/** Returns the Drake version number string. */
function vers(): string {
return DRAKE_VERS;
}
/** Set description of next registered task. */
function desc(description: string): void {
taskRegistry.desc(description);
}
/**
* Create and register a task.
* @param name - A unique task name.
* @param prereqs - An array of prerequisite task names. Prerequisites can be _Normal task_ names, _File task_ names, file paths or globs (wildcards).
* @param action - An optional function that is run if the task is selected for execution.
*/
function task(
name: string,
prereqs: string[] = [],
action?: Action,
): void {
taskRegistry.register(name, prereqs, action);
}
/**
* Execute named tasks along with their prerequisite tasks (direct and indirect). If no `names` are
* specified then the command-line tasks are run. If no command-line tasks were specified the
* default task (set in `env("--default-task")`) is run.
*
* Task execution is ordered such that prerequisite tasks are executed prior to their parent task.
* The same task is never run twice.
*/
async function run(...names: string[]) {
if (env("--help") || env("--version")) {
return;
}
if (env("--list-tasks") || env("--list-all")) {
taskRegistry.list().forEach((t) => console.log(t));
} else {
if (names.length === 0) {
names = env("--tasks");
if (names.length === 0 && env("--default-task")) {
names.push(env("--default-task"));
}
}
if (names.length === 0) {
abort("no task specified");
}
await taskRegistry.run(...names);
}
}
/**
* Unconditionally execute task action functions asynchronously.
* Silently skip tasks that have no action function.
*/
async function execute(...names: string[]) {
await taskRegistry.execute(...names);
}