-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
79 lines (72 loc) · 1.97 KB
/
index.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
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
const {grey, reset} = require("kleur");
const Spinnies = require("spinnies");
function taskz(tasks, options = { parallel: false }) {
return {
run: async (
ctx = {},
level = 0,
spinnies = new Spinnies({ succeedColor: "green", succeedPrefix: "✔" })
) =>
(options.parallel ? runParallel : runSequence)(
typeof tasks === 'function' ? tasks(ctx) : tasks,
level,
ctx,
spinnies
)
};
}
async function runParallel(tasks, level, ctx, spinnies) {
if (tasks.filter(t => t.stopOnError).length) {
throw new Error("stopOnError is not compatible with runParallel");
}
const promises = tasks.map((t, i) => {
return new Promise(async (resolve, reject) => {
try {
runTask({ t, i, level, ctx, spinnies, tasks });
resolve();
} catch (e) {
reject();
}
});
});
try {
await Promise.all(promises);
} catch (e) {}
}
async function runSequence(tasks, level, ctx, spinnies) {
for (let i = 0; i < tasks.length; i++) {
try {
await runTask({ t: tasks[i], i, level, ctx, spinnies, tasks });
} catch (e) {
break;
}
}
}
async function runTask({ t, i, level, ctx, spinnies, tasks }) {
const id = String(i + Math.random());
const counter = grey().dim(` [${i + 1}/${tasks.length}]`);
spinnies.add(id, { text: `${t.text}${counter}`, indent: level });
const setText = val => {
spinnies.update(id, { text: `${val}${counter}` });
};
if (t.tasks) {
// Subtasks
spinnies.update(id, {
text: `→ ${t.text}${counter}`,
status: "stopped"
});
await t.tasks.run(ctx, level + 1, spinnies);
} else {
// Run one task
try {
await t.task(ctx, setText);
spinnies.succeed(id, { text: reset(spinnies.pick(id).text) });
} catch (e) {
spinnies.fail(id, { text: reset(`${e.message}${counter}`) });
if (t.stopOnError) {
throw new Error("Stopped on error.");
}
}
}
}
module.exports = taskz;