@runium/core 0.1.1 → 0.2.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 (2) hide show
  1. package/index.js +301 -215
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -1,97 +1,183 @@
1
- import { readFile as x, writeFile as M, mkdir as j } from "node:fs/promises";
2
- import { EventEmitter as v } from "node:events";
3
- import q from "bcx-expression-evaluator";
4
- import K from "ajv/dist/2020.js";
5
- import U from "ajv-keywords";
6
- import { spawn as F } from "node:child_process";
7
- import { createWriteStream as w } from "node:fs";
8
- import { resolve as I, dirname as P } from "node:path";
1
+ import { readFile as V, writeFile as J, mkdir as P } from "node:fs/promises";
2
+ import { exec as B, spawn as H } from "node:child_process";
3
+ import { setTimeout as W } from "node:timers/promises";
4
+ import { promisify as X } from "node:util";
5
+ import { EventEmitter as D } from "node:events";
6
+ import z from "bcx-expression-evaluator";
7
+ import Z from "ajv/dist/2020.js";
8
+ import Q from "ajv-keywords";
9
+ import { createWriteStream as j } from "node:fs";
10
+ import { constants as tt } from "node:os";
11
+ import { resolve as w, dirname as b } from "node:path";
9
12
  class p extends Error {
10
13
  code;
11
14
  payload;
12
- constructor(t, s, r = null) {
13
- super(t), this.code = s, this.payload = r;
15
+ constructor(t, s, i = null) {
16
+ super(t), this.code = s, this.payload = i;
14
17
  }
15
18
  }
16
- function At(e) {
19
+ function xt(e) {
17
20
  return !!e && e instanceof p;
18
21
  }
19
- var Y = ((e) => (e.READ_JSON = "file-read-json", e.WRITE_JSON = "file-write-json", e))(Y || {});
20
- async function _t(e) {
22
+ var et = ((e) => (e.READ_JSON = "file-read-json", e.WRITE_JSON = "file-write-json", e))(et || {});
23
+ async function Mt(e) {
21
24
  try {
22
- const t = await x(e, { encoding: "utf-8" });
25
+ const t = await V(e, { encoding: "utf-8" });
23
26
  return JSON.parse(t);
24
27
  } catch (t) {
25
28
  throw new p(`Can not read JSON file ${e}`, "file-read-json", { path: e, original: t });
26
29
  }
27
30
  }
28
- async function It(e, t) {
31
+ async function Kt(e, t) {
29
32
  try {
30
- await M(e, JSON.stringify(t, null, 2), { encoding: "utf-8" });
33
+ await J(e, JSON.stringify(t, null, 2), { encoding: "utf-8" });
31
34
  } catch (s) {
32
35
  throw new p(`Can not write JSON file ${e}`, "file-write-json", { path: e, data: t, original: s });
33
36
  }
34
37
  }
35
- const V = String.raw`"\$unwrap\((.*)\)"`;
36
- var J = ((e) => (e.MACRO_APPLY_ERROR = "macro-apply-error", e))(J || {});
37
- function $t(e, t) {
38
- const s = Object.keys(t), r = String.raw`\$(${s.join("|")})\(([^()]*(?:\([^()]*\)[^()]*)*)\)`, i = (o) => {
39
- const a = new RegExp(r, "g");
38
+ const st = String.raw`"\$unwrap\((.*)\)"`;
39
+ var rt = ((e) => (e.MACRO_APPLY_ERROR = "macro-apply-error", e))(rt || {});
40
+ function qt(e, t) {
41
+ const s = Object.keys(t), i = String.raw`\$(${s.join("|")})\(([^()]*(?:\([^()]*\)[^()]*)*)\)`, r = (o) => {
42
+ const a = new RegExp(i, "g");
40
43
  let c = o, n = !0;
41
- for (; n; ) n = !1, c = c.replace(a, (u, A, N) => {
44
+ for (; n; ) n = !1, c = c.replace(a, (u, f, F) => {
42
45
  n = !0;
43
- const y = [];
44
- let _ = 0, S = "";
45
- for (const g of N) g === "," && _ === 0 ? (y.push(S.trim()), S = "") : (g === "(" && _++, g === ")" && _--, S += g);
46
- y.push(S.trim());
47
- const G = y.map((g) => g ? i(g) : "");
46
+ const E = [];
47
+ let I = 0, R = "";
48
+ for (const h of F) h === "," && I === 0 ? (E.push(R.trim()), R = "") : (h === "(" && I++, h === ")" && I--, R += h);
49
+ E.push(R.trim());
50
+ const Y = E.map((h) => h ? r(h) : "");
48
51
  try {
49
- return t[A](...G);
50
- } catch (g) {
51
- throw new p(`Can not apply macro "${A}"`, "macro-apply-error", { type: A, args: y, original: g });
52
+ return t[f](...Y);
53
+ } catch (h) {
54
+ throw new p(`Can not apply macro "${f}"`, "macro-apply-error", { type: f, args: E, original: h });
52
55
  }
53
56
  });
54
57
  return c;
55
58
  };
56
59
  return (function(o) {
57
- const a = new RegExp(V, "g");
60
+ const a = new RegExp(st, "g");
58
61
  return o.replace(a, (c, n) => n.trim());
59
- })(s.length ? i(e) : e);
62
+ })(s.length ? r(e) : e);
60
63
  }
61
- var f = ((e) => (e.IMMEDIATE = "immediate", e.DEFERRED = "deferred", e.IGNORE = "ignore", e))(f || {}), d = ((e) => (e.EMIT_EVENT = "emit-event", e.START_TASK = "start-task", e.RESTART_TASK = "restart-task", e.STOP_TASK = "stop-task", e.STOP_PROJECT = "stop-project", e.ENABLE_TRIGGER = "enable-trigger", e.DISABLE_TRIGGER = "disable-trigger", e))(d || {}), T = ((e) => (e.DEFAULT = "default", e))(T || {}), l = ((e) => (e.EVENT = "event", e.TIMEOUT = "timeout", e.INTERVAL = "interval", e))(l || {}), m = ((e) => (e.ALWAYS = "always", e.ON_FAILURE = "on-failure", e))(m || {}), B = ((e) => (e.INCORRECT_DATA = "project-config-incorrect-data", e.TASKS_CIRCULAR_DEPENDENCY = "project-config-tasks-circular-dependency", e.TASK_NOT_EXISTS = "project-config-task-not-exists", e.TRIGGER_NOT_EXISTS = "project-config-trigger-not-exists", e))(B || {});
62
- const H = /* @__PURE__ */ new Set(["start-task", "restart-task", "stop-task"]), W = /* @__PURE__ */ new Set(["enable-trigger", "disable-trigger"]), X = new Set(Object.values(d)), z = new Set(Object.values(l));
63
- function b(e) {
64
- return H.has(e.type);
64
+ const N = X(B);
65
+ async function L(e) {
66
+ return process.platform === "win32" ? G(e) : (async function(t) {
67
+ try {
68
+ const { stdout: s } = await N("ps -eo pid,ppid"), i = s.split(`
69
+ `).slice(1), r = /* @__PURE__ */ new Map();
70
+ for (const o of i) {
71
+ const a = o.trim().split(/\s+/);
72
+ if (a.length >= 2) {
73
+ const c = parseInt(a[0], 10), n = parseInt(a[1], 10);
74
+ isNaN(c) || isNaN(n) || (r.has(n) || r.set(n, []), r.get(n).push(c));
75
+ }
76
+ }
77
+ return x(t, r);
78
+ } catch {
79
+ return { pid: t, children: [], depth: 0 };
80
+ }
81
+ })(e);
65
82
  }
66
- function O(e) {
67
- return W.has(e.type);
83
+ async function G(e) {
84
+ try {
85
+ const { stdout: t } = await N(`wmic process where (ParentProcessId=${e}) get ProcessId`, { windowsHide: !0 }), s = t.split(`
86
+ `).map((r) => r.trim()).filter(Boolean).slice(1).map((r) => parseInt(r, 10)).filter((r) => !isNaN(r)), i = [];
87
+ for (const r of s) {
88
+ const o = await G(r);
89
+ i.push(o);
90
+ }
91
+ return { pid: e, children: i, depth: 0 };
92
+ } catch {
93
+ return { pid: e, children: [], depth: 0 };
94
+ }
68
95
  }
69
- function Z(e) {
70
- return !X.has(e.type);
96
+ function x(e, t) {
97
+ return { pid: e, children: (t.get(e) || []).map((s) => x(s, t)), depth: 0 };
71
98
  }
72
- function Q(e) {
73
- return !z.has(e.type);
99
+ function M(e, t = 0) {
100
+ e.depth = t;
101
+ for (const s of e.children) M(s, t + 1);
74
102
  }
75
- const C = new K({ allowUnionTypes: !0, allErrors: !0, verbose: !0 });
76
- function tt(e) {
103
+ function T(e) {
104
+ try {
105
+ return process.kill(e, 0), !0;
106
+ } catch (t) {
107
+ const s = t;
108
+ return s.code !== "ESRCH" && s.code === "EPERM";
109
+ }
110
+ }
111
+ async function O(e, t = "SIGTERM", s = 1e3) {
112
+ if (!T(e)) return !0;
113
+ try {
114
+ process.kill(e, t);
115
+ } catch (r) {
116
+ const o = r;
117
+ if (o.code === "ESRCH") return !0;
118
+ throw o;
119
+ }
120
+ const i = Date.now();
121
+ for (; Date.now() - i < s; ) {
122
+ if (!T(e)) return !0;
123
+ await W(50);
124
+ }
125
+ return !T(e);
126
+ }
127
+ async function it(e, t = "SIGTERM", s = "SIGKILL", i = 1e3) {
128
+ const r = await L(e);
129
+ M(r);
130
+ const o = (function(a) {
131
+ const c = [], n = (u) => {
132
+ c.push(u);
133
+ for (const f of u.children) n(f);
134
+ };
135
+ return n(a), c.sort((u, f) => f.depth - u.depth);
136
+ })(r);
137
+ for (const a of o)
138
+ T(a.pid) && !await O(a.pid, t, i) && T(a.pid) && await O(a.pid, s, i);
139
+ }
140
+ async function Ut(e) {
141
+ const t = await L(e), s = [], i = (r) => {
142
+ s.push(r.pid);
143
+ for (const o of r.children) i(o);
144
+ };
145
+ return i(t), s;
146
+ }
147
+ var m = ((e) => (e.IMMEDIATE = "immediate", e.DEFERRED = "deferred", e.IGNORE = "ignore", e))(m || {}), d = ((e) => (e.EMIT_EVENT = "emit-event", e.START_TASK = "start-task", e.RESTART_TASK = "restart-task", e.STOP_TASK = "stop-task", e.STOP_PROJECT = "stop-project", e.ENABLE_TRIGGER = "enable-trigger", e.DISABLE_TRIGGER = "disable-trigger", e))(d || {}), y = ((e) => (e.DEFAULT = "default", e))(y || {}), l = ((e) => (e.EVENT = "event", e.TIMEOUT = "timeout", e.INTERVAL = "interval", e))(l || {}), S = ((e) => (e.ALWAYS = "always", e.ON_FAILURE = "on-failure", e))(S || {}), ot = ((e) => (e.INCORRECT_DATA = "project-config-incorrect-data", e.TASKS_CIRCULAR_DEPENDENCY = "project-config-tasks-circular-dependency", e.TASK_NOT_EXISTS = "project-config-task-not-exists", e.TRIGGER_NOT_EXISTS = "project-config-trigger-not-exists", e))(ot || {});
148
+ const nt = /* @__PURE__ */ new Set(["start-task", "restart-task", "stop-task"]), at = /* @__PURE__ */ new Set(["enable-trigger", "disable-trigger"]), ct = new Set(Object.values(d)), pt = new Set(Object.values(l));
149
+ function v(e) {
150
+ return nt.has(e.type);
151
+ }
152
+ function C(e) {
153
+ return at.has(e.type);
154
+ }
155
+ function dt(e) {
156
+ return !ct.has(e.type);
157
+ }
158
+ function ut(e) {
159
+ return !pt.has(e.type);
160
+ }
161
+ const K = new Z({ allowUnionTypes: !0, allErrors: !0, verbose: !0 });
162
+ function ht(e) {
77
163
  const t = /* @__PURE__ */ new Map(), s = /* @__PURE__ */ new Set();
78
- for (const i of e.tasks) s.add(i.id), t.set(i.id, []);
79
- const r = new Set(e.triggers?.map((i) => i.id) || []);
80
- for (const i of e.tasks) {
81
- for (const o of i.dependencies || []) {
82
- if (!s.has(o.taskId)) throw new p(`Task "${i.id}" depends on not existing task "${o.taskId}"`, "project-config-task-not-exists", { taskId: i.id, dependency: { ...o }, scope: "dependencies" });
83
- t.get(o.taskId).push(i.id);
164
+ for (const r of e.tasks) s.add(r.id), t.set(r.id, []);
165
+ const i = new Set(e.triggers?.map((r) => r.id) || []);
166
+ for (const r of e.tasks) {
167
+ for (const o of r.dependencies || []) {
168
+ if (!s.has(o.taskId)) throw new p(`Task "${r.id}" depends on not existing task "${o.taskId}"`, "project-config-task-not-exists", { taskId: r.id, dependency: { ...o }, scope: "dependencies" });
169
+ t.get(o.taskId).push(r.id);
84
170
  }
85
- for (const o of i.handlers || []) {
86
- if (b(o.action) && !s.has(o.action.taskId)) throw new p(`Task "${i.id}" handler action uses not existing task "${o.action.taskId}"`, "project-config-task-not-exists", { taskId: i.id, handler: { ...o }, scope: "handlers" });
87
- if (O(o.action) && !r.has(o.action.triggerId)) throw new p(`Task "${i.id}" handler action uses not existing trigger "${o.action.triggerId}"`, "project-config-trigger-not-exists", { taskId: i.id, handler: { ...o }, scope: "handlers" });
171
+ for (const o of r.handlers || []) {
172
+ if (v(o.action) && !s.has(o.action.taskId)) throw new p(`Task "${r.id}" handler action uses not existing task "${o.action.taskId}"`, "project-config-task-not-exists", { taskId: r.id, handler: { ...o }, scope: "handlers" });
173
+ if (C(o.action) && !i.has(o.action.triggerId)) throw new p(`Task "${r.id}" handler action uses not existing trigger "${o.action.triggerId}"`, "project-config-trigger-not-exists", { taskId: r.id, handler: { ...o }, scope: "handlers" });
88
174
  }
89
175
  }
90
- for (const i of e.triggers || []) {
91
- if (b(i.action) && !s.has(i.action.taskId)) throw new p(`Trigger "${i.id}" action uses not existing task "${i.action.taskId}"`, "project-config-task-not-exists", { trigger: { ...i }, scope: "triggers" });
92
- if (O(i.action) && !r.has(i.action.triggerId)) throw new p(`Trigger "${i.id}" action uses not existing trigger "${i.action.triggerId}"`, "project-config-trigger-not-exists", { trigger: { ...i }, scope: "triggers" });
176
+ for (const r of e.triggers || []) {
177
+ if (v(r.action) && !s.has(r.action.taskId)) throw new p(`Trigger "${r.id}" action uses not existing task "${r.action.taskId}"`, "project-config-task-not-exists", { trigger: { ...r }, scope: "triggers" });
178
+ if (C(r.action) && !i.has(r.action.triggerId)) throw new p(`Trigger "${r.id}" action uses not existing trigger "${r.action.triggerId}"`, "project-config-trigger-not-exists", { trigger: { ...r }, scope: "triggers" });
93
179
  }
94
- (function(i) {
180
+ (function(r) {
95
181
  const o = /* @__PURE__ */ new Set(), a = /* @__PURE__ */ new Set(), c = (n) => {
96
182
  if (a.has(n)) {
97
183
  const u = [...a, n].join(" -> ");
@@ -99,30 +185,30 @@ function tt(e) {
99
185
  }
100
186
  if (!o.has(n)) {
101
187
  a.add(n);
102
- for (const u of i.get(n) || []) c(u);
188
+ for (const u of r.get(n) || []) c(u);
103
189
  a.delete(n), o.add(n);
104
190
  }
105
191
  };
106
- for (const n of i.keys()) o.has(n) || c(n);
192
+ for (const n of r.keys()) o.has(n) || c(n);
107
193
  })(t);
108
194
  }
109
- function et(e, t) {
110
- (function(s, r) {
111
- const i = C.compile(r || {});
112
- if (!i(s)) throw new p("Incorrect project config data", "project-config-incorrect-data", { errors: i.errors });
113
- })(e, t), tt(e);
195
+ function gt(e, t) {
196
+ (function(s, i) {
197
+ const r = K.compile(i || {});
198
+ if (!r(s)) throw new p("Incorrect project config data", "project-config-incorrect-data", { errors: r.errors });
199
+ })(e, t), ht(e);
114
200
  }
115
- U(C, ["uniqueItemProperties"]);
116
- var h = ((e) => (e.IDLE = "idle", e.STARTING = "starting", e.STARTED = "started", e.COMPLETED = "completed", e.FAILED = "failed", e.STOPPING = "stopping", e.STOPPED = "stopped", e))(h || {}), k = ((e) => (e.STATE_CHANGE = "state-change", e.STDOUT = "stdout", e.STDERR = "stderr", e))(k || {});
117
- const jt = -1;
118
- class D extends v {
201
+ Q(K, ["uniqueItemProperties"]);
202
+ var g = ((e) => (e.IDLE = "idle", e.STARTING = "starting", e.STARTED = "started", e.COMPLETED = "completed", e.FAILED = "failed", e.STOPPING = "stopping", e.STOPPED = "stopped", e))(g || {}), k = ((e) => (e.STATE_CHANGE = "state-change", e.STDOUT = "stdout", e.STDERR = "stderr", e))(k || {});
203
+ const Ft = -1;
204
+ class q extends D {
119
205
  constructor(t) {
120
206
  super(), this.options = t;
121
207
  }
122
208
  }
123
- class st extends D {
209
+ class lt extends q {
124
210
  constructor(t) {
125
- super(t), this.options = t, this.setMaxListeners(50), this.options.env = { ...process.env, ...t.env || {} }, this.options.cwd = I(process.cwd(), t.cwd || "");
211
+ super(t), this.options = t, this.setMaxListeners(50), this.options.env = { ...process.env, ...t.env || {} }, this.options.cwd = w(process.cwd(), t.cwd || "");
126
212
  }
127
213
  state = { status: "idle", pid: -1, timestamp: Date.now(), iteration: 0 };
128
214
  process = null;
@@ -144,8 +230,8 @@ class st extends D {
144
230
  this.updateState({ status: "starting", iteration: this.state.iteration + 1, pid: -1, exitCode: void 0, error: void 0 });
145
231
  try {
146
232
  await this.initLogStreams();
147
- const { cwd: t, command: s, arguments: r = [], env: i, shell: o = !0 } = this.options;
148
- this.process = F(s, r, { cwd: t, env: i, shell: o, stdio: ["ignore", "pipe", "pipe"] }), this.addProcessListeners(), this.setTTLTimer(), this.updateState({ status: "started", pid: this.process.pid });
233
+ const { cwd: t, command: s, arguments: i = [], env: r, shell: o = !0 } = this.options;
234
+ this.process = H(s, i, { cwd: t, env: r, shell: o, stdio: ["ignore", "pipe", "pipe"] }), this.addProcessListeners(), this.setTTLTimer(), this.updateState({ status: "started", pid: this.process.pid });
149
235
  } catch (t) {
150
236
  this.onError(t);
151
237
  }
@@ -156,29 +242,27 @@ class st extends D {
156
242
  return !!this.process && t === "started";
157
243
  }
158
244
  async stop(t = "") {
159
- if (this.canStop()) return this.updateState({ status: "stopping", reason: t }), new Promise((s) => {
160
- const r = this.options.stopSignal || "SIGTERM";
161
- this.process.kill(r), this.process.on("exit", () => {
162
- s();
163
- });
164
- });
245
+ if (!this.canStop()) return;
246
+ this.updateState({ status: "stopping", reason: t });
247
+ const s = this.options.stopSignal || "SIGTERM";
248
+ await it(Number(this.process.pid), s, "SIGKILL", 3e4);
165
249
  }
166
250
  async restart() {
167
251
  await this.stop("restart"), this.start();
168
252
  }
169
253
  updateState(t) {
170
254
  const s = { ...this.state, ...t, timestamp: Date.now() };
171
- this.state = Object.fromEntries(Object.entries(s).filter(([r, i]) => i !== void 0)), this.emit("state-change", this.getState()), this.emit(this.state.status, this.getState());
255
+ this.state = Object.fromEntries(Object.entries(s).filter(([i, r]) => r !== void 0)), this.emit("state-change", this.getState()), this.emit(this.state.status, this.getState());
172
256
  }
173
257
  async initLogStreams() {
174
258
  const { stdout: t = null, stderr: s = null } = this.options.log || {};
175
259
  if (t) {
176
- const r = I(t);
177
- await j(P(r), { recursive: !0 }), this.stdoutStream = w(t, { flags: "w" });
260
+ const i = w(t);
261
+ await P(b(i), { recursive: !0 }), this.stdoutStream = j(t, { flags: "w" });
178
262
  }
179
263
  if (s) {
180
- const r = I(s);
181
- await j(P(r), { recursive: !0 }), this.stderrStream = w(s, { flags: "w" });
264
+ const i = w(s);
265
+ await P(b(i), { recursive: !0 }), this.stderrStream = j(s, { flags: "w" });
182
266
  }
183
267
  }
184
268
  setTTLTimer() {
@@ -199,8 +283,8 @@ class st extends D {
199
283
  this.emit("stderr", s), this.stderrStream && this.stderrStream.write(s);
200
284
  }
201
285
  onExit(t) {
202
- const s = t !== null ? t : -1;
203
- this.updateState({ status: s === 0 ? "completed" : s === -1 ? "stopped" : "failed", exitCode: s }), this.cleanup();
286
+ let s = -1;
287
+ t !== null && (s = t === 128 + tt.signals[this.options.stopSignal || "SIGTERM"] ? -1 : t), this.updateState({ status: s === 0 ? "completed" : s === -1 ? "stopped" : "failed", exitCode: s }), this.cleanup();
204
288
  }
205
289
  onError(t) {
206
290
  this.updateState({ status: "failed", error: t }), this.cleanup();
@@ -209,70 +293,70 @@ class st extends D {
209
293
  this.ttlTimer && (clearTimeout(this.ttlTimer), this.ttlTimer = null), this.process && (this.process.removeAllListeners(), this.process.stdout?.removeAllListeners(), this.process.stderr?.removeAllListeners(), this.process = null), this.stdoutStream && (this.stdoutStream.end(), this.stdoutStream = null), this.stderrStream && (this.stderrStream.end(), this.stderrStream = null);
210
294
  }
211
295
  }
212
- var rt = ((e) => (e.ACTION_TYPE_ALREADY_USED = "project-schema-action-type-already-used", e.TASK_TYPE_ALREADY_USED = "project-schema-task-type-already-used", e.TRIGGER_TYPE_ALREADY_USED = "project-schema-trigger-type-already-used", e))(rt || {});
213
- const $ = /^[a-zA-Z0-9_-]+$/, it = { id: { type: "string", pattern: $.source }, name: { type: "string" }, type: { type: "string" }, mode: { $ref: "#/$defs/Runium_TaskStartMode" }, dependencies: { type: "array", items: { $ref: "#/$defs/Runium_TaskDependency" } }, handlers: { type: "array", items: { $ref: "#/$defs/Runium_TaskHandler" } }, restart: { $ref: "#/$defs/Runium_TaskRestartPolicy" } }, E = { id: { type: "string", pattern: $.source }, action: { $ref: "#/$defs/Runium_Action" }, disabled: { type: "boolean" } };
214
- function L(e, t) {
215
- return { type: "object", properties: { ...structuredClone(it), type: { const: e }, options: { ...structuredClone(t) } }, required: ["id", "options"], additionalProperties: !1 };
296
+ var ft = ((e) => (e.ACTION_TYPE_ALREADY_USED = "project-schema-action-type-already-used", e.TASK_TYPE_ALREADY_USED = "project-schema-task-type-already-used", e.TRIGGER_TYPE_ALREADY_USED = "project-schema-trigger-type-already-used", e))(ft || {});
297
+ const $ = /^[a-zA-Z0-9_-]+$/, Tt = { id: { type: "string", pattern: $.source }, name: { type: "string" }, type: { type: "string" }, mode: { $ref: "#/$defs/Runium_TaskStartMode" }, dependencies: { type: "array", items: { $ref: "#/$defs/Runium_TaskDependency" } }, handlers: { type: "array", items: { $ref: "#/$defs/Runium_TaskHandler" } }, restart: { $ref: "#/$defs/Runium_TaskRestartPolicy" } }, A = { id: { type: "string", pattern: $.source }, action: { $ref: "#/$defs/Runium_Action" }, disabled: { type: "boolean" } };
298
+ function U(e, t) {
299
+ return { type: "object", properties: { ...structuredClone(Tt), type: { const: e }, options: { ...structuredClone(t) } }, required: ["id", "options"], additionalProperties: !1 };
216
300
  }
217
- function ot(e, t) {
301
+ function mt(e, t) {
218
302
  return { type: "object", properties: { type: { type: "string", const: e }, ...t ? { payload: t } : {} }, required: ["type", ...t ? ["payload"] : []], additionalProperties: !1 };
219
303
  }
220
- function nt(e, t) {
221
- const s = t ? { payload: t } : {}, r = t ? ["payload"] : [];
222
- return { type: "object", properties: { ...structuredClone(E), type: { type: "string", const: e }, ...s }, required: ["id", "type", "action", ...r], additionalProperties: !1 };
304
+ function yt(e, t) {
305
+ const s = t ? { payload: t } : {}, i = t ? ["payload"] : [];
306
+ return { type: "object", properties: { ...structuredClone(A), type: { type: "string", const: e }, ...s }, required: ["id", "type", "action", ...i], additionalProperties: !1 };
223
307
  }
224
- function at() {
225
- const e = { $schema: "https://json-schema.org/draft/2020-12/schema", $id: "https://example.com/schemas/project.json", title: "Project", type: "object", properties: { id: { type: "string", pattern: $.source }, name: { type: "string" }, tasks: { type: "array", items: { oneOf: [{ $ref: "#/$defs/Runium_TaskConfig" }] }, minItems: 1, uniqueItemProperties: ["id"] }, triggers: { type: "array", items: { $ref: "#/$defs/Runium_Trigger" }, uniqueItemProperties: ["id"] } }, required: ["id", "tasks"], additionalProperties: !1, $defs: { Runium_EnvValue: { type: ["string", "number", "boolean"] }, Runium_Env: { type: "object", additionalProperties: { $ref: "#/$defs/Runium_EnvValue" } }, Runium_TaskStartMode: { type: "string", enum: Object.values(f) }, Runium_TaskHandler: { type: "object", properties: { action: { $ref: "#/$defs/Runium_Action" }, condition: { $ref: "#/$defs/Runium_TaskStateCondition" } }, required: ["action", "condition"], additionalProperties: !1 }, Runium_TaskStateCondition: { oneOf: [{ type: "string" }, { type: "boolean" }, { $ref: "#/$defs/Runium_TaskState" }] }, Runium_TaskState: { type: "object", properties: { status: { $ref: "#/$defs/Runium_TaskStatus" }, iteration: { type: "number", minimum: 0 }, exitCode: { type: "number", minimum: 0 }, reason: { type: "string" } }, additionalProperties: !1 }, Runium_TaskStatus: { type: "string", enum: Object.values(h) }, Runium_TaskDependency: { type: "object", properties: { taskId: { type: "string" }, condition: { $ref: "#/$defs/Runium_TaskStateCondition" } }, required: ["taskId", "condition"], additionalProperties: !1 }, Runium_Trigger: { oneOf: [{ $ref: "#/$defs/Runium_TriggerEvent" }, { $ref: "#/$defs/Runium_TriggerInterval" }, { $ref: "#/$defs/Runium_TriggerTimeout" }] }, Runium_TriggerEvent: { type: "object", properties: { ...structuredClone(E), type: { const: l.EVENT }, event: { type: "string" } }, required: ["id", "type", "event", "action"], additionalProperties: !1 }, Runium_TriggerInterval: { type: "object", properties: { ...structuredClone(E), type: { const: l.INTERVAL }, interval: { type: "number", minimum: 0 } }, required: ["id", "type", "interval", "action"], additionalProperties: !1 }, Runium_TriggerTimeout: { type: "object", properties: { ...structuredClone(E), type: { const: l.TIMEOUT }, timeout: { type: "number", minimum: 0 } }, required: ["id", "type", "timeout", "action"], additionalProperties: !1 }, Runium_Action: { oneOf: [{ $ref: "#/$defs/Runium_ActionEmitEvent" }, { $ref: "#/$defs/Runium_ActionProcessTask" }, { $ref: "#/$defs/Runium_ActionStopProject" }, { $ref: "#/$defs/Runium_ActionToggleTrigger" }] }, Runium_ActionEmitEvent: { type: "object", properties: { type: { type: "string", const: d.EMIT_EVENT }, event: { type: "string" } }, required: ["type", "event"], additionalProperties: !1 }, Runium_ActionProcessTask: { type: "object", properties: { type: { type: "string", enum: [d.START_TASK, d.RESTART_TASK, d.STOP_TASK] }, taskId: { type: "string" } }, required: ["type", "taskId"], additionalProperties: !1 }, Runium_ActionStopProject: { type: "object", properties: { type: { type: "string", const: d.STOP_PROJECT } }, required: ["type"], additionalProperties: !1 }, Runium_ActionToggleTrigger: { type: "object", properties: { type: { type: "string", enum: [d.ENABLE_TRIGGER, d.DISABLE_TRIGGER] }, triggerId: { type: "string" } }, required: ["type", "triggerId"], additionalProperties: !1 }, Runium_TaskConfig: { ...L(T.DEFAULT, { $ref: "#/$defs/Runium_TaskOptions" }) }, Runium_TaskOptions: { type: "object", properties: { command: { type: "string" }, arguments: { type: "array", items: { type: "string" } }, shell: { type: "boolean" }, cwd: { type: "string" }, env: { $ref: "#/$defs/Runium_Env" }, ttl: { type: "number" }, log: { $ref: "#/$defs/Runium_TaskLog" }, stopSignal: { type: "string" } }, required: ["command"], additionalProperties: !1 }, Runium_TaskRestartPolicy: { oneOf: [{ $ref: "#/$defs/Runium_TaskRestartPolicyAlways" }, { $ref: "#/$defs/Runium_TaskRestartPolicyOnFailure" }] }, Runium_TaskRestartPolicyAlways: { type: "object", required: ["policy"], properties: { policy: { const: m.ALWAYS }, delay: { type: "number" } }, additionalProperties: !1 }, Runium_TaskRestartPolicyOnFailure: { type: "object", required: ["policy"], properties: { policy: { const: m.ON_FAILURE }, delay: { type: "number" }, maxRetries: { type: "number" } }, additionalProperties: !1 }, Runium_TaskLog: { type: "object", properties: { stdout: { type: ["string", "null"] }, stderr: { type: ["string", "null"] } }, additionalProperties: !1 } } };
308
+ function St() {
309
+ const e = { $schema: "https://json-schema.org/draft/2020-12/schema", $id: "https://example.com/schemas/project.json", title: "Project", type: "object", properties: { id: { type: "string", pattern: $.source }, name: { type: "string" }, tasks: { type: "array", items: { oneOf: [{ $ref: "#/$defs/Runium_TaskConfig" }] }, minItems: 1, uniqueItemProperties: ["id"] }, triggers: { type: "array", items: { $ref: "#/$defs/Runium_Trigger" }, uniqueItemProperties: ["id"] } }, required: ["id", "tasks"], additionalProperties: !1, $defs: { Runium_EnvValue: { type: ["string", "number", "boolean"] }, Runium_Env: { type: "object", additionalProperties: { $ref: "#/$defs/Runium_EnvValue" } }, Runium_TaskStartMode: { type: "string", enum: Object.values(m) }, Runium_TaskHandler: { type: "object", properties: { action: { $ref: "#/$defs/Runium_Action" }, condition: { $ref: "#/$defs/Runium_TaskStateCondition" } }, required: ["action", "condition"], additionalProperties: !1 }, Runium_TaskStateCondition: { oneOf: [{ type: "string" }, { type: "boolean" }, { $ref: "#/$defs/Runium_TaskState" }] }, Runium_TaskState: { type: "object", properties: { status: { $ref: "#/$defs/Runium_TaskStatus" }, iteration: { type: "number", minimum: 0 }, exitCode: { type: "number", minimum: 0 }, reason: { type: "string" } }, additionalProperties: !1 }, Runium_TaskStatus: { type: "string", enum: Object.values(g) }, Runium_TaskDependency: { type: "object", properties: { taskId: { type: "string" }, condition: { $ref: "#/$defs/Runium_TaskStateCondition" } }, required: ["taskId", "condition"], additionalProperties: !1 }, Runium_Trigger: { oneOf: [{ $ref: "#/$defs/Runium_TriggerEvent" }, { $ref: "#/$defs/Runium_TriggerInterval" }, { $ref: "#/$defs/Runium_TriggerTimeout" }] }, Runium_TriggerEvent: { type: "object", properties: { ...structuredClone(A), type: { const: l.EVENT }, event: { type: "string" } }, required: ["id", "type", "event", "action"], additionalProperties: !1 }, Runium_TriggerInterval: { type: "object", properties: { ...structuredClone(A), type: { const: l.INTERVAL }, interval: { type: "number", minimum: 0 } }, required: ["id", "type", "interval", "action"], additionalProperties: !1 }, Runium_TriggerTimeout: { type: "object", properties: { ...structuredClone(A), type: { const: l.TIMEOUT }, timeout: { type: "number", minimum: 0 } }, required: ["id", "type", "timeout", "action"], additionalProperties: !1 }, Runium_Action: { oneOf: [{ $ref: "#/$defs/Runium_ActionEmitEvent" }, { $ref: "#/$defs/Runium_ActionProcessTask" }, { $ref: "#/$defs/Runium_ActionStopProject" }, { $ref: "#/$defs/Runium_ActionToggleTrigger" }] }, Runium_ActionEmitEvent: { type: "object", properties: { type: { type: "string", const: d.EMIT_EVENT }, event: { type: "string" } }, required: ["type", "event"], additionalProperties: !1 }, Runium_ActionProcessTask: { type: "object", properties: { type: { type: "string", enum: [d.START_TASK, d.RESTART_TASK, d.STOP_TASK] }, taskId: { type: "string" } }, required: ["type", "taskId"], additionalProperties: !1 }, Runium_ActionStopProject: { type: "object", properties: { type: { type: "string", const: d.STOP_PROJECT } }, required: ["type"], additionalProperties: !1 }, Runium_ActionToggleTrigger: { type: "object", properties: { type: { type: "string", enum: [d.ENABLE_TRIGGER, d.DISABLE_TRIGGER] }, triggerId: { type: "string" } }, required: ["type", "triggerId"], additionalProperties: !1 }, Runium_TaskConfig: { ...U(y.DEFAULT, { $ref: "#/$defs/Runium_TaskOptions" }) }, Runium_TaskOptions: { type: "object", properties: { command: { type: "string" }, arguments: { type: "array", items: { type: "string" } }, shell: { type: "boolean" }, cwd: { type: "string" }, env: { $ref: "#/$defs/Runium_Env" }, ttl: { type: "number" }, log: { $ref: "#/$defs/Runium_TaskLog" }, stopSignal: { type: "string" } }, required: ["command"], additionalProperties: !1 }, Runium_TaskRestartPolicy: { oneOf: [{ $ref: "#/$defs/Runium_TaskRestartPolicyAlways" }, { $ref: "#/$defs/Runium_TaskRestartPolicyOnFailure" }] }, Runium_TaskRestartPolicyAlways: { type: "object", required: ["policy"], properties: { policy: { const: S.ALWAYS }, delay: { type: "number" } }, additionalProperties: !1 }, Runium_TaskRestartPolicyOnFailure: { type: "object", required: ["policy"], properties: { policy: { const: S.ON_FAILURE }, delay: { type: "number" }, maxRetries: { type: "number" } }, additionalProperties: !1 }, Runium_TaskLog: { type: "object", properties: { stdout: { type: ["string", "null"] }, stderr: { type: ["string", "null"] } }, additionalProperties: !1 } } };
226
310
  return Object.freeze(e);
227
311
  }
228
- function ct(e, t) {
312
+ function Et(e, t) {
229
313
  let s = structuredClone(e);
230
- return t.project && (s = (function(r, i) {
231
- return i && (r.properties = { ...i.properties || {}, ...r.properties }, r.required = Array.from(/* @__PURE__ */ new Set([...r.required ?? [], ...i.required ?? []]))), r;
232
- })(s, t.project)), t.definitions && (s = (function(r, i) {
233
- return i && (r.$defs = { ...i, ...r.$defs }), r;
234
- })(s, t.definitions)), t.tasks && (s = (function(r, i) {
235
- if (i) {
236
- const o = new Set(r.properties.tasks.items.oneOf.map((c) => {
314
+ return t.project && (s = (function(i, r) {
315
+ return r && (i.properties = { ...r.properties || {}, ...i.properties }, i.required = Array.from(/* @__PURE__ */ new Set([...i.required ?? [], ...r.required ?? []]))), i;
316
+ })(s, t.project)), t.definitions && (s = (function(i, r) {
317
+ return r && (i.$defs = { ...r, ...i.$defs }), i;
318
+ })(s, t.definitions)), t.tasks && (s = (function(i, r) {
319
+ if (r) {
320
+ const o = new Set(i.properties.tasks.items.oneOf.map((c) => {
237
321
  const n = c.$ref.split("/").pop() || "";
238
- return r.$defs[n]?.properties?.type?.const || T.DEFAULT;
322
+ return i.$defs[n]?.properties?.type?.const || y.DEFAULT;
239
323
  })), a = {};
240
- for (const [c, n] of Object.entries(i)) {
324
+ for (const [c, n] of Object.entries(r)) {
241
325
  if (o.has(n.type)) throw new p(`Task type "${n.type}" already used in project schema`, "project-schema-task-type-already-used", { type: n.type });
242
- a[c] = L(n.type, n.options), r.properties.tasks.items.oneOf.push({ $ref: `#/$defs/${c}` }), o.add(n.type);
326
+ a[c] = U(n.type, n.options), i.properties.tasks.items.oneOf.push({ $ref: `#/$defs/${c}` }), o.add(n.type);
243
327
  }
244
- r.$defs = { ...a, ...r.$defs };
328
+ i.$defs = { ...a, ...i.$defs };
245
329
  }
246
- return r;
247
- })(s, t.tasks)), t.actions && (s = (function(r, i) {
248
- if (i) {
249
- const o = new Set(r.$defs.Runium_Action.oneOf.map((c) => {
250
- const n = c.$ref.split("/").pop() || "", u = r.$defs[n]?.properties?.type;
330
+ return i;
331
+ })(s, t.tasks)), t.actions && (s = (function(i, r) {
332
+ if (r) {
333
+ const o = new Set(i.$defs.Runium_Action.oneOf.map((c) => {
334
+ const n = c.$ref.split("/").pop() || "", u = i.$defs[n]?.properties?.type;
251
335
  return u?.enum || [u?.const];
252
336
  }).flat()), a = {};
253
- for (const [c, n] of Object.entries(i)) {
337
+ for (const [c, n] of Object.entries(r)) {
254
338
  if (o.has(n.type)) throw new p(`Action type "${n.type}" already used in project schema`, "project-schema-action-type-already-used", { type: n.type });
255
- a[c] = ot(n.type, n.payload), r.$defs.Runium_Action.oneOf.push({ $ref: `#/$defs/${c}` }), o.add(n.type);
339
+ a[c] = mt(n.type, n.payload), i.$defs.Runium_Action.oneOf.push({ $ref: `#/$defs/${c}` }), o.add(n.type);
256
340
  }
257
- r.$defs = { ...a, ...r.$defs };
341
+ i.$defs = { ...a, ...i.$defs };
258
342
  }
259
- return r;
260
- })(s, t.actions)), t.triggers && (s = (function(r, i) {
261
- if (i) {
262
- const o = new Set(r.$defs.Runium_Trigger.oneOf.map((c) => {
343
+ return i;
344
+ })(s, t.actions)), t.triggers && (s = (function(i, r) {
345
+ if (r) {
346
+ const o = new Set(i.$defs.Runium_Trigger.oneOf.map((c) => {
263
347
  const n = c.$ref.split("/").pop() || "";
264
- return r.$defs[n]?.properties?.type?.const;
348
+ return i.$defs[n]?.properties?.type?.const;
265
349
  })), a = {};
266
- for (const [c, n] of Object.entries(i)) {
350
+ for (const [c, n] of Object.entries(r)) {
267
351
  if (o.has(n.type)) throw new p(`Trigger type "${n.type}" already used in project schema`, "project-schema-trigger-type-already-used", { type: n.type });
268
- a[c] = nt(n.type, n.payload), r.$defs.Runium_Trigger.oneOf.push({ $ref: `#/$defs/${c}` }), o.add(n.type);
352
+ a[c] = yt(n.type, n.payload), i.$defs.Runium_Trigger.oneOf.push({ $ref: `#/$defs/${c}` }), o.add(n.type);
269
353
  }
270
- r.$defs = { ...a, ...r.$defs };
354
+ i.$defs = { ...a, ...i.$defs };
271
355
  }
272
- return r;
356
+ return i;
273
357
  })(s, t.triggers)), Object.freeze(s);
274
358
  }
275
- class R {
359
+ class _ {
276
360
  project;
277
361
  id;
278
362
  action;
@@ -287,7 +371,7 @@ class R {
287
371
  return this.disabled;
288
372
  }
289
373
  }
290
- class pt extends R {
374
+ class Rt extends _ {
291
375
  event;
292
376
  constructor(t, s) {
293
377
  super(t, s), this.event = t.event;
@@ -302,7 +386,7 @@ class pt extends R {
302
386
  this.project.processAction(this.action);
303
387
  };
304
388
  }
305
- class dt extends R {
389
+ class kt extends _ {
306
390
  interval;
307
391
  intervalId = null;
308
392
  constructor(t, s) {
@@ -317,7 +401,7 @@ class dt extends R {
317
401
  this.intervalId && clearInterval(this.intervalId), this.disabled = !0, this.intervalId = null;
318
402
  }
319
403
  }
320
- class ut extends R {
404
+ class At extends _ {
321
405
  timeout;
322
406
  timeoutId = null;
323
407
  constructor(t, s) {
@@ -332,20 +416,20 @@ class ut extends R {
332
416
  this.timeoutId && clearTimeout(this.timeoutId), this.disabled = !0, this.timeoutId = null;
333
417
  }
334
418
  }
335
- var gt = ((e) => (e.STATE_CHANGE = "state-change", e.START_TASK = "start-task", e.RESTART_TASK = "restart-task", e.STOP_TASK = "stop-task", e.PROCESS_ACTION = "process-action", e.ENABLE_TRIGGER = "enable-trigger", e.DISABLE_TRIGGER = "disable-trigger", e.TASK_STATE_CHANGE = "task-state-change", e.TASK_STDOUT = "task-stdout", e.TASK_STDERR = "task-stderr", e))(gt || {}), ht = ((e) => (e.IDLE = "idle", e.STARTING = "starting", e.STARTED = "started", e.STOPPING = "stopping", e.STOPPED = "stopped", e))(ht || {}), lt = ((e) => (e.ACTION_PROCESSOR_ALREADY_REGISTERED = "project-action-processor-already-registered", e.ACTION_PROCESSOR_INCORRECT = "project-action-processor-incorrect", e.TASK_PROCESSOR_NOT_FOUND = "project-task-processor-not-found", e.TASK_PROCESSOR_ALREADY_REGISTERED = "project-task-processor-already-registered", e.TASK_PROCESSOR_INCORRECT = "project-task-processor-incorrect", e.TRIGGER_PROCESSOR_ALREADY_REGISTERED = "project-trigger-processor-already-registered", e.TRIGGER_PROCESSOR_INCORRECT = "project-trigger-processor-incorrect", e))(lt || {});
336
- class wt extends v {
419
+ var _t = ((e) => (e.STATE_CHANGE = "state-change", e.START_TASK = "start-task", e.RESTART_TASK = "restart-task", e.STOP_TASK = "stop-task", e.PROCESS_ACTION = "process-action", e.ENABLE_TRIGGER = "enable-trigger", e.DISABLE_TRIGGER = "disable-trigger", e.TASK_STATE_CHANGE = "task-state-change", e.TASK_STDOUT = "task-stdout", e.TASK_STDERR = "task-stderr", e))(_t || {}), It = ((e) => (e.IDLE = "idle", e.STARTING = "starting", e.STARTED = "started", e.STOPPING = "stopping", e.STOPPED = "stopped", e))(It || {}), wt = ((e) => (e.ACTION_PROCESSOR_ALREADY_REGISTERED = "project-action-processor-already-registered", e.ACTION_PROCESSOR_INCORRECT = "project-action-processor-incorrect", e.TASK_PROCESSOR_NOT_FOUND = "project-task-processor-not-found", e.TASK_PROCESSOR_ALREADY_REGISTERED = "project-task-processor-already-registered", e.TASK_PROCESSOR_INCORRECT = "project-task-processor-incorrect", e.TRIGGER_PROCESSOR_ALREADY_REGISTERED = "project-trigger-processor-already-registered", e.TRIGGER_PROCESSOR_INCORRECT = "project-trigger-processor-incorrect", e))(wt || {});
420
+ class Yt extends D {
337
421
  constructor(t) {
338
422
  super(), this.config = t;
339
423
  }
340
- schema = at();
341
- taskProcessors = /* @__PURE__ */ new Map([[T.DEFAULT, st]]);
424
+ schema = St();
425
+ taskProcessors = /* @__PURE__ */ new Map([[y.DEFAULT, lt]]);
342
426
  actionProcessors = /* @__PURE__ */ new Map();
343
427
  triggerProcessors = /* @__PURE__ */ new Map();
344
428
  tasks = /* @__PURE__ */ new Map();
345
429
  triggers = /* @__PURE__ */ new Map();
346
430
  state = { timestamp: Date.now(), status: "idle" };
347
431
  validate() {
348
- et(this.config, this.schema);
432
+ gt(this.config, this.schema);
349
433
  }
350
434
  getConfig() {
351
435
  return { ...this.config };
@@ -361,23 +445,23 @@ class wt extends v {
361
445
  this.validate(), this.initTasks(), this.initTriggers(), this.updateState({ status: "starting" });
362
446
  const t = this.getTasksStartOrder();
363
447
  for (const s of t) if (this.tasks.has(s)) {
364
- const { instance: r, config: i } = this.tasks.get(s), { mode: o = f.IMMEDIATE, dependencies: a = [] } = i;
365
- o === f.IMMEDIATE && a.length === 0 && r.start();
448
+ const { instance: i, config: r } = this.tasks.get(s), { mode: o = m.IMMEDIATE, dependencies: a = [] } = r;
449
+ o === m.IMMEDIATE && a.length === 0 && i.start();
366
450
  }
367
451
  this.updateState({ status: "started" });
368
452
  }
369
453
  async stop(t = "") {
370
454
  if (this.state.status !== "started" && this.state.status !== "starting") return;
371
455
  this.updateState({ status: "stopping", reason: t }), this.cleanupTriggers();
372
- const s = this.getTasksStartOrder().reverse(), r = [];
373
- for (const i of s) if (this.tasks.has(i)) {
374
- const { instance: o } = this.tasks.get(i), { status: a } = o.getState();
375
- a === h.STARTED && r.push(o.stop("project-stop"));
456
+ const s = this.getTasksStartOrder().reverse(), i = [];
457
+ for (const r of s) if (this.tasks.has(r)) {
458
+ const { instance: o } = this.tasks.get(r), { status: a } = o.getState();
459
+ a === g.STARTED && i.push(o.stop("project-stop"));
376
460
  }
377
- await Promise.allSettled(r), this.updateState({ status: "stopped" });
461
+ await Promise.allSettled(i), this.updateState({ status: "stopped" });
378
462
  }
379
463
  extendValidationSchema(t) {
380
- this.schema = ct(this.schema, t);
464
+ this.schema = Et(this.schema, t);
381
465
  }
382
466
  registerAction(t, s) {
383
467
  if (this.actionProcessors.has(t)) throw new p(`Action processor for type "${t}" already registered`, "project-action-processor-already-registered", { type: t });
@@ -386,12 +470,12 @@ class wt extends v {
386
470
  }
387
471
  registerTask(t, s) {
388
472
  if (this.taskProcessors.has(t)) throw new p(`Task processor for type "${t}" already registered`, "project-task-processor-already-registered", { type: t });
389
- if (!(s.prototype instanceof D)) throw new p(`Task processor for type "${t}" must be a subclass of "RuniumTask"`, "project-task-processor-incorrect", { type: t });
473
+ if (!(s.prototype instanceof q)) throw new p(`Task processor for type "${t}" must be a subclass of "RuniumTask"`, "project-task-processor-incorrect", { type: t });
390
474
  this.taskProcessors.set(t, s);
391
475
  }
392
476
  registerTrigger(t, s) {
393
477
  if (this.triggerProcessors.has(t)) throw new p(`Trigger processor for type "${t}" already registered`, "project-trigger-processor-already-registered", { type: t });
394
- if (!(s.prototype instanceof R)) throw new p(`Trigger processor for type "${t}" must be a subclass of "RuniumTrigger"`, "project-trigger-processor-incorrect", { type: t });
478
+ if (!(s.prototype instanceof _)) throw new p(`Trigger processor for type "${t}" must be a subclass of "RuniumTrigger"`, "project-trigger-processor-incorrect", { type: t });
395
479
  this.triggerProcessors.set(t, s);
396
480
  }
397
481
  updateState(t) {
@@ -400,59 +484,59 @@ class wt extends v {
400
484
  initTasks() {
401
485
  this.tasks.clear();
402
486
  for (const t of this.config.tasks) {
403
- const s = t.type || T.DEFAULT, r = this.taskProcessors.get(s);
404
- if (!r) throw new p(`Task processor for type "${s}" not found`, "project-task-processor-not-found", { type: s });
405
- const i = new r(t.options);
406
- i.on(k.STATE_CHANGE, (o) => {
487
+ const s = t.type || y.DEFAULT, i = this.taskProcessors.get(s);
488
+ if (!i) throw new p(`Task processor for type "${s}" not found`, "project-task-processor-not-found", { type: s });
489
+ const r = new i(t.options);
490
+ r.on(k.STATE_CHANGE, (o) => {
407
491
  this.emit("task-state-change", t.id, o), this.onTaskStateChange(t.id, o);
408
- }), i.on(k.STDOUT, (o) => {
492
+ }), r.on(k.STDOUT, (o) => {
409
493
  this.emit("task-stdout", t.id, o);
410
- }), i.on(k.STDERR, (o) => {
494
+ }), r.on(k.STDERR, (o) => {
411
495
  this.emit("task-stderr", t.id, o);
412
- }), this.tasks.set(t.id, { instance: i, config: t, dependencies: [...t.dependencies || []], dependents: null });
496
+ }), this.tasks.set(t.id, { instance: r, config: t, dependencies: [...t.dependencies || []], dependents: null });
413
497
  }
414
498
  }
415
499
  getTasksStartOrder() {
416
- const t = [], s = /* @__PURE__ */ new Set(), r = (i) => {
417
- if (s.has(i)) return;
418
- s.add(i);
419
- const { dependencies: o = [] } = this.tasks.get(i) || {};
420
- for (const a of o) this.tasks.has(a.taskId) && r(a.taskId);
421
- t.push(i);
500
+ const t = [], s = /* @__PURE__ */ new Set(), i = (r) => {
501
+ if (s.has(r)) return;
502
+ s.add(r);
503
+ const { dependencies: o = [] } = this.tasks.get(r) || {};
504
+ for (const a of o) this.tasks.has(a.taskId) && i(a.taskId);
505
+ t.push(r);
422
506
  };
423
- for (const i of this.tasks.keys()) r(i);
507
+ for (const r of this.tasks.keys()) i(r);
424
508
  return t;
425
509
  }
426
510
  onTaskStateChange(t, s) {
427
- const { config: r, instance: i } = this.tasks.get(t), o = this.getDependentTasks(t);
511
+ const { config: i, instance: r } = this.tasks.get(t), o = this.getDependentTasks(t);
428
512
  for (const a of o) this.isDependentTaskReady(a) && this.startTask(a);
429
- if (s.status === h.COMPLETED || s.status === h.FAILED) {
430
- const { restart: a } = r;
513
+ if (s.status === g.COMPLETED || s.status === g.FAILED) {
514
+ const { restart: a } = i;
431
515
  if (a && s.exitCode !== -1) {
432
516
  const { policy: c } = a;
433
- if (c === m.ALWAYS || c === m.ON_FAILURE && s.exitCode !== 0) {
517
+ if (c === S.ALWAYS || c === S.ON_FAILURE && s.exitCode !== 0) {
434
518
  const { maxRetries: n = 1 / 0, delay: u = 0 } = a;
435
- s.iteration <= n && setTimeout(i.restart.bind(i), u);
519
+ s.iteration <= n && setTimeout(r.restart.bind(r), u);
436
520
  }
437
521
  }
438
522
  }
439
- (r.handlers || []).forEach((a) => {
523
+ (i.handlers || []).forEach((a) => {
440
524
  this.checkTaskStateCondition(a.condition, s) && this.processAction(a.action);
441
525
  });
442
526
  }
443
527
  checkTaskStateCondition(t, s) {
444
- return typeof t == "string" ? q.evaluate(t, s) === !0 : typeof t == "object" ? Object.entries(t).every(([r, i]) => s[r] === i) : typeof t == "boolean" && t;
528
+ return typeof t == "string" ? z.evaluate(t, s) === !0 : typeof t == "object" ? Object.entries(t).every(([i, r]) => s[i] === r) : typeof t == "boolean" && t;
445
529
  }
446
530
  async startTask(t) {
447
531
  if (this.tasks.has(t)) {
448
- const { instance: s } = this.tasks.get(t), { status: r } = s.getState();
449
- if (r !== h.STARTED && r !== h.STARTING && r !== h.STOPPING) return this.emit("start-task", t), s.start();
532
+ const { instance: s } = this.tasks.get(t), { status: i } = s.getState();
533
+ if (i !== g.STARTED && i !== g.STARTING && i !== g.STOPPING) return this.emit("start-task", t), s.start();
450
534
  }
451
535
  }
452
536
  async stopTask(t) {
453
537
  if (this.tasks.has(t)) {
454
- const { instance: s } = this.tasks.get(t), { status: r } = s.getState();
455
- if (r === h.STARTED) return this.emit("stop-task", t), s.stop("action-stop");
538
+ const { instance: s } = this.tasks.get(t), { status: i } = s.getState();
539
+ if (i === g.STARTED) return this.emit("stop-task", t), s.stop("action-stop");
456
540
  }
457
541
  }
458
542
  async restartTask(t) {
@@ -470,10 +554,10 @@ class wt extends v {
470
554
  }
471
555
  isDependentTaskReady(t) {
472
556
  if (this.tasks.has(t)) {
473
- const { dependencies: s = [], config: r } = this.tasks.get(t);
474
- if (r.mode === f.IGNORE) return !1;
475
- for (const { taskId: i, condition: o } of s) {
476
- const { instance: a } = this.tasks.get(i);
557
+ const { dependencies: s = [], config: i } = this.tasks.get(t);
558
+ if (i.mode === m.IGNORE) return !1;
559
+ for (const { taskId: r, condition: o } of s) {
560
+ const { instance: a } = this.tasks.get(r);
477
561
  if (!this.checkTaskStateCondition(o, a.getState())) return !1;
478
562
  }
479
563
  return !0;
@@ -483,17 +567,17 @@ class wt extends v {
483
567
  getDependentTasks(t) {
484
568
  let s = [];
485
569
  if (this.tasks.has(t)) {
486
- const { dependents: r } = this.tasks.get(t);
487
- if (r) s = r;
570
+ const { dependents: i } = this.tasks.get(t);
571
+ if (i) s = i;
488
572
  else {
489
- for (const [i, { dependencies: o }] of this.tasks) for (const a of o) a.taskId === t && s.push(i);
573
+ for (const [r, { dependencies: o }] of this.tasks) for (const a of o) a.taskId === t && s.push(r);
490
574
  this.tasks.get(t).dependents = s;
491
575
  }
492
576
  }
493
577
  return s;
494
578
  }
495
579
  processAction(t) {
496
- if (this.emit("process-action", t), Z(t)) {
580
+ if (this.emit("process-action", t), dt(t)) {
497
581
  const s = this.actionProcessors.get(t.type);
498
582
  s && s(t.payload || {});
499
583
  } else switch (t.type) {
@@ -522,21 +606,21 @@ class wt extends v {
522
606
  initTriggers() {
523
607
  const t = { processAction: this.processAction.bind(this), on: this.on.bind(this), off: this.off.bind(this) };
524
608
  let s = null;
525
- for (const r of this.config.triggers || []) {
526
- if (Q(r)) {
527
- const i = this.triggerProcessors.get(r.type);
528
- i && (s = new i(r, t));
529
- } else switch (r.type) {
609
+ for (const i of this.config.triggers || []) {
610
+ if (ut(i)) {
611
+ const r = this.triggerProcessors.get(i.type);
612
+ r && (s = new r(i, t));
613
+ } else switch (i.type) {
530
614
  case l.EVENT:
531
- s = new pt(r, t);
615
+ s = new Rt(i, t);
532
616
  break;
533
617
  case l.INTERVAL:
534
- s = new dt(r, t);
618
+ s = new kt(i, t);
535
619
  break;
536
620
  case l.TIMEOUT:
537
- s = new ut(r, t);
621
+ s = new At(i, t);
538
622
  }
539
- s && (this.triggers.set(r.id, s), r.disabled !== !0 && s.enable());
623
+ s && (this.triggers.set(i.id, s), i.disabled !== !0 && s.enable());
540
624
  }
541
625
  }
542
626
  cleanupTriggers() {
@@ -556,39 +640,41 @@ class wt extends v {
556
640
  }
557
641
  }
558
642
  export {
559
- pt as EventTrigger,
560
- Y as FileErrorCode,
643
+ Rt as EventTrigger,
644
+ et as FileErrorCode,
561
645
  $ as ID_REGEX,
562
- dt as IntervalTrigger,
563
- J as MacrosErrorCode,
564
- wt as Project,
646
+ kt as IntervalTrigger,
647
+ rt as MacrosErrorCode,
648
+ Yt as Project,
565
649
  d as ProjectActionType,
566
- B as ProjectConfigErrorCode,
567
- lt as ProjectErrorCode,
568
- gt as ProjectEvent,
569
- rt as ProjectSchemaErrorCode,
570
- ht as ProjectStatus,
571
- m as ProjectTaskRestartPolicyType,
572
- f as ProjectTaskStartMode,
573
- T as ProjectTaskType,
650
+ ot as ProjectConfigErrorCode,
651
+ wt as ProjectErrorCode,
652
+ _t as ProjectEvent,
653
+ ft as ProjectSchemaErrorCode,
654
+ It as ProjectStatus,
655
+ S as ProjectTaskRestartPolicyType,
656
+ m as ProjectTaskStartMode,
657
+ y as ProjectTaskType,
574
658
  l as ProjectTriggerType,
575
659
  p as RuniumError,
576
- D as RuniumTask,
577
- R as RuniumTrigger,
578
- jt as SILENT_EXIT_CODE,
579
- st as Task,
660
+ q as RuniumTask,
661
+ _ as RuniumTrigger,
662
+ Ft as SILENT_EXIT_CODE,
663
+ lt as Task,
580
664
  k as TaskEvent,
581
- h as TaskStatus,
582
- ut as TimeoutTrigger,
583
- $t as applyMacros,
584
- ct as extendProjectSchema,
585
- at as getProjectSchema,
586
- Z as isCustomAction,
587
- Q as isCustomTrigger,
588
- b as isProcessTaskAction,
589
- At as isRuniumError,
590
- O as isToggleTriggerAction,
591
- _t as readJsonFile,
592
- et as validateProject,
593
- It as writeJsonFile
665
+ g as TaskStatus,
666
+ At as TimeoutTrigger,
667
+ qt as applyMacros,
668
+ Et as extendProjectSchema,
669
+ Ut as getProcessTreePids,
670
+ St as getProjectSchema,
671
+ dt as isCustomAction,
672
+ ut as isCustomTrigger,
673
+ v as isProcessTaskAction,
674
+ xt as isRuniumError,
675
+ C as isToggleTriggerAction,
676
+ it as killProcessTree,
677
+ Mt as readJsonFile,
678
+ gt as validateProject,
679
+ Kt as writeJsonFile
594
680
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@runium/core",
3
- "version": "0.1.1",
3
+ "version": "0.2.0",
4
4
  "description": "Runium Core",
5
5
  "author": "TheBeastApp",
6
6
  "license": "MIT",