@runium/core 0.0.1

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 (3) hide show
  1. package/index.d.ts +387 -0
  2. package/index.js +571 -0
  3. package/package.json +26 -0
package/index.d.ts ADDED
@@ -0,0 +1,387 @@
1
+ import { EventEmitter } from 'node:events';
2
+
3
+ export declare function applyMacros(text: string, macros: MacrosCollection): string;
4
+
5
+ export declare class EventTrigger extends RuniumTrigger<EventTriggerOptions> {
6
+ constructor(options: EventTriggerOptions, project: RuniumTriggerProjectAccessible);
7
+ enable(): void;
8
+ disable(): void;
9
+ }
10
+
11
+ export declare interface EventTriggerOptions extends RuniumTriggerOptions {
12
+ event: string;
13
+ }
14
+
15
+ export declare function extendProjectSchema(schema: object, extensions: ProjectSchemaExtension): object;
16
+
17
+ export declare enum FileErrorCode {
18
+ READ_JSON = "file-read-json",
19
+ WRITE_JSON = "file-write-json"
20
+ }
21
+
22
+ export declare function getProjectSchema(): object;
23
+
24
+ export declare class IntervalTrigger extends RuniumTrigger<IntervalTriggerOptions> {
25
+ constructor(options: IntervalTriggerOptions, project: RuniumTriggerProjectAccessible);
26
+ enable(): void;
27
+ disable(): void;
28
+ }
29
+
30
+ export declare interface IntervalTriggerOptions extends RuniumTriggerOptions {
31
+ interval: number;
32
+ }
33
+
34
+ export declare function isCustomAction(action: ProjectAction): action is ProjectCustomAction;
35
+
36
+ export declare function isCustomTrigger(trigger: ProjectTrigger): trigger is ProjectCustomTrigger;
37
+
38
+ export declare function isProcessTaskAction(action: ProjectAction): action is ProjectActionProcessTask;
39
+
40
+ export declare function isRuniumError(error: unknown): boolean;
41
+
42
+ export declare function isToggleTriggerAction(action: ProjectAction): action is ProjectActionToggleTrigger;
43
+
44
+ export declare interface JSONObject {
45
+ [x: string]: JSONValue;
46
+ }
47
+
48
+ export declare type JSONValue = string | number | boolean | JSONObject | JSONValue[];
49
+
50
+ export declare type Macro = (...params: string[]) => string;
51
+
52
+ export declare type MacrosCollection = Record<string, Macro>;
53
+
54
+ export declare class Project extends EventEmitter {
55
+ constructor(config: ProjectConfig);
56
+ validate(): void;
57
+ getConfig(): ProjectConfig;
58
+ setConfig(config: ProjectConfig): void;
59
+ getState(): ProjectState;
60
+ start(): Promise<void>;
61
+ stop(): Promise<void>;
62
+ extendValidationSchema(extensions: ProjectSchemaExtension): void;
63
+ registerAction(type: string, processor: RuniumActionProcessor): void;
64
+ registerTask(type: string, processor: RuniumTaskConstructor<unknown, any>): void;
65
+ registerTrigger(type: string, processor: RuniumTriggerConstructor<any>): void;
66
+ }
67
+
68
+ export declare type ProjectAction = ProjectCustomAction | ProjectActionEmitEvent | ProjectActionProcessTask | ProjectActionStopProject | ProjectActionToggleTrigger;
69
+
70
+ export declare interface ProjectActionEmitEvent {
71
+ type: ProjectActionType.EMIT_EVENT;
72
+ event: string;
73
+ }
74
+
75
+ export declare interface ProjectActionProcessTask {
76
+ type: ProjectActionType.START_TASK | ProjectActionType.RESTART_TASK | ProjectActionType.STOP_TASK;
77
+ taskId: string;
78
+ }
79
+
80
+ export declare interface ProjectActionStopProject {
81
+ type: ProjectActionType.STOP_PROJECT;
82
+ }
83
+
84
+ export declare interface ProjectActionToggleTrigger {
85
+ type: ProjectActionType.ENABLE_TRIGGER | ProjectActionType.DISABLE_TRIGGER;
86
+ triggerId: string;
87
+ }
88
+
89
+ export declare enum ProjectActionType {
90
+ EMIT_EVENT = "emit-event",
91
+ START_TASK = "start-task",
92
+ RESTART_TASK = "restart-task",
93
+ STOP_TASK = "stop-task",
94
+ STOP_PROJECT = "stop-project",
95
+ ENABLE_TRIGGER = "enable-trigger",
96
+ DISABLE_TRIGGER = "disable-trigger"
97
+ }
98
+
99
+ export declare interface ProjectConfig {
100
+ id: string;
101
+ name?: string;
102
+ tasks: ProjectTaskConfig[];
103
+ triggers?: ProjectTrigger[];
104
+ }
105
+
106
+ export declare enum ProjectConfigErrorCode {
107
+ INCORRECT_DATA = "project-config-incorrect-data",
108
+ TASKS_CIRCULAR_DEPENDENCY = "project-config-tasks-circular-dependency",
109
+ TASK_NOT_EXISTS = "project-config-task-not-exists",
110
+ TRIGGER_NOT_EXISTS = "project-config-trigger-not-exists"
111
+ }
112
+
113
+ export declare interface ProjectCustomAction {
114
+ type: string;
115
+ payload?: unknown;
116
+ }
117
+
118
+ export declare interface ProjectCustomTrigger extends ProjectTriggerBase {
119
+ type: string;
120
+ payload?: unknown;
121
+ }
122
+
123
+ export declare interface ProjectDefaultTaskConfig extends ProjectTaskConfig<TaskOptions> {
124
+ type?: ProjectTaskType.DEFAULT;
125
+ }
126
+
127
+ export declare enum ProjectErrorCode {
128
+ ACTION_PROCESSOR_ALREADY_REGISTERED = "project-action-processor-already-registered",
129
+ ACTION_PROCESSOR_INCORRECT = "project-action-processor-incorrect",
130
+ TASK_PROCESSOR_NOT_FOUND = "project-task-processor-not-found",
131
+ TASK_PROCESSOR_ALREADY_REGISTERED = "project-task-processor-already-registered",
132
+ TASK_PROCESSOR_INCORRECT = "project-task-processor-incorrect",
133
+ TRIGGER_PROCESSOR_ALREADY_REGISTERED = "project-trigger-processor-already-registered",
134
+ TRIGGER_PROCESSOR_INCORRECT = "project-trigger-processor-incorrect"
135
+ }
136
+
137
+ export declare enum ProjectEvent {
138
+ STATE_CHANGE = "state-change",
139
+ START_TASK = "start-task",
140
+ RESTART_TASK = "restart-task",
141
+ STOP_TASK = "stop-task",
142
+ PROCESS_ACTION = "process-action",
143
+ ENABLE_TRIGGER = "enable-trigger",
144
+ DISABLE_TRIGGER = "disable-trigger",
145
+ TASK_STATE_CHANGE = "task-state-change",
146
+ TASK_STDOUT = "task-stdout",
147
+ TASK_STDERR = "task-stderr"
148
+ }
149
+
150
+ export declare enum ProjectSchemaErrorCode {
151
+ ACTION_TYPE_ALREADY_USED = "project-schema-action-type-already-used",
152
+ TASK_TYPE_ALREADY_USED = "project-schema-task-type-already-used",
153
+ TRIGGER_TYPE_ALREADY_USED = "project-schema-trigger-type-already-used"
154
+ }
155
+
156
+ export declare interface ProjectSchemaExtension {
157
+ project?: {
158
+ properties: unknown;
159
+ required?: string[];
160
+ };
161
+ tasks?: Record<string, {
162
+ type: string;
163
+ options: unknown;
164
+ }>;
165
+ definitions?: Record<string, unknown>;
166
+ actions?: Record<string, {
167
+ type: string;
168
+ payload?: unknown;
169
+ }>;
170
+ triggers?: Record<string, {
171
+ type: string;
172
+ payload?: unknown;
173
+ }>;
174
+ }
175
+
176
+ export declare interface ProjectState {
177
+ status: ProjectStatus;
178
+ timestamp: number;
179
+ }
180
+
181
+ export declare enum ProjectStatus {
182
+ IDLE = "idle",
183
+ STARTING = "starting",
184
+ STARTED = "started",
185
+ STOPPING = "stopping",
186
+ STOPPED = "stopped"
187
+ }
188
+
189
+ export declare interface ProjectTaskConfig<Options = unknown> {
190
+ id: string;
191
+ options: Options;
192
+ type?: ProjectTaskType | string;
193
+ name?: string;
194
+ mode?: ProjectTaskStartMode;
195
+ dependencies?: ProjectTaskDependency[];
196
+ handlers?: ProjectTaskHandler[];
197
+ restart?: ProjectTaskRestartPolicy;
198
+ }
199
+
200
+ export declare interface ProjectTaskDependency {
201
+ taskId: string;
202
+ condition: ProjectTaskStateCondition;
203
+ }
204
+
205
+ export declare interface ProjectTaskHandler {
206
+ condition: ProjectTaskStateCondition;
207
+ action: ProjectAction;
208
+ }
209
+
210
+ export declare type ProjectTaskRestartPolicy = ProjectTaskRestartPolicyAlways | ProjectTaskRestartPolicyOnFailure;
211
+
212
+ export declare interface ProjectTaskRestartPolicyAlways {
213
+ policy: ProjectTaskRestartPolicyType.ALWAYS;
214
+ delay?: number;
215
+ }
216
+
217
+ export declare interface ProjectTaskRestartPolicyOnFailure {
218
+ policy: ProjectTaskRestartPolicyType.ON_FAILURE;
219
+ delay?: number;
220
+ maxRetries?: number;
221
+ }
222
+
223
+ export declare enum ProjectTaskRestartPolicyType {
224
+ ALWAYS = "always",
225
+ ON_FAILURE = "on-failure"
226
+ }
227
+
228
+ export declare enum ProjectTaskStartMode {
229
+ IMMEDIATE = "immediate",
230
+ DEFERRED = "deferred"
231
+ }
232
+
233
+ export declare type ProjectTaskStateCondition = string | boolean | Partial<RuniumTaskState> | ((state: RuniumTaskState) => boolean);
234
+
235
+ export declare enum ProjectTaskType {
236
+ DEFAULT = "default"
237
+ }
238
+
239
+ export declare type ProjectTrigger = ProjectTriggerEvent | ProjectTriggerInterval | ProjectTriggerTimeout | ProjectCustomTrigger;
240
+
241
+ declare interface ProjectTriggerBase {
242
+ id: string;
243
+ type: ProjectTriggerType | string;
244
+ action: ProjectAction;
245
+ disabled?: boolean;
246
+ }
247
+
248
+ export declare interface ProjectTriggerEvent extends ProjectTriggerBase {
249
+ type: ProjectTriggerType.EVENT;
250
+ event: string;
251
+ }
252
+
253
+ export declare interface ProjectTriggerInterval extends ProjectTriggerBase {
254
+ type: ProjectTriggerType.INTERVAL;
255
+ interval: number;
256
+ }
257
+
258
+ export declare interface ProjectTriggerTimeout extends ProjectTriggerBase {
259
+ type: ProjectTriggerType.TIMEOUT;
260
+ timeout: number;
261
+ }
262
+
263
+ export declare enum ProjectTriggerType {
264
+ EVENT = "event",
265
+ TIMEOUT = "timeout",
266
+ INTERVAL = "interval"
267
+ }
268
+
269
+ export declare function readJsonFile<T = JSONValue>(path: string): Promise<T>;
270
+
271
+ declare type RuniumActionProcessor = (payload: unknown) => void;
272
+
273
+ export declare class RuniumError extends Error {
274
+ code: string;
275
+ payload: unknown;
276
+ constructor(message: string, code: string, payload?: unknown);
277
+ }
278
+
279
+ export declare abstract class RuniumTask<Options = unknown, State = RuniumTaskState> extends EventEmitter {
280
+ protected options: Options;
281
+ protected constructor(options: Options);
282
+ abstract getOptions(): Options;
283
+ abstract getState(): State;
284
+ abstract start(): Promise<void>;
285
+ abstract stop(): Promise<void>;
286
+ abstract restart(): Promise<void>;
287
+ }
288
+
289
+ export declare type RuniumTaskConstructor<Options = unknown, State extends RuniumTaskState = RuniumTaskState> = new (options: any) => RuniumTask<Options, State>;
290
+
291
+ export declare interface RuniumTaskState {
292
+ status: TaskStatus;
293
+ timestamp: number;
294
+ iteration: number;
295
+ exitCode?: number;
296
+ error?: Error;
297
+ }
298
+
299
+ export declare abstract class RuniumTrigger<Options extends RuniumTriggerOptions> {
300
+ protected project: RuniumTriggerProjectAccessible;
301
+ protected id: string;
302
+ protected action: ProjectAction;
303
+ protected disabled: boolean;
304
+ constructor(options: Options, project: RuniumTriggerProjectAccessible);
305
+ abstract enable(): void;
306
+ abstract disable(): void;
307
+ getId(): string;
308
+ isDisabled(): boolean;
309
+ }
310
+
311
+ export declare type RuniumTriggerConstructor<Options extends RuniumTriggerOptions> = new (options: Options, project: RuniumTriggerProjectAccessible) => RuniumTrigger<Options>;
312
+
313
+ export declare interface RuniumTriggerOptions {
314
+ id: string;
315
+ action: ProjectAction;
316
+ disabled?: boolean;
317
+ }
318
+
319
+ export declare interface RuniumTriggerProjectAccessible {
320
+ processAction(action: ProjectAction): void;
321
+ on: NodeJS.EventEmitter['on'];
322
+ off: NodeJS.EventEmitter['off'];
323
+ }
324
+
325
+ export declare const SILENT_EXIT_CODE = -1;
326
+
327
+ export declare class Task extends RuniumTask<TaskOptions, TaskState> {
328
+ protected readonly options: TaskOptions;
329
+ constructor(options: TaskOptions);
330
+ getState(): TaskState;
331
+ getOptions(): TaskOptions;
332
+ start(): Promise<void>;
333
+ stop(): Promise<void>;
334
+ restart(): Promise<void>;
335
+ }
336
+
337
+ export declare enum TaskEvent {
338
+ STATE_CHANGE = "state-change",
339
+ STDOUT = "stdout",
340
+ STDERR = "stderr"
341
+ }
342
+
343
+ export declare interface TaskOptions {
344
+ command: string;
345
+ arguments?: string[];
346
+ shell?: boolean;
347
+ stopSignal?: string;
348
+ cwd?: string;
349
+ env?: {
350
+ [key: string]: string | number | boolean;
351
+ };
352
+ ttl?: number;
353
+ log?: {
354
+ stdout?: string | null;
355
+ stderr?: string | null;
356
+ };
357
+ }
358
+
359
+ export declare interface TaskState extends RuniumTaskState {
360
+ pid: number;
361
+ }
362
+
363
+ export declare enum TaskStatus {
364
+ IDLE = "idle",
365
+ STARTING = "starting",
366
+ STARTED = "started",
367
+ COMPLETED = "completed",
368
+ FAILED = "failed",
369
+ STOPPING = "stopping",
370
+ STOPPED = "stopped"
371
+ }
372
+
373
+ export declare class TimeoutTrigger extends RuniumTrigger<TimeoutTriggerOptions> {
374
+ constructor(options: TimeoutTriggerOptions, project: RuniumTriggerProjectAccessible);
375
+ enable(): void;
376
+ disable(): void;
377
+ }
378
+
379
+ export declare interface TimeoutTriggerOptions extends RuniumTriggerOptions {
380
+ timeout: number;
381
+ }
382
+
383
+ export declare function validateProject(project: ProjectConfig, schema: object): void;
384
+
385
+ export declare function writeJsonFile<T = JSONValue>(path: string, data: T): Promise<void>;
386
+
387
+ export { }
package/index.js ADDED
@@ -0,0 +1,571 @@
1
+ import { readFile as G, writeFile as x, mkdir as I } from "node:fs/promises";
2
+ import { EventEmitter as b } from "node:events";
3
+ import M from "bcx-expression-evaluator";
4
+ import q from "ajv/dist/2020.js";
5
+ import K from "ajv-keywords";
6
+ import { spawn as U } from "node:child_process";
7
+ import { createWriteStream as $ } from "node:fs";
8
+ import { resolve as _, dirname as j } from "node:path";
9
+ class d extends Error {
10
+ code;
11
+ payload;
12
+ constructor(t, s, r = null) {
13
+ super(t), this.code = s, this.payload = r;
14
+ }
15
+ }
16
+ function Et(e) {
17
+ return !!e && e instanceof d;
18
+ }
19
+ var F = ((e) => (e.READ_JSON = "file-read-json", e.WRITE_JSON = "file-write-json", e))(F || {});
20
+ async function Rt(e) {
21
+ try {
22
+ const t = await G(e, { encoding: "utf-8" });
23
+ return JSON.parse(t);
24
+ } catch (t) {
25
+ throw new d(`Can not read JSON file ${e}`, "file-read-json", { path: e, original: t });
26
+ }
27
+ }
28
+ async function At(e, t) {
29
+ try {
30
+ await x(e, JSON.stringify(t, null, 2), { encoding: "utf-8" });
31
+ } catch (s) {
32
+ throw new d(`Can not write JSON file ${e}`, "file-write-json", { path: e, data: t, original: s });
33
+ }
34
+ }
35
+ const Y = String.raw`"\$unwrap\((.*)\)"`;
36
+ function _t(e, t) {
37
+ const s = Object.keys(t), r = String.raw`\$(${s.join("|")})\(([^()]*(?:\([^()]*\)[^()]*)*)\)`, i = (o) => {
38
+ const a = new RegExp(r, "g");
39
+ let c = o, n = !0;
40
+ for (; n; ) n = !1, c = c.replace(a, (u, D, L) => {
41
+ n = !0;
42
+ const R = [];
43
+ let A = 0, m = "";
44
+ for (const l of L) l === "," && A === 0 ? (R.push(m.trim()), m = "") : (l === "(" && A++, l === ")" && A--, m += l);
45
+ R.push(m.trim());
46
+ const N = R.map((l) => l ? i(l) : "");
47
+ return t[D](...N);
48
+ });
49
+ return c;
50
+ };
51
+ return (function(o) {
52
+ const a = new RegExp(Y, "g");
53
+ return o.replace(a, (c, n) => n.trim());
54
+ })(s.length ? i(e) : e);
55
+ }
56
+ var k = ((e) => (e.IMMEDIATE = "immediate", e.DEFERRED = "deferred", e))(k || {}), p = ((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))(p || {}), f = ((e) => (e.DEFAULT = "default", e))(f || {}), h = ((e) => (e.EVENT = "event", e.TIMEOUT = "timeout", e.INTERVAL = "interval", e))(h || {}), T = ((e) => (e.ALWAYS = "always", e.ON_FAILURE = "on-failure", e))(T || {}), V = ((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))(V || {});
57
+ const J = /* @__PURE__ */ new Set(["start-task", "restart-task", "stop-task"]), B = /* @__PURE__ */ new Set(["enable-trigger", "disable-trigger"]), H = new Set(Object.values(p)), W = new Set(Object.values(h));
58
+ function w(e) {
59
+ return J.has(e.type);
60
+ }
61
+ function P(e) {
62
+ return B.has(e.type);
63
+ }
64
+ function X(e) {
65
+ return !H.has(e.type);
66
+ }
67
+ function z(e) {
68
+ return !W.has(e.type);
69
+ }
70
+ const O = new q({ allowUnionTypes: !0 });
71
+ function Q(e) {
72
+ const t = /* @__PURE__ */ new Map(), s = /* @__PURE__ */ new Set();
73
+ for (const i of e.tasks) s.add(i.id), t.set(i.id, []);
74
+ const r = new Set(e.triggers?.map((i) => i.id) || []);
75
+ for (const i of e.tasks) {
76
+ for (const o of i.dependencies || []) {
77
+ if (!s.has(o.taskId)) throw new d(`Task "${i.id}" depends on not existing task "${o.taskId}"`, "project-config-task-not-exists", { taskId: i.id, dependency: { ...o }, scope: "dependencies" });
78
+ t.get(o.taskId).push(i.id);
79
+ }
80
+ for (const o of i.handlers || []) {
81
+ if (w(o.action) && !s.has(o.action.taskId)) throw new d(`Task "${i.id}" handler action uses not existing task "${o.action.taskId}"`, "project-config-task-not-exists", { taskId: i.id, handler: { ...o }, scope: "handlers" });
82
+ if (P(o.action) && !r.has(o.action.triggerId)) throw new d(`Task "${i.id}" handler action uses not existing trigger "${o.action.triggerId}"`, "project-config-trigger-not-exists", { taskId: i.id, handler: { ...o }, scope: "handlers" });
83
+ }
84
+ }
85
+ for (const i of e.triggers || []) {
86
+ if (w(i.action) && !s.has(i.action.taskId)) throw new d(`Trigger "${i.id}" action uses not existing task "${i.action.taskId}"`, "project-config-task-not-exists", { trigger: { ...i }, scope: "triggers" });
87
+ if (P(i.action) && !r.has(i.action.triggerId)) throw new d(`Trigger "${i.id}" action uses not existing trigger "${i.action.triggerId}"`, "project-config-trigger-not-exists", { trigger: { ...i }, scope: "triggers" });
88
+ }
89
+ (function(i) {
90
+ const o = /* @__PURE__ */ new Set(), a = /* @__PURE__ */ new Set(), c = (n) => {
91
+ if (a.has(n)) {
92
+ const u = [...a, n].join(" -> ");
93
+ throw new d(`Project config tasks circular dependency detected ${u}`, "project-config-tasks-circular-dependency", { task: n, dependencies: [...a] });
94
+ }
95
+ if (!o.has(n)) {
96
+ a.add(n);
97
+ for (const u of i.get(n) || []) c(u);
98
+ a.delete(n), o.add(n);
99
+ }
100
+ };
101
+ for (const n of i.keys()) o.has(n) || c(n);
102
+ })(t);
103
+ }
104
+ function Z(e, t) {
105
+ (function(s, r) {
106
+ const i = O.compile(r || {});
107
+ if (!i(s)) throw new d("Incorrect project config data", "project-config-incorrect-data", { errors: i.errors });
108
+ })(e, t), Q(e);
109
+ }
110
+ K(O, ["uniqueItemProperties"]);
111
+ 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 || {}), y = ((e) => (e.STATE_CHANGE = "state-change", e.STDOUT = "stdout", e.STDERR = "stderr", e))(y || {});
112
+ const It = -1;
113
+ class v extends b {
114
+ constructor(t) {
115
+ super(), this.options = t;
116
+ }
117
+ }
118
+ class tt extends v {
119
+ constructor(t) {
120
+ super(t), this.options = t, this.setMaxListeners(50), this.options.env = { ...process.env, ...t.env || {} }, this.options.cwd = _(process.cwd(), t.cwd || "");
121
+ }
122
+ state = { status: "idle", pid: -1, timestamp: Date.now(), iteration: 0 };
123
+ process = null;
124
+ stdoutStream = null;
125
+ stderrStream = null;
126
+ ttlTimer = null;
127
+ getState() {
128
+ return { ...this.state };
129
+ }
130
+ getOptions() {
131
+ return { ...this.options };
132
+ }
133
+ async start() {
134
+ const { status: t } = this.state;
135
+ if (t !== "started" && t !== "starting" && t !== "stopping") {
136
+ this.updateState({ status: "starting", iteration: this.state.iteration + 1, pid: -1, exitCode: void 0, error: void 0 });
137
+ try {
138
+ await this.initLogStreams();
139
+ const { cwd: s, command: r, arguments: i = [], env: o, shell: a = !0 } = this.options;
140
+ this.process = U(r, i, { cwd: s, env: o, shell: a, stdio: ["ignore", "pipe", "pipe"] }), this.addProcessListeners(), this.setTTLTimer(), this.updateState({ status: "started", pid: this.process.pid });
141
+ } catch (s) {
142
+ this.onError(s);
143
+ }
144
+ }
145
+ }
146
+ async stop() {
147
+ if (this.process && this.state.status !== "stopping" && this.state.status === "started") return this.updateState({ status: "stopping" }), new Promise((t) => {
148
+ const s = this.options.stopSignal || "SIGTERM";
149
+ this.process.kill(s), this.process.on("exit", () => {
150
+ t();
151
+ });
152
+ });
153
+ }
154
+ async restart() {
155
+ await this.stop(), this.start();
156
+ }
157
+ updateState(t) {
158
+ const s = { ...this.state, ...t, timestamp: Date.now() };
159
+ 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());
160
+ }
161
+ async initLogStreams() {
162
+ const { stdout: t = null, stderr: s = null } = this.options.log || {};
163
+ if (t) {
164
+ const r = _(t);
165
+ await I(j(r), { recursive: !0 }), this.stdoutStream = $(t, { flags: "w" });
166
+ }
167
+ if (s) {
168
+ const r = _(s);
169
+ await I(j(r), { recursive: !0 }), this.stderrStream = $(s, { flags: "w" });
170
+ }
171
+ }
172
+ setTTLTimer() {
173
+ const { ttl: t } = this.options;
174
+ t && this.process && (this.ttlTimer = setTimeout(() => {
175
+ this.stop();
176
+ }, t));
177
+ }
178
+ addProcessListeners() {
179
+ this.process && (this.process.stdout?.on("data", (t) => this.onStdOutData(t)), this.process.stderr?.on("data", (t) => this.onStdErrData(t)), this.process.on("exit", (t) => this.onExit(t)), this.process.on("error", (t) => this.onError(t)));
180
+ }
181
+ onStdOutData(t) {
182
+ const s = t.toString();
183
+ this.emit("stdout", s), this.stdoutStream && this.stdoutStream.write(s);
184
+ }
185
+ onStdErrData(t) {
186
+ const s = t.toString();
187
+ this.emit("stderr", s), this.stderrStream && this.stderrStream.write(s);
188
+ }
189
+ onExit(t) {
190
+ const s = t !== null ? t : -1;
191
+ this.updateState({ status: s === 0 ? "completed" : s === -1 ? "stopped" : "failed", exitCode: s }), this.cleanup();
192
+ }
193
+ onError(t) {
194
+ this.updateState({ status: "failed", error: t }), this.cleanup();
195
+ }
196
+ cleanup() {
197
+ 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);
198
+ }
199
+ }
200
+ var et = ((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))(et || {});
201
+ const st = { id: { type: "string" }, 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" } }, S = { id: { type: "string" }, action: { $ref: "#/$defs/Runium_Action" }, disabled: { type: "boolean" } };
202
+ function C(e, t) {
203
+ return { type: "object", properties: { ...structuredClone(st), type: { const: e }, options: { ...structuredClone(t) } }, required: ["id", "options"], additionalProperties: !1 };
204
+ }
205
+ function rt(e, t) {
206
+ return { type: "object", properties: { type: { type: "string", const: e }, ...t ? { payload: t } : {} }, required: ["type", ...t ? ["payload"] : []], additionalProperties: !1 };
207
+ }
208
+ function it(e, t) {
209
+ const s = t ? { payload: t } : {}, r = t ? ["payload"] : [];
210
+ return { type: "object", properties: { ...structuredClone(S), type: { type: "string", const: e }, ...s }, required: ["id", "type", "action", ...r], additionalProperties: !1 };
211
+ }
212
+ function ot() {
213
+ 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" }, 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(k) }, 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 } }, 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(S), type: { const: h.EVENT }, event: { type: "string" } }, required: ["id", "type", "event", "action"], additionalProperties: !1 }, Runium_TriggerInterval: { type: "object", properties: { ...structuredClone(S), type: { const: h.INTERVAL }, interval: { type: "number", minimum: 0 } }, required: ["id", "type", "interval", "action"], additionalProperties: !1 }, Runium_TriggerTimeout: { type: "object", properties: { ...structuredClone(S), type: { const: h.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: p.EMIT_EVENT }, event: { type: "string" } }, required: ["type", "event"], additionalProperties: !1 }, Runium_ActionProcessTask: { type: "object", properties: { type: { type: "string", enum: [p.START_TASK, p.RESTART_TASK, p.STOP_TASK] }, taskId: { type: "string" } }, required: ["type", "taskId"], additionalProperties: !1 }, Runium_ActionStopProject: { type: "object", properties: { type: { type: "string", const: p.STOP_PROJECT } }, required: ["type"], additionalProperties: !1 }, Runium_ActionToggleTrigger: { type: "object", properties: { type: { type: "string", enum: [p.ENABLE_TRIGGER, p.DISABLE_TRIGGER] }, triggerId: { type: "string" } }, required: ["type", "triggerId"], additionalProperties: !1 }, Runium_TaskConfig: { ...C(f.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: T.ALWAYS }, delay: { type: "number" } }, additionalProperties: !1 }, Runium_TaskRestartPolicyOnFailure: { type: "object", required: ["policy"], properties: { policy: { const: T.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 } } };
214
+ return Object.freeze(e);
215
+ }
216
+ function nt(e, t) {
217
+ let s = structuredClone(e);
218
+ return t.project && (s = (function(r, i) {
219
+ return i && (r.properties = { ...i.properties || {}, ...r.properties }, r.required = Array.from(/* @__PURE__ */ new Set([...r.required ?? [], ...i.required ?? []]))), r;
220
+ })(s, t.project)), t.definitions && (s = (function(r, i) {
221
+ return i && (r.$defs = { ...i, ...r.$defs }), r;
222
+ })(s, t.definitions)), t.tasks && (s = (function(r, i) {
223
+ if (i) {
224
+ const o = new Set(r.properties.tasks.items.oneOf.map((c) => {
225
+ const n = c.$ref.split("/").pop() || "";
226
+ return r.$defs[n]?.properties?.type?.const || f.DEFAULT;
227
+ })), a = {};
228
+ for (const [c, n] of Object.entries(i)) {
229
+ if (o.has(n.type)) throw new d(`Task type "${n.type}" already used in project schema`, "project-schema-task-type-already-used", { type: n.type });
230
+ a[c] = C(n.type, n.options), r.properties.tasks.items.oneOf.push({ $ref: `#/$defs/${c}` }), o.add(n.type);
231
+ }
232
+ r.$defs = { ...a, ...r.$defs };
233
+ }
234
+ return r;
235
+ })(s, t.tasks)), t.actions && (s = (function(r, i) {
236
+ if (i) {
237
+ const o = new Set(r.$defs.Runium_Action.oneOf.map((c) => {
238
+ const n = c.$ref.split("/").pop() || "", u = r.$defs[n]?.properties?.type;
239
+ return u?.enum || [u?.const];
240
+ }).flat()), a = {};
241
+ for (const [c, n] of Object.entries(i)) {
242
+ if (o.has(n.type)) throw new d(`Action type "${n.type}" already used in project schema`, "project-schema-action-type-already-used", { type: n.type });
243
+ a[c] = rt(n.type, n.payload), r.$defs.Runium_Action.oneOf.push({ $ref: `#/$defs/${c}` }), o.add(n.type);
244
+ }
245
+ r.$defs = { ...a, ...r.$defs };
246
+ }
247
+ return r;
248
+ })(s, t.actions)), t.triggers && (s = (function(r, i) {
249
+ if (i) {
250
+ const o = new Set(r.$defs.Runium_Trigger.oneOf.map((c) => {
251
+ const n = c.$ref.split("/").pop() || "";
252
+ return r.$defs[n]?.properties?.type?.const;
253
+ })), a = {};
254
+ for (const [c, n] of Object.entries(i)) {
255
+ if (o.has(n.type)) throw new d(`Trigger type "${n.type}" already used in project schema`, "project-schema-trigger-type-already-used", { type: n.type });
256
+ a[c] = it(n.type, n.payload), r.$defs.Runium_Trigger.oneOf.push({ $ref: `#/$defs/${c}` }), o.add(n.type);
257
+ }
258
+ r.$defs = { ...a, ...r.$defs };
259
+ }
260
+ return r;
261
+ })(s, t.triggers)), Object.freeze(s);
262
+ }
263
+ class E {
264
+ project;
265
+ id;
266
+ action;
267
+ disabled;
268
+ constructor(t, s) {
269
+ this.id = t.id, this.action = t.action, this.disabled = t.disabled ?? !1, this.project = s;
270
+ }
271
+ getId() {
272
+ return this.id;
273
+ }
274
+ isDisabled() {
275
+ return this.disabled;
276
+ }
277
+ }
278
+ class at extends E {
279
+ event;
280
+ constructor(t, s) {
281
+ super(t, s), this.event = t.event;
282
+ }
283
+ enable() {
284
+ this.project.on(this.event, this.handler), this.disabled = !1;
285
+ }
286
+ disable() {
287
+ this.project.off(this.event, this.handler), this.disabled = !0;
288
+ }
289
+ handler = () => {
290
+ this.project.processAction(this.action);
291
+ };
292
+ }
293
+ class ct extends E {
294
+ interval;
295
+ intervalId = null;
296
+ constructor(t, s) {
297
+ super(t, s), this.interval = t.interval;
298
+ }
299
+ enable() {
300
+ this.intervalId = setInterval(() => {
301
+ this.project.processAction(this.action);
302
+ }, this.interval), this.disabled = !1;
303
+ }
304
+ disable() {
305
+ this.intervalId && clearInterval(this.intervalId), this.disabled = !0, this.intervalId = null;
306
+ }
307
+ }
308
+ class dt extends E {
309
+ timeout;
310
+ timeoutId = null;
311
+ constructor(t, s) {
312
+ super(t, s), this.timeout = t.timeout;
313
+ }
314
+ enable() {
315
+ this.timeoutId = setTimeout(() => {
316
+ this.project.processAction(this.action);
317
+ }, this.timeout), this.disabled = !1;
318
+ }
319
+ disable() {
320
+ this.timeoutId && clearTimeout(this.timeoutId), this.disabled = !0, this.timeoutId = null;
321
+ }
322
+ }
323
+ var pt = ((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))(pt || {}), ut = ((e) => (e.IDLE = "idle", e.STARTING = "starting", e.STARTED = "started", e.STOPPING = "stopping", e.STOPPED = "stopped", e))(ut || {}), gt = ((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))(gt || {});
324
+ class $t extends b {
325
+ constructor(t) {
326
+ super(), this.config = t;
327
+ }
328
+ schema = ot();
329
+ taskProcessors = /* @__PURE__ */ new Map([[f.DEFAULT, tt]]);
330
+ actionProcessors = /* @__PURE__ */ new Map();
331
+ triggerProcessors = /* @__PURE__ */ new Map();
332
+ tasks = /* @__PURE__ */ new Map();
333
+ triggers = /* @__PURE__ */ new Map();
334
+ state = { timestamp: Date.now(), status: "idle" };
335
+ validate() {
336
+ Z(this.config, this.schema);
337
+ }
338
+ getConfig() {
339
+ return { ...this.config };
340
+ }
341
+ setConfig(t) {
342
+ this.config = { ...t };
343
+ }
344
+ getState() {
345
+ return { ...this.state };
346
+ }
347
+ async start() {
348
+ if (this.state.status !== "idle" && this.state.status !== "stopped") return;
349
+ this.validate(), this.initTasks(), this.initTriggers(), this.updateState({ status: "starting" });
350
+ const t = this.getTasksStartOrder();
351
+ for (const s of t) if (this.tasks.has(s)) {
352
+ const { instance: r, config: i } = this.tasks.get(s), { mode: o = k.IMMEDIATE, dependencies: a = [] } = i;
353
+ o === k.IMMEDIATE && a.length === 0 && r.start();
354
+ }
355
+ this.updateState({ status: "started" });
356
+ }
357
+ async stop() {
358
+ if (this.state.status !== "started" && this.state.status !== "starting") return;
359
+ this.updateState({ status: "stopping" }), this.cleanupTriggers();
360
+ const t = this.getTasksStartOrder().reverse(), s = [];
361
+ for (const r of t) if (this.tasks.has(r)) {
362
+ const { instance: i } = this.tasks.get(r), { status: o } = i.getState();
363
+ o === g.STARTED && s.push(i.stop());
364
+ }
365
+ await Promise.allSettled(s), this.updateState({ status: "stopped" });
366
+ }
367
+ extendValidationSchema(t) {
368
+ this.schema = nt(this.schema, t);
369
+ }
370
+ registerAction(t, s) {
371
+ if (this.actionProcessors.has(t)) throw new d(`Action processor for type "${t}" already registered`, "project-action-processor-already-registered", { type: t });
372
+ if (typeof s != "function") throw new d(`Action processor for type "${t}" must be a function`, "project-action-processor-incorrect", { type: t });
373
+ this.actionProcessors.set(t, s);
374
+ }
375
+ registerTask(t, s) {
376
+ if (this.taskProcessors.has(t)) throw new d(`Task processor for type "${t}" already registered`, "project-task-processor-already-registered", { type: t });
377
+ if (!(s.prototype instanceof v)) throw new d(`Task processor for type "${t}" must be a subclass of "RuniumTask"`, "project-task-processor-incorrect", { type: t });
378
+ this.taskProcessors.set(t, s);
379
+ }
380
+ registerTrigger(t, s) {
381
+ if (this.triggerProcessors.has(t)) throw new d(`Trigger processor for type "${t}" already registered`, "project-trigger-processor-already-registered", { type: t });
382
+ if (!(s.prototype instanceof E)) throw new d(`Trigger processor for type "${t}" must be a subclass of "RuniumTrigger"`, "project-trigger-processor-incorrect", { type: t });
383
+ this.triggerProcessors.set(t, s);
384
+ }
385
+ updateState(t) {
386
+ this.state = { ...this.state, ...t, timestamp: Date.now() }, this.emit("state-change", this.getState()), this.emit(this.state.status, this.getState());
387
+ }
388
+ initTasks() {
389
+ this.tasks.clear();
390
+ for (const t of this.config.tasks) {
391
+ const s = t.type || f.DEFAULT, r = this.taskProcessors.get(s);
392
+ if (!r) throw new d(`Task processor for type "${s}" not found`, "project-task-processor-not-found", { type: s });
393
+ const i = new r(t.options);
394
+ i.on(y.STATE_CHANGE, (o) => {
395
+ this.emit("task-state-change", t.id, o), this.onTaskStateChange(t.id, o);
396
+ }), i.on(y.STDOUT, (o) => {
397
+ this.emit("task-stdout", t.id, o);
398
+ }), i.on(y.STDERR, (o) => {
399
+ this.emit("task-stderr", t.id, o);
400
+ }), this.tasks.set(t.id, { instance: i, config: t, dependencies: [...t.dependencies || []], dependents: null });
401
+ }
402
+ }
403
+ getTasksStartOrder() {
404
+ const t = [], s = /* @__PURE__ */ new Set(), r = (i) => {
405
+ if (s.has(i)) return;
406
+ s.add(i);
407
+ const { dependencies: o = [] } = this.tasks.get(i) || {};
408
+ for (const a of o) this.tasks.has(a.taskId) && r(a.taskId);
409
+ t.push(i);
410
+ };
411
+ for (const i of this.tasks.keys()) r(i);
412
+ return t;
413
+ }
414
+ onTaskStateChange(t, s) {
415
+ const { config: r, instance: i } = this.tasks.get(t), o = this.getDependentTasks(t);
416
+ for (const a of o) this.isDependentTaskReady(a) && this.startTask(a);
417
+ if (s.status === g.COMPLETED || s.status === g.FAILED) {
418
+ const { restart: a } = r;
419
+ if (a && s.exitCode !== -1) {
420
+ const { policy: c } = a;
421
+ if (c === T.ALWAYS || c === T.ON_FAILURE && s.exitCode !== 0) {
422
+ const { maxRetries: n = 1 / 0, delay: u = 0 } = a;
423
+ s.iteration <= n && setTimeout(i.restart.bind(i), u);
424
+ }
425
+ }
426
+ }
427
+ (r.handlers || []).forEach((a) => {
428
+ this.checkTaskStateCondition(a.condition, s) && this.processAction(a.action);
429
+ });
430
+ }
431
+ checkTaskStateCondition(t, s) {
432
+ return typeof t == "string" ? M.evaluate(t, s) === !0 : typeof t == "object" ? Object.entries(t).every(([r, i]) => s[r] === i) : typeof t == "boolean" && t;
433
+ }
434
+ async startTask(t) {
435
+ if (this.tasks.has(t)) {
436
+ const { instance: s } = this.tasks.get(t), { status: r } = s.getState();
437
+ if (r !== g.STARTED && r !== g.STARTING && r !== g.STOPPING) return this.emit("start-task", t), s.start();
438
+ }
439
+ }
440
+ async stopTask(t) {
441
+ if (this.tasks.has(t)) {
442
+ const { instance: s } = this.tasks.get(t), { status: r } = s.getState();
443
+ if (r === g.STARTED) return this.emit("stop-task", t), s.stop();
444
+ }
445
+ }
446
+ async restartTask(t) {
447
+ if (this.tasks.has(t)) {
448
+ const { instance: s } = this.tasks.get(t);
449
+ return this.emit("restart-task", t), s.restart();
450
+ }
451
+ }
452
+ isDependentTaskReady(t) {
453
+ if (this.tasks.has(t)) {
454
+ const { dependencies: s = [] } = this.tasks.get(t);
455
+ for (const { taskId: r, condition: i } of s) {
456
+ const { instance: o } = this.tasks.get(r);
457
+ if (!this.checkTaskStateCondition(i, o.getState())) return !1;
458
+ }
459
+ return !0;
460
+ }
461
+ return !1;
462
+ }
463
+ getDependentTasks(t) {
464
+ let s = [];
465
+ if (this.tasks.has(t)) {
466
+ const { dependents: r } = this.tasks.get(t);
467
+ if (r) s = r;
468
+ else {
469
+ for (const [i, { dependencies: o }] of this.tasks) for (const a of o) a.taskId === t && s.push(i);
470
+ this.tasks.get(t).dependents = s;
471
+ }
472
+ }
473
+ return s;
474
+ }
475
+ processAction(t) {
476
+ if (this.emit("process-action", t), X(t)) {
477
+ const s = this.actionProcessors.get(t.type);
478
+ s && s(t.payload || {});
479
+ } else switch (t.type) {
480
+ case p.START_TASK:
481
+ this.startTask(t.taskId);
482
+ break;
483
+ case p.RESTART_TASK:
484
+ this.restartTask(t.taskId);
485
+ break;
486
+ case p.STOP_TASK:
487
+ this.stopTask(t.taskId);
488
+ break;
489
+ case p.EMIT_EVENT:
490
+ this.emit(t.event);
491
+ break;
492
+ case p.STOP_PROJECT:
493
+ break;
494
+ case p.ENABLE_TRIGGER:
495
+ this.enableTrigger(t.triggerId);
496
+ break;
497
+ case p.DISABLE_TRIGGER:
498
+ this.disableTrigger(t.triggerId);
499
+ }
500
+ }
501
+ initTriggers() {
502
+ const t = { processAction: this.processAction.bind(this), on: this.on.bind(this), off: this.off.bind(this) };
503
+ let s = null;
504
+ for (const r of this.config.triggers || []) {
505
+ if (z(r)) {
506
+ const i = this.triggerProcessors.get(r.type);
507
+ i && (s = new i(r, t));
508
+ } else switch (r.type) {
509
+ case h.EVENT:
510
+ s = new at(r, t);
511
+ break;
512
+ case h.INTERVAL:
513
+ s = new ct(r, t);
514
+ break;
515
+ case h.TIMEOUT:
516
+ s = new dt(r, t);
517
+ }
518
+ s && (this.triggers.set(r.id, s), r.disabled !== !0 && s.enable());
519
+ }
520
+ }
521
+ cleanupTriggers() {
522
+ for (const t in this.triggers) {
523
+ const s = this.triggers.get(t);
524
+ s && s.disable();
525
+ }
526
+ this.triggers.clear();
527
+ }
528
+ enableTrigger(t) {
529
+ const s = this.triggers.get(t);
530
+ s && (this.emit("enable-trigger", t), s.enable());
531
+ }
532
+ disableTrigger(t) {
533
+ const s = this.triggers.get(t);
534
+ s && (this.emit("disable-trigger", t), s.disable());
535
+ }
536
+ }
537
+ export {
538
+ at as EventTrigger,
539
+ F as FileErrorCode,
540
+ ct as IntervalTrigger,
541
+ $t as Project,
542
+ p as ProjectActionType,
543
+ V as ProjectConfigErrorCode,
544
+ gt as ProjectErrorCode,
545
+ pt as ProjectEvent,
546
+ et as ProjectSchemaErrorCode,
547
+ ut as ProjectStatus,
548
+ T as ProjectTaskRestartPolicyType,
549
+ k as ProjectTaskStartMode,
550
+ f as ProjectTaskType,
551
+ h as ProjectTriggerType,
552
+ d as RuniumError,
553
+ v as RuniumTask,
554
+ E as RuniumTrigger,
555
+ It as SILENT_EXIT_CODE,
556
+ tt as Task,
557
+ y as TaskEvent,
558
+ g as TaskStatus,
559
+ dt as TimeoutTrigger,
560
+ _t as applyMacros,
561
+ nt as extendProjectSchema,
562
+ ot as getProjectSchema,
563
+ X as isCustomAction,
564
+ z as isCustomTrigger,
565
+ w as isProcessTaskAction,
566
+ Et as isRuniumError,
567
+ P as isToggleTriggerAction,
568
+ Rt as readJsonFile,
569
+ Z as validateProject,
570
+ At as writeJsonFile
571
+ };
package/package.json ADDED
@@ -0,0 +1,26 @@
1
+ {
2
+ "name": "@runium/core",
3
+ "version": "0.0.1",
4
+ "description": "Runium Core",
5
+ "author": "TheBeastApp",
6
+ "license": "MIT",
7
+ "type": "module",
8
+ "types": "./index.d.ts",
9
+ "main": "./index.js",
10
+ "module": "./index.js",
11
+ "files": [
12
+ "index.js",
13
+ "index.d.ts"
14
+ ],
15
+ "exports": {
16
+ ".": {
17
+ "types": "./index.d.ts",
18
+ "import": "./index.js"
19
+ }
20
+ },
21
+ "dependencies": {
22
+ "ajv": "^8.17.1",
23
+ "ajv-keywords": "^5.1.0",
24
+ "bcx-expression-evaluator": "^1.2.1"
25
+ }
26
+ }