-
Notifications
You must be signed in to change notification settings - Fork 7
/
mod.ts
107 lines (97 loc) · 3.02 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
// Drake API.
export {
abort,
env,
glob,
log,
quote,
readFile,
sh,
updateFile,
writeFile
} from "./lib/utils.ts";
export { desc, execute, run, task, vers };
import { existsSync } from "https://deno.land/std@v0.35.0/fs/mod.ts";
import * as path from "https://deno.land/std@v0.35.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";
/** The Drake version number. */
const vers: string = "0.4.0";
/** 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"] ? 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);
}
}
/** 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 i.e. the names of tasks to be run prior to executing the task action function.
* @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 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"]) {
taskRegistry.list();
} 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 ignoring task prerequisites.
*
* - If `names` is a task name string execute the task action.
* - If `names` is an array of task names execute their actions asynchronously.
* - Silently skip tasks that have no action function.
*/
async function execute(names: string | string[]) {
await taskRegistry.execute(names);
}