@lekman/devops-toolkit-tasks 1.4.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/help.js ADDED
@@ -0,0 +1,151 @@
1
+ // @bun
2
+ // bin/help.ts
3
+ import { basename, resolve } from "path";
4
+ import { parseArgs } from "util";
5
+
6
+ // src/help/help-groups.ts
7
+ var ROOT_TITLE = "Project";
8
+ var TASKFILE_NAMES = /^taskfile(\.dist)?\.ya?ml$/i;
9
+
10
+ class HelpGroups {
11
+ static group(entries, rootDir) {
12
+ const byTitle = new Map;
13
+ for (const entry of entries) {
14
+ const title = HelpGroups.title(entry.taskfile, rootDir);
15
+ byTitle.set(title, [...byTitle.get(title) ?? [], entry]);
16
+ }
17
+ return [...byTitle.entries()].sort(([a], [b]) => HelpGroups.order(a, b)).map(([title, list]) => ({
18
+ title,
19
+ entries: [...list].sort((a, b) => a.name.localeCompare(b.name))
20
+ }));
21
+ }
22
+ static title(taskfile, rootDir) {
23
+ const parts = taskfile.split(/[\\/]/).filter(Boolean);
24
+ const file = parts.at(-1) ?? "";
25
+ const folder = parts.slice(0, -1).join("/");
26
+ const root = rootDir.split(/[\\/]/).filter(Boolean).join("/");
27
+ if (TASKFILE_NAMES.test(file)) {
28
+ return folder === root ? ROOT_TITLE : parts.at(-2) ?? ROOT_TITLE;
29
+ }
30
+ return file.replace(/\.ya?ml$/i, "");
31
+ }
32
+ static order(a, b) {
33
+ if (a === ROOT_TITLE)
34
+ return -1;
35
+ if (b === ROOT_TITLE)
36
+ return 1;
37
+ return a.localeCompare(b);
38
+ }
39
+ }
40
+
41
+ // src/help/help-table.ts
42
+ var STYLE = {
43
+ alias: "\x1B[33m",
44
+ bold: "\x1B[1m",
45
+ heading: "\x1B[1;34m",
46
+ name: "\x1B[32m",
47
+ reset: "\x1B[0m"
48
+ };
49
+
50
+ class HelpTable {
51
+ static render(title, groups, color) {
52
+ const entries = groups.flatMap((group) => group.entries);
53
+ const nameWidth = HelpTable.widest(entries.map((e) => `task ${e.name}`));
54
+ const aliasWidth = HelpTable.widest([
55
+ "Aliases",
56
+ ...entries.map((e) => e.aliases.join(", "))
57
+ ]);
58
+ const paint = (style, text) => color ? `${STYLE[style]}${text}${STYLE.reset}` : text;
59
+ const lines = [paint("bold", title), ""];
60
+ lines.push(` ${"Command".padEnd(nameWidth)} ${"Aliases".padEnd(aliasWidth)} Description`);
61
+ for (const group of groups) {
62
+ lines.push("", paint("heading", group.title));
63
+ for (const entry of group.entries) {
64
+ const name = `task ${entry.name}`.padEnd(nameWidth);
65
+ const aliases = entry.aliases.join(", ").padEnd(aliasWidth);
66
+ const row = ` ${paint("name", name)} ${paint("alias", aliases)} ${entry.desc}`;
67
+ lines.push(row.trimEnd());
68
+ }
69
+ }
70
+ lines.push("", "Pass arguments to a task after --, as in: task <name> -- <args>");
71
+ return `${lines.join(`
72
+ `)}
73
+ `;
74
+ }
75
+ static widest(values) {
76
+ return values.reduce((max, value) => Math.max(max, value.length), 0);
77
+ }
78
+ }
79
+
80
+ // src/help/task-catalog.ts
81
+ class TaskCatalog {
82
+ static parse(json) {
83
+ const doc = JSON.parse(json);
84
+ const tasks = TaskCatalog.field(doc, "tasks");
85
+ if (!Array.isArray(tasks)) {
86
+ throw new Error("task --list-all --json returned no tasks array");
87
+ }
88
+ return tasks.flatMap((task) => {
89
+ const name = TaskCatalog.field(task, "name");
90
+ if (typeof name !== "string" || name === "")
91
+ return [];
92
+ const desc = TaskCatalog.field(task, "desc");
93
+ const aliases = TaskCatalog.field(task, "aliases");
94
+ const location = TaskCatalog.field(task, "location");
95
+ const taskfile = TaskCatalog.field(location, "taskfile");
96
+ return [
97
+ {
98
+ name,
99
+ desc: typeof desc === "string" ? desc : "",
100
+ aliases: Array.isArray(aliases) ? aliases.filter((a) => typeof a === "string") : [],
101
+ taskfile: typeof taskfile === "string" ? taskfile : ""
102
+ }
103
+ ];
104
+ });
105
+ }
106
+ static field(value, key) {
107
+ if (typeof value !== "object" || value === null)
108
+ return;
109
+ return value[key];
110
+ }
111
+ }
112
+
113
+ // src/help/help.ts
114
+ class Help {
115
+ static page(catalog, options) {
116
+ const entries = TaskCatalog.parse(catalog.listAll(options.rootDir));
117
+ const groups = HelpGroups.group(entries, options.rootDir);
118
+ return HelpTable.render(options.title, groups, options.color);
119
+ }
120
+ }
121
+ // src/help/help.system.ts
122
+ import { spawnSync } from "child_process";
123
+
124
+ class TaskBinaryCatalog {
125
+ taskBinary;
126
+ constructor(taskBinary = "task") {
127
+ this.taskBinary = taskBinary;
128
+ }
129
+ listAll(rootDir) {
130
+ const result = spawnSync(this.taskBinary, ["--list-all", "--json", "--dir", rootDir], { encoding: "utf8" });
131
+ if (result.status !== 0) {
132
+ throw new Error(`${this.taskBinary} --list-all --json failed: ${result.stderr || result.error?.message}`);
133
+ }
134
+ return result.stdout;
135
+ }
136
+ }
137
+ // bin/help.ts
138
+ var { values } = parseArgs({
139
+ options: {
140
+ dir: { type: "string" },
141
+ task: { type: "string" },
142
+ title: { type: "string" }
143
+ }
144
+ });
145
+ var rootDir = resolve(values.dir ?? process.cwd());
146
+ var color = Boolean(process.stdout.isTTY) && !process.env.NO_COLOR;
147
+ process.stdout.write(Help.page(new TaskBinaryCatalog(values.task || "task"), {
148
+ color,
149
+ rootDir,
150
+ title: values.title || basename(rootDir)
151
+ }));