-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.ts
113 lines (104 loc) · 2.81 KB
/
index.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
import { helpers, termost } from "termost";
type ProgramContext = {
computedFromOtherTaskValues: "big" | "small";
execOutput: string;
size: number;
};
const program = termost<ProgramContext>("Example to showcase the `task` API");
program
.task({
key: "size",
label: "Task with returned value (persisted)",
handler() {
return 45;
},
})
.task({
label: "Task with side-effect only (no persisted value)",
async handler() {
// @note: side-effect only handler
await wait(500);
},
})
.task({
key: "computedFromOtherTaskValues",
label: "Task can also access other persisted task values",
async handler(context) {
if (context.size > 2000) {
return Promise.resolve("big" as const);
}
return Promise.resolve("small" as const);
},
})
.task({
key: "execOutput",
label: "Or even execute external commands thanks to its provided helpers",
async handler() {
return helpers.exec("echo 'Hello from shell'");
},
})
.task({
label: "A task can be skipped as well",
async handler() {
await wait(2000);
},
skip(context) {
const needOptimization = context.size > 2000;
return !needOptimization;
},
})
.task({
label: (context) =>
`A task can have a dynamic label generated from contextual values: ${context.computedFromOtherTaskValues}`,
handler() {
return;
},
})
.task({
handler(context) {
helpers.message(
`If you don't specify a label, the handler is executed in "live mode" (the output is not hidden by the label and is displayed gradually).`,
{ label: "Label & console output" },
);
helpers.message(
`A task with a specified "key" can be retrieved here. Size = ${context.size}. If no "key" was specified the task returned value cannot be persisted across program instructions.`,
{ label: "Context management" },
);
},
})
.task({
handler(context) {
const content =
"The `message` helpers can be used to display task content in a nice way";
helpers.message(content, {
label: "Output formatting",
});
helpers.message(content, { type: "warning" });
helpers.message(content, { type: "error" });
helpers.message(content, { type: "success" });
helpers.message(content, {
label: "👋 You can also customize the label",
type: "information",
});
helpers.message(
["I support also", "multilines", "with array input"],
{
label: "👋 You can also customize the label",
type: "information",
},
);
console.log(
helpers.format(
"\nYou can also have a total control on the formatting through the `format` helper.",
{
color: "white",
modifiers: ["italic", "strikethrough", "bold"],
},
),
);
console.info(JSON.stringify(context, null, 2));
},
});
const wait = async (delay: number) => {
return new Promise((resolve) => setTimeout(resolve, delay));
};