@calmo/task-runner 1.0.0 → 1.1.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.
Files changed (41) hide show
  1. package/.gemini/commands/speckit.analyze.toml +188 -0
  2. package/.gemini/commands/speckit.checklist.toml +298 -0
  3. package/.gemini/commands/speckit.clarify.toml +185 -0
  4. package/.gemini/commands/speckit.constitution.toml +86 -0
  5. package/.gemini/commands/speckit.implement.toml +139 -0
  6. package/.gemini/commands/speckit.plan.toml +93 -0
  7. package/.gemini/commands/speckit.specify.toml +262 -0
  8. package/.gemini/commands/speckit.tasks.toml +141 -0
  9. package/.gemini/commands/speckit.taskstoissues.toml +34 -0
  10. package/.github/dependabot.yml +16 -0
  11. package/.github/workflows/ci.yml +43 -0
  12. package/.github/workflows/release.yml +46 -0
  13. package/.husky/commit-msg +1 -0
  14. package/.husky/pre-commit +1 -0
  15. package/.prettierignore +4 -0
  16. package/.releaserc.json +32 -0
  17. package/CHANGELOG.md +59 -0
  18. package/GEMINI.md +33 -0
  19. package/README.md +3 -1
  20. package/commitlint.config.js +3 -0
  21. package/coverage/TaskRunner.ts.html +685 -0
  22. package/coverage/base.css +224 -0
  23. package/coverage/block-navigation.js +87 -0
  24. package/coverage/coverage-final.json +2 -0
  25. package/coverage/favicon.png +0 -0
  26. package/coverage/index.html +116 -0
  27. package/coverage/prettify.css +1 -0
  28. package/coverage/prettify.js +2 -0
  29. package/coverage/sort-arrow-sprite.png +0 -0
  30. package/coverage/sorter.js +210 -0
  31. package/dist/TaskRunner.d.ts +47 -0
  32. package/dist/TaskRunner.js +53 -2
  33. package/dist/TaskRunner.js.map +1 -1
  34. package/package.json +24 -23
  35. package/pnpm-workspace.yaml +2 -0
  36. package/src/TaskResult.ts +15 -0
  37. package/src/TaskRunner.ts +200 -0
  38. package/src/TaskStatus.ts +4 -0
  39. package/src/TaskStep.ts +18 -0
  40. package/src/index.ts +4 -0
  41. package/tsconfig.test.json +8 -0
@@ -0,0 +1,210 @@
1
+ /* eslint-disable */
2
+ var addSorting = (function() {
3
+ 'use strict';
4
+ var cols,
5
+ currentSort = {
6
+ index: 0,
7
+ desc: false
8
+ };
9
+
10
+ // returns the summary table element
11
+ function getTable() {
12
+ return document.querySelector('.coverage-summary');
13
+ }
14
+ // returns the thead element of the summary table
15
+ function getTableHeader() {
16
+ return getTable().querySelector('thead tr');
17
+ }
18
+ // returns the tbody element of the summary table
19
+ function getTableBody() {
20
+ return getTable().querySelector('tbody');
21
+ }
22
+ // returns the th element for nth column
23
+ function getNthColumn(n) {
24
+ return getTableHeader().querySelectorAll('th')[n];
25
+ }
26
+
27
+ function onFilterInput() {
28
+ const searchValue = document.getElementById('fileSearch').value;
29
+ const rows = document.getElementsByTagName('tbody')[0].children;
30
+
31
+ // Try to create a RegExp from the searchValue. If it fails (invalid regex),
32
+ // it will be treated as a plain text search
33
+ let searchRegex;
34
+ try {
35
+ searchRegex = new RegExp(searchValue, 'i'); // 'i' for case-insensitive
36
+ } catch (error) {
37
+ searchRegex = null;
38
+ }
39
+
40
+ for (let i = 0; i < rows.length; i++) {
41
+ const row = rows[i];
42
+ let isMatch = false;
43
+
44
+ if (searchRegex) {
45
+ // If a valid regex was created, use it for matching
46
+ isMatch = searchRegex.test(row.textContent);
47
+ } else {
48
+ // Otherwise, fall back to the original plain text search
49
+ isMatch = row.textContent
50
+ .toLowerCase()
51
+ .includes(searchValue.toLowerCase());
52
+ }
53
+
54
+ row.style.display = isMatch ? '' : 'none';
55
+ }
56
+ }
57
+
58
+ // loads the search box
59
+ function addSearchBox() {
60
+ var template = document.getElementById('filterTemplate');
61
+ var templateClone = template.content.cloneNode(true);
62
+ templateClone.getElementById('fileSearch').oninput = onFilterInput;
63
+ template.parentElement.appendChild(templateClone);
64
+ }
65
+
66
+ // loads all columns
67
+ function loadColumns() {
68
+ var colNodes = getTableHeader().querySelectorAll('th'),
69
+ colNode,
70
+ cols = [],
71
+ col,
72
+ i;
73
+
74
+ for (i = 0; i < colNodes.length; i += 1) {
75
+ colNode = colNodes[i];
76
+ col = {
77
+ key: colNode.getAttribute('data-col'),
78
+ sortable: !colNode.getAttribute('data-nosort'),
79
+ type: colNode.getAttribute('data-type') || 'string'
80
+ };
81
+ cols.push(col);
82
+ if (col.sortable) {
83
+ col.defaultDescSort = col.type === 'number';
84
+ colNode.innerHTML =
85
+ colNode.innerHTML + '<span class="sorter"></span>';
86
+ }
87
+ }
88
+ return cols;
89
+ }
90
+ // attaches a data attribute to every tr element with an object
91
+ // of data values keyed by column name
92
+ function loadRowData(tableRow) {
93
+ var tableCols = tableRow.querySelectorAll('td'),
94
+ colNode,
95
+ col,
96
+ data = {},
97
+ i,
98
+ val;
99
+ for (i = 0; i < tableCols.length; i += 1) {
100
+ colNode = tableCols[i];
101
+ col = cols[i];
102
+ val = colNode.getAttribute('data-value');
103
+ if (col.type === 'number') {
104
+ val = Number(val);
105
+ }
106
+ data[col.key] = val;
107
+ }
108
+ return data;
109
+ }
110
+ // loads all row data
111
+ function loadData() {
112
+ var rows = getTableBody().querySelectorAll('tr'),
113
+ i;
114
+
115
+ for (i = 0; i < rows.length; i += 1) {
116
+ rows[i].data = loadRowData(rows[i]);
117
+ }
118
+ }
119
+ // sorts the table using the data for the ith column
120
+ function sortByIndex(index, desc) {
121
+ var key = cols[index].key,
122
+ sorter = function(a, b) {
123
+ a = a.data[key];
124
+ b = b.data[key];
125
+ return a < b ? -1 : a > b ? 1 : 0;
126
+ },
127
+ finalSorter = sorter,
128
+ tableBody = document.querySelector('.coverage-summary tbody'),
129
+ rowNodes = tableBody.querySelectorAll('tr'),
130
+ rows = [],
131
+ i;
132
+
133
+ if (desc) {
134
+ finalSorter = function(a, b) {
135
+ return -1 * sorter(a, b);
136
+ };
137
+ }
138
+
139
+ for (i = 0; i < rowNodes.length; i += 1) {
140
+ rows.push(rowNodes[i]);
141
+ tableBody.removeChild(rowNodes[i]);
142
+ }
143
+
144
+ rows.sort(finalSorter);
145
+
146
+ for (i = 0; i < rows.length; i += 1) {
147
+ tableBody.appendChild(rows[i]);
148
+ }
149
+ }
150
+ // removes sort indicators for current column being sorted
151
+ function removeSortIndicators() {
152
+ var col = getNthColumn(currentSort.index),
153
+ cls = col.className;
154
+
155
+ cls = cls.replace(/ sorted$/, '').replace(/ sorted-desc$/, '');
156
+ col.className = cls;
157
+ }
158
+ // adds sort indicators for current column being sorted
159
+ function addSortIndicators() {
160
+ getNthColumn(currentSort.index).className += currentSort.desc
161
+ ? ' sorted-desc'
162
+ : ' sorted';
163
+ }
164
+ // adds event listeners for all sorter widgets
165
+ function enableUI() {
166
+ var i,
167
+ el,
168
+ ithSorter = function ithSorter(i) {
169
+ var col = cols[i];
170
+
171
+ return function() {
172
+ var desc = col.defaultDescSort;
173
+
174
+ if (currentSort.index === i) {
175
+ desc = !currentSort.desc;
176
+ }
177
+ sortByIndex(i, desc);
178
+ removeSortIndicators();
179
+ currentSort.index = i;
180
+ currentSort.desc = desc;
181
+ addSortIndicators();
182
+ };
183
+ };
184
+ for (i = 0; i < cols.length; i += 1) {
185
+ if (cols[i].sortable) {
186
+ // add the click event handler on the th so users
187
+ // dont have to click on those tiny arrows
188
+ el = getNthColumn(i).querySelector('.sorter').parentElement;
189
+ if (el.addEventListener) {
190
+ el.addEventListener('click', ithSorter(i));
191
+ } else {
192
+ el.attachEvent('onclick', ithSorter(i));
193
+ }
194
+ }
195
+ }
196
+ }
197
+ // adds sorting functionality to the UI
198
+ return function() {
199
+ if (!getTable()) {
200
+ return;
201
+ }
202
+ cols = loadColumns();
203
+ loadData();
204
+ addSearchBox();
205
+ addSortIndicators();
206
+ enableUI();
207
+ };
208
+ })();
209
+
210
+ window.addEventListener('load', addSorting);
@@ -1,5 +1,33 @@
1
1
  import { TaskStep } from "./TaskStep.js";
2
2
  import { TaskResult } from "./TaskResult.js";
3
+ /**
4
+ * Define the payload for every possible event in the lifecycle.
5
+ */
6
+ export interface RunnerEventPayloads<TContext> {
7
+ workflowStart: {
8
+ context: TContext;
9
+ steps: TaskStep<TContext>[];
10
+ };
11
+ workflowEnd: {
12
+ context: TContext;
13
+ results: Map<string, TaskResult>;
14
+ };
15
+ taskStart: {
16
+ step: TaskStep<TContext>;
17
+ };
18
+ taskEnd: {
19
+ step: TaskStep<TContext>;
20
+ result: TaskResult;
21
+ };
22
+ taskSkipped: {
23
+ step: TaskStep<TContext>;
24
+ result: TaskResult;
25
+ };
26
+ }
27
+ /**
28
+ * A generic listener type that maps the event key to its specific payload.
29
+ */
30
+ export type RunnerEventListener<TContext, K extends keyof RunnerEventPayloads<TContext>> = (data: RunnerEventPayloads<TContext>[K]) => void | Promise<void>;
3
31
  /**
4
32
  * The main class that orchestrates the execution of a list of tasks
5
33
  * based on their dependencies, with support for parallel execution.
@@ -8,10 +36,29 @@ import { TaskResult } from "./TaskResult.js";
8
36
  export declare class TaskRunner<TContext> {
9
37
  private context;
10
38
  private running;
39
+ private listeners;
11
40
  /**
12
41
  * @param context The shared context object to be passed to each task.
13
42
  */
14
43
  constructor(context: TContext);
44
+ /**
45
+ * Subscribe to an event.
46
+ * @param event The event name.
47
+ * @param callback The callback to execute when the event is emitted.
48
+ */
49
+ on<K extends keyof RunnerEventPayloads<TContext>>(event: K, callback: RunnerEventListener<TContext, K>): void;
50
+ /**
51
+ * Unsubscribe from an event.
52
+ * @param event The event name.
53
+ * @param callback The callback to remove.
54
+ */
55
+ off<K extends keyof RunnerEventPayloads<TContext>>(event: K, callback: RunnerEventListener<TContext, K>): void;
56
+ /**
57
+ * Emit an event to all subscribers.
58
+ * @param event The event name.
59
+ * @param data The payload for the event.
60
+ */
61
+ private emit;
15
62
  /**
16
63
  * Executes a list of tasks, respecting their dependencies and running
17
64
  * independent tasks in parallel.
@@ -6,12 +6,56 @@
6
6
  export class TaskRunner {
7
7
  context;
8
8
  running = new Set();
9
+ listeners = {};
9
10
  /**
10
11
  * @param context The shared context object to be passed to each task.
11
12
  */
12
13
  constructor(context) {
13
14
  this.context = context;
14
15
  }
16
+ /**
17
+ * Subscribe to an event.
18
+ * @param event The event name.
19
+ * @param callback The callback to execute when the event is emitted.
20
+ */
21
+ on(event, callback) {
22
+ if (!this.listeners[event]) {
23
+ // Type assertion needed because TypeScript cannot verify that the generic K
24
+ // matches the specific key in the mapped type during assignment.
25
+ this.listeners[event] = new Set();
26
+ }
27
+ // Type assertion needed to tell TS that this specific Set matches the callback type
28
+ this.listeners[event].add(callback);
29
+ }
30
+ /**
31
+ * Unsubscribe from an event.
32
+ * @param event The event name.
33
+ * @param callback The callback to remove.
34
+ */
35
+ off(event, callback) {
36
+ if (this.listeners[event]) {
37
+ this.listeners[event].delete(callback);
38
+ }
39
+ }
40
+ /**
41
+ * Emit an event to all subscribers.
42
+ * @param event The event name.
43
+ * @param data The payload for the event.
44
+ */
45
+ emit(event, data) {
46
+ const listeners = this.listeners[event];
47
+ if (listeners) {
48
+ for (const listener of listeners) {
49
+ try {
50
+ listener(data);
51
+ }
52
+ catch (error) {
53
+ // Prevent listener errors from bubbling up
54
+ console.error(`Error in event listener for ${String(event)}:`, error);
55
+ }
56
+ }
57
+ }
58
+ }
15
59
  /**
16
60
  * Executes a list of tasks, respecting their dependencies and running
17
61
  * independent tasks in parallel.
@@ -20,6 +64,7 @@ export class TaskRunner {
20
64
  * and values are the corresponding TaskResult objects.
21
65
  */
22
66
  async execute(steps) {
67
+ this.emit("workflowStart", { context: this.context, steps });
23
68
  const results = new Map();
24
69
  while (results.size < steps.length) {
25
70
  const pendingSteps = steps.filter((step) => !results.has(step.name) && !this.running.has(step.name));
@@ -34,10 +79,12 @@ export class TaskRunner {
34
79
  const deps = step.dependencies ?? [];
35
80
  const failedDep = deps.find((dep) => results.has(dep) && results.get(dep)?.status !== "success");
36
81
  if (failedDep) {
37
- results.set(step.name, {
82
+ const result = {
38
83
  status: "skipped",
39
84
  message: `Skipped due to failed dependency: ${failedDep}`,
40
- });
85
+ };
86
+ results.set(step.name, result);
87
+ this.emit("taskSkipped", { step, result });
41
88
  }
42
89
  }
43
90
  if (readySteps.length === 0 &&
@@ -49,6 +96,7 @@ export class TaskRunner {
49
96
  }
50
97
  await Promise.all(readySteps.map(async (step) => {
51
98
  this.running.add(step.name);
99
+ this.emit("taskStart", { step });
52
100
  try {
53
101
  const result = await step.run(this.context);
54
102
  results.set(step.name, result);
@@ -61,9 +109,12 @@ export class TaskRunner {
61
109
  }
62
110
  finally {
63
111
  this.running.delete(step.name);
112
+ const result = results.get(step.name);
113
+ this.emit("taskEnd", { step, result });
64
114
  }
65
115
  }));
66
116
  }
117
+ this.emit("workflowEnd", { context: this.context, results });
67
118
  return results;
68
119
  }
69
120
  }
@@ -1 +1 @@
1
- {"version":3,"file":"TaskRunner.js","sourceRoot":"","sources":["../src/TaskRunner.ts"],"names":[],"mappings":"AAGA;;;;GAIG;AACH,MAAM,OAAO,UAAU;IAMD;IALZ,OAAO,GAAG,IAAI,GAAG,EAAU,CAAC;IAEpC;;OAEG;IACH,YAAoB,OAAiB;QAAjB,YAAO,GAAP,OAAO,CAAU;IAAG,CAAC;IAEzC;;;;;;OAMG;IACH,KAAK,CAAC,OAAO,CAAC,KAA2B;QACvC,MAAM,OAAO,GAAG,IAAI,GAAG,EAAsB,CAAC;QAE9C,OAAO,OAAO,CAAC,IAAI,GAAG,KAAK,CAAC,MAAM,EAAE,CAAC;YACnC,MAAM,YAAY,GAAG,KAAK,CAAC,MAAM,CAC/B,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,CAClE,CAAC;YAEF,MAAM,UAAU,GAAG,YAAY,CAAC,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE;gBAC9C,MAAM,IAAI,GAAG,IAAI,CAAC,YAAY,IAAI,EAAE,CAAC;gBACrC,OAAO,IAAI,CAAC,KAAK,CACf,CAAC,GAAG,EAAE,EAAE,CAAC,OAAO,CAAC,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AO,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,CAAE,CAAC;oBACvC,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,CAAC,CAAC;gBACzC,CAAC;YACH,CAAC,CAAC,CACH,CAAC;QACJ,CAAC;QAED,IAAI,CAAC,IAAI,CAAC,aAAa,EAAE,EAAE,OAAO,EAAE,IAAI,CAAC,OAAO,EAAE,OAAO,EAAE,CAAC,CAAC;QAC7D,OAAO,OAAO,CAAC;IACjB,CAAC;CACF"}
package/package.json CHANGED
@@ -1,7 +1,11 @@
1
1
  {
2
2
  "name": "@calmo/task-runner",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "description": "",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "https://github.com/thalesraymond/task-runner.git"
8
+ },
5
9
  "main": "dist/index.js",
6
10
  "types": "dist/index.d.ts",
7
11
  "exports": {
@@ -10,31 +14,35 @@
10
14
  "import": "./dist/index.js"
11
15
  }
12
16
  },
13
- "publishConfig": {
14
- "access": "public"
15
- },
16
- "files": [
17
- "dist",
18
- "README.md",
19
- "LICENSE"
20
- ],
21
- "repository": {
22
- "type": "git",
23
- "url": "https://github.com/thalesraymond/task-runner.git"
24
- },
25
17
  "type": "module",
18
+ "scripts": {
19
+ "build": "tsc",
20
+ "test": "tsc --noEmit -p tsconfig.test.json && vitest run --coverage",
21
+ "lint": "eslint .",
22
+ "lint:fix": "eslint . --fix",
23
+ "format": "prettier --write .",
24
+ "prepare": "husky",
25
+ "commit": "git add . &&git-cz"
26
+ },
26
27
  "config": {
27
28
  "commitizen": {
28
29
  "path": "git-cz"
29
30
  }
30
31
  },
31
32
  "keywords": [],
32
- "author": "Thales Raymond",
33
+ "author": "",
33
34
  "license": "ISC",
35
+ "packageManager": "pnpm@10.28.0",
34
36
  "devDependencies": {
35
37
  "@commitlint/cli": "^20.3.1",
36
38
  "@commitlint/config-conventional": "^20.3.1",
37
39
  "@eslint/js": "^9.39.2",
40
+ "@semantic-release/changelog": "^6.0.3",
41
+ "@semantic-release/commit-analyzer": "^13.0.1",
42
+ "@semantic-release/git": "^10.0.1",
43
+ "@semantic-release/github": "^12.0.2",
44
+ "@semantic-release/npm": "^13.1.3",
45
+ "@semantic-release/release-notes-generator": "^14.1.0",
38
46
  "@types/node": "^25.0.9",
39
47
  "@vitest/coverage-v8": "4.0.17",
40
48
  "eslint": "^9.39.2",
@@ -42,16 +50,9 @@
42
50
  "git-cz": "^4.9.0",
43
51
  "husky": "^9.1.7",
44
52
  "prettier": "^3.8.0",
53
+ "semantic-release": "^25.0.2",
45
54
  "typescript": "^5.9.3",
46
55
  "typescript-eslint": "^8.53.0",
47
56
  "vitest": "^4.0.17"
48
- },
49
- "scripts": {
50
- "build": "tsc",
51
- "test": "vitest run --coverage",
52
- "lint": "eslint .",
53
- "lint:fix": "eslint . --fix",
54
- "format": "prettier --write .",
55
- "commit": "git add . && git-cz"
56
57
  }
57
- }
58
+ }
@@ -0,0 +1,2 @@
1
+ ignoredBuiltDependencies:
2
+ - esbuild
@@ -0,0 +1,15 @@
1
+ import { TaskStatus } from "./TaskStatus.js";
2
+
3
+ /**
4
+ * Defines the result object returned by a single task step.
5
+ */
6
+ export interface TaskResult {
7
+ /** The final status of the task. */
8
+ status: TaskStatus;
9
+ /** An optional message, typically used for success statuses. */
10
+ message?: string;
11
+ /** An optional error message, typically used for failure statuses. */
12
+ error?: string;
13
+ /** Optional data produced by the step for later inspection. */
14
+ data?: unknown;
15
+ }
@@ -0,0 +1,200 @@
1
+ import { TaskStep } from "./TaskStep.js";
2
+ import { TaskResult } from "./TaskResult.js";
3
+
4
+ /**
5
+ * Define the payload for every possible event in the lifecycle.
6
+ */
7
+ export interface RunnerEventPayloads<TContext> {
8
+ workflowStart: {
9
+ context: TContext;
10
+ steps: TaskStep<TContext>[];
11
+ };
12
+ workflowEnd: {
13
+ context: TContext;
14
+ results: Map<string, TaskResult>;
15
+ };
16
+ taskStart: {
17
+ step: TaskStep<TContext>;
18
+ };
19
+ taskEnd: {
20
+ step: TaskStep<TContext>;
21
+ result: TaskResult;
22
+ };
23
+ taskSkipped: {
24
+ step: TaskStep<TContext>;
25
+ result: TaskResult;
26
+ };
27
+ }
28
+
29
+ /**
30
+ * A generic listener type that maps the event key to its specific payload.
31
+ */
32
+ export type RunnerEventListener<
33
+ TContext,
34
+ K extends keyof RunnerEventPayloads<TContext>,
35
+ > = (data: RunnerEventPayloads<TContext>[K]) => void | Promise<void>;
36
+
37
+ /**
38
+ * Helper type for the listeners map to avoid private access issues in generic contexts.
39
+ */
40
+ type ListenerMap<TContext> = {
41
+ [K in keyof RunnerEventPayloads<TContext>]?: Set<
42
+ RunnerEventListener<TContext, K>
43
+ >;
44
+ };
45
+
46
+ /**
47
+ * The main class that orchestrates the execution of a list of tasks
48
+ * based on their dependencies, with support for parallel execution.
49
+ * @template TContext The shape of the shared context object.
50
+ */
51
+ export class TaskRunner<TContext> {
52
+ private running = new Set<string>();
53
+ private listeners: ListenerMap<TContext> = {};
54
+
55
+ /**
56
+ * @param context The shared context object to be passed to each task.
57
+ */
58
+ constructor(private context: TContext) {}
59
+
60
+ /**
61
+ * Subscribe to an event.
62
+ * @param event The event name.
63
+ * @param callback The callback to execute when the event is emitted.
64
+ */
65
+ public on<K extends keyof RunnerEventPayloads<TContext>>(
66
+ event: K,
67
+ callback: RunnerEventListener<TContext, K>
68
+ ): void {
69
+ if (!this.listeners[event]) {
70
+ // Type assertion needed because TypeScript cannot verify that the generic K
71
+ // matches the specific key in the mapped type during assignment.
72
+ this.listeners[event] = new Set() as unknown as ListenerMap<TContext>[K];
73
+ }
74
+ // Type assertion needed to tell TS that this specific Set matches the callback type
75
+ (this.listeners[event] as Set<RunnerEventListener<TContext, K>>).add(
76
+ callback
77
+ );
78
+ }
79
+
80
+ /**
81
+ * Unsubscribe from an event.
82
+ * @param event The event name.
83
+ * @param callback The callback to remove.
84
+ */
85
+ public off<K extends keyof RunnerEventPayloads<TContext>>(
86
+ event: K,
87
+ callback: RunnerEventListener<TContext, K>
88
+ ): void {
89
+ if (this.listeners[event]) {
90
+ (this.listeners[event] as Set<RunnerEventListener<TContext, K>>).delete(
91
+ callback
92
+ );
93
+ }
94
+ }
95
+
96
+ /**
97
+ * Emit an event to all subscribers.
98
+ * @param event The event name.
99
+ * @param data The payload for the event.
100
+ */
101
+ private emit<K extends keyof RunnerEventPayloads<TContext>>(
102
+ event: K,
103
+ data: RunnerEventPayloads<TContext>[K]
104
+ ): void {
105
+ const listeners = this.listeners[event] as
106
+ | Set<RunnerEventListener<TContext, K>>
107
+ | undefined;
108
+ if (listeners) {
109
+ for (const listener of listeners) {
110
+ try {
111
+ listener(data);
112
+ } catch (error) {
113
+ // Prevent listener errors from bubbling up
114
+ console.error(
115
+ `Error in event listener for ${String(event)}:`,
116
+ error
117
+ );
118
+ }
119
+ }
120
+ }
121
+ }
122
+
123
+ /**
124
+ * Executes a list of tasks, respecting their dependencies and running
125
+ * independent tasks in parallel.
126
+ * @param steps An array of TaskStep objects to be executed.
127
+ * @returns A Promise that resolves to a Map where keys are task names
128
+ * and values are the corresponding TaskResult objects.
129
+ */
130
+ async execute(steps: TaskStep<TContext>[]): Promise<Map<string, TaskResult>> {
131
+ this.emit("workflowStart", { context: this.context, steps });
132
+
133
+ const results = new Map<string, TaskResult>();
134
+
135
+ while (results.size < steps.length) {
136
+ const pendingSteps = steps.filter(
137
+ (step) => !results.has(step.name) && !this.running.has(step.name)
138
+ );
139
+
140
+ const readySteps = pendingSteps.filter((step) => {
141
+ const deps = step.dependencies ?? [];
142
+ return deps.every(
143
+ (dep) => results.has(dep) && results.get(dep)?.status === "success"
144
+ );
145
+ });
146
+
147
+ // Skip tasks with failed dependencies
148
+ for (const step of pendingSteps) {
149
+ if (results.has(step.name)) continue;
150
+ const deps = step.dependencies ?? [];
151
+ const failedDep = deps.find(
152
+ (dep) => results.has(dep) && results.get(dep)?.status !== "success"
153
+ );
154
+ if (failedDep) {
155
+ const result: TaskResult = {
156
+ status: "skipped",
157
+ message: `Skipped due to failed dependency: ${failedDep}`,
158
+ };
159
+ results.set(step.name, result);
160
+ this.emit("taskSkipped", { step, result });
161
+ }
162
+ }
163
+
164
+ if (
165
+ readySteps.length === 0 &&
166
+ this.running.size === 0 &&
167
+ results.size < steps.length
168
+ ) {
169
+ const unrunnableSteps = steps.filter((s) => !results.has(s.name));
170
+ const unrunnableStepNames = unrunnableSteps.map((s) => s.name);
171
+ throw new Error(
172
+ `Circular dependency or missing dependency detected. Unable to run tasks: ${unrunnableStepNames.join(", ")}`
173
+ );
174
+ }
175
+
176
+ await Promise.all(
177
+ readySteps.map(async (step) => {
178
+ this.running.add(step.name);
179
+ this.emit("taskStart", { step });
180
+ try {
181
+ const result = await step.run(this.context);
182
+ results.set(step.name, result);
183
+ } catch (e) {
184
+ results.set(step.name, {
185
+ status: "failure",
186
+ error: e instanceof Error ? e.message : String(e),
187
+ });
188
+ } finally {
189
+ this.running.delete(step.name);
190
+ const result = results.get(step.name)!;
191
+ this.emit("taskEnd", { step, result });
192
+ }
193
+ })
194
+ );
195
+ }
196
+
197
+ this.emit("workflowEnd", { context: this.context, results });
198
+ return results;
199
+ }
200
+ }
@@ -0,0 +1,4 @@
1
+ /**
2
+ * Represents the completion status of a task.
3
+ */
4
+ export type TaskStatus = "success" | "failure" | "skipped";