-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask.js
49 lines (43 loc) · 1.28 KB
/
task.js
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
import { walkSync } from "https://deno.land/std/fs/walk.ts";
/**
* Tasks are user-defined subcommands of the `saur` CLI that are active
* when the user is at the top level of an application directory. They
* are typically created using the `task()` function which is the
* default export of this module. Generally, `Task` classes do not need
* to be instantiated or used direcrlty.
*/
export class Task {
static async all() {
const tasks = [];
try {
for (const { filename } of walkSync(`${Deno.cwd()}/tasks`)) {
if (filename.match(/\.js$/)) {
const exports = await import(filename);
tasks.push(exports.default);
}
}
return tasks;
} catch (e) {
return tasks;
}
}
static find(command) {
const task = this.all.find((task) => task.name === command);
if (!task) {
throw new Error(`Invalid command "${command}"`);
}
return task;
}
constructor({ name, description, perform }) {
this.name = name;
this.description = description;
this.perform = perform;
}
}
/**
* Create a new task with the given `name` and `description`, calling
* the function when the task is invoked.
*/
export default function task(name, description, perform) {
return new Task({ name, description, perform });
}