@oxog/pigment 1.0.2 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{index-ChElNHaA.d.cts → index-CLqJXFfX.d.cts} +8 -0
- package/dist/{index-ChElNHaA.d.ts → index-CLqJXFfX.d.ts} +8 -0
- package/dist/index.cjs +514 -84
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +3 -2
- package/dist/index.d.ts +3 -2
- package/dist/index.js +514 -84
- package/dist/index.js.map +1 -1
- package/dist/plugins/index.cjs +511 -79
- package/dist/plugins/index.cjs.map +1 -1
- package/dist/plugins/index.d.cts +1 -1
- package/dist/plugins/index.d.ts +1 -1
- package/dist/plugins/index.js +511 -79
- package/dist/plugins/index.js.map +1 -1
- package/package.json +37 -21
package/dist/index.js
CHANGED
|
@@ -8,8 +8,464 @@ var __export = (target, all) => {
|
|
|
8
8
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
9
|
};
|
|
10
10
|
|
|
11
|
-
//
|
|
12
|
-
import {
|
|
11
|
+
// node_modules/@oxog/plugin/dist/index.js
|
|
12
|
+
import { PluginError } from "@oxog/types";
|
|
13
|
+
import { PluginError as PluginError2 } from "@oxog/types";
|
|
14
|
+
var x = ((i) => (i.Created = "created", i.Initializing = "initializing", i.Ready = "ready", i.Destroying = "destroying", i.Destroyed = "destroyed", i))(x || {});
|
|
15
|
+
var h = class {
|
|
16
|
+
context;
|
|
17
|
+
constructor(e) {
|
|
18
|
+
this.context = e;
|
|
19
|
+
}
|
|
20
|
+
get() {
|
|
21
|
+
return this.context;
|
|
22
|
+
}
|
|
23
|
+
update(e) {
|
|
24
|
+
this.context = Object.assign(this.context, e);
|
|
25
|
+
}
|
|
26
|
+
deepUpdate(e) {
|
|
27
|
+
let t = this.deepMerge(this.context, e);
|
|
28
|
+
this.context = Object.assign(this.context, t);
|
|
29
|
+
}
|
|
30
|
+
deepMerge(e, t) {
|
|
31
|
+
if (t === null || typeof t != "object") return t;
|
|
32
|
+
if (Array.isArray(t)) return [...t];
|
|
33
|
+
if (e === null || typeof e != "object" || Array.isArray(e)) return { ...t };
|
|
34
|
+
let n = { ...e };
|
|
35
|
+
for (let r of Object.keys(t)) {
|
|
36
|
+
let i = t[r], a = n[r];
|
|
37
|
+
n[r] = this.deepMerge(a, i);
|
|
38
|
+
}
|
|
39
|
+
return n;
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
var u = class {
|
|
43
|
+
handlers = /* @__PURE__ */ new Map();
|
|
44
|
+
wildcardHandlers = new Array();
|
|
45
|
+
patternHandlers = /* @__PURE__ */ new Map();
|
|
46
|
+
onceWrappers = /* @__PURE__ */ new Map();
|
|
47
|
+
on(e, t) {
|
|
48
|
+
let n = String(e);
|
|
49
|
+
this.handlers.has(n) || this.handlers.set(n, []);
|
|
50
|
+
let r = this.handlers.get(n), i = t;
|
|
51
|
+
return r.includes(i) || r.push(i), () => {
|
|
52
|
+
let a = this.handlers.get(n);
|
|
53
|
+
if (a) {
|
|
54
|
+
let s = a.indexOf(i);
|
|
55
|
+
s !== -1 && a.splice(s, 1);
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
once(e, t) {
|
|
60
|
+
let n = t, r = String(e), i = this.onceWrappers.get(r);
|
|
61
|
+
i || (i = /* @__PURE__ */ new Map(), this.onceWrappers.set(r, i));
|
|
62
|
+
let a = (s) => {
|
|
63
|
+
i.delete(n), this.off(e, a), t(s);
|
|
64
|
+
};
|
|
65
|
+
return i.set(n, a), this.on(e, a);
|
|
66
|
+
}
|
|
67
|
+
off(e, t) {
|
|
68
|
+
let n = String(e), r = this.handlers.get(n);
|
|
69
|
+
if (r) {
|
|
70
|
+
let i = t, a = this.onceWrappers.get(n), s = a?.get(i), o = s || i, l = r.indexOf(o);
|
|
71
|
+
l !== -1 && (r.splice(l, 1), s && a.delete(i));
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
emit(e, t) {
|
|
75
|
+
let n = String(e), r = this.handlers.get(n);
|
|
76
|
+
if (r) for (let i of [...r]) try {
|
|
77
|
+
i(t);
|
|
78
|
+
} catch {
|
|
79
|
+
}
|
|
80
|
+
for (let i of [...this.wildcardHandlers]) try {
|
|
81
|
+
i(n, t);
|
|
82
|
+
} catch {
|
|
83
|
+
}
|
|
84
|
+
for (let [i, a] of this.patternHandlers) if (this.matchPattern(i, n)) for (let s of [...a]) try {
|
|
85
|
+
s(n, t);
|
|
86
|
+
} catch {
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
onWildcard(e) {
|
|
90
|
+
return this.wildcardHandlers.includes(e) || this.wildcardHandlers.push(e), () => {
|
|
91
|
+
let t = this.wildcardHandlers.indexOf(e);
|
|
92
|
+
t !== -1 && this.wildcardHandlers.splice(t, 1);
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
onPattern(e, t) {
|
|
96
|
+
this.patternHandlers.has(e) || this.patternHandlers.set(e, []);
|
|
97
|
+
let n = this.patternHandlers.get(e);
|
|
98
|
+
return n.includes(t) || n.push(t), () => {
|
|
99
|
+
let r = this.patternHandlers.get(e);
|
|
100
|
+
if (r) {
|
|
101
|
+
let i = r.indexOf(t);
|
|
102
|
+
i !== -1 && r.splice(i, 1);
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
clear() {
|
|
107
|
+
this.handlers.clear(), this.wildcardHandlers.length = 0, this.patternHandlers.clear(), this.onceWrappers.clear();
|
|
108
|
+
}
|
|
109
|
+
matchPattern(e, t) {
|
|
110
|
+
if (e === "*") return true;
|
|
111
|
+
if (e.endsWith(":*")) {
|
|
112
|
+
let n = e.slice(0, -2);
|
|
113
|
+
return t.startsWith(n + ":");
|
|
114
|
+
}
|
|
115
|
+
return e === t;
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
var f = class {
|
|
119
|
+
graph = /* @__PURE__ */ new Map();
|
|
120
|
+
addPlugin(e, t = []) {
|
|
121
|
+
this.graph.set(e, [...t]);
|
|
122
|
+
}
|
|
123
|
+
removePlugin(e) {
|
|
124
|
+
this.graph.delete(e);
|
|
125
|
+
for (let t of this.graph.values()) {
|
|
126
|
+
let n = t.indexOf(e);
|
|
127
|
+
n !== -1 && t.splice(n, 1);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
validateSinglePlugin(e) {
|
|
131
|
+
let t = this.graph.get(e);
|
|
132
|
+
if (!t) return;
|
|
133
|
+
for (let r of t) if (!this.graph.has(r)) throw new PluginError(`Missing dependency: '${r}' required by '${e}'`, e);
|
|
134
|
+
let n = this.detectCycle();
|
|
135
|
+
if (n && n.includes(e)) throw new PluginError(`Circular dependency detected: ${n.join(" -> ")}`, "dependency-resolver");
|
|
136
|
+
}
|
|
137
|
+
resolve() {
|
|
138
|
+
let e = /* @__PURE__ */ new Map(), t = [], n = [], r = /* @__PURE__ */ new Map();
|
|
139
|
+
for (let [s, o] of this.graph) {
|
|
140
|
+
let l = o.filter((p) => this.graph.has(p));
|
|
141
|
+
r.set(s, l);
|
|
142
|
+
}
|
|
143
|
+
for (let [s, o] of this.graph) for (let l of o) if (!this.graph.has(l)) throw new PluginError(`Missing dependency: '${l}' required by '${s}'`, s);
|
|
144
|
+
for (let [s, o] of r) e.set(s, o.length), o.length === 0 && t.push(s);
|
|
145
|
+
let i = /* @__PURE__ */ new Map();
|
|
146
|
+
for (let [s, o] of r) for (let l of o) {
|
|
147
|
+
let p = i.get(l);
|
|
148
|
+
p ? p.push(s) : i.set(l, [s]);
|
|
149
|
+
}
|
|
150
|
+
let a = 0;
|
|
151
|
+
for (; a < t.length; ) {
|
|
152
|
+
let s = t[a++];
|
|
153
|
+
n.push(s);
|
|
154
|
+
for (let o of i.get(s) ?? []) e.set(o, e.get(o) - 1), e.get(o) === 0 && t.push(o);
|
|
155
|
+
}
|
|
156
|
+
if (n.length !== r.size) {
|
|
157
|
+
let s = this.detectCycle();
|
|
158
|
+
throw new PluginError(`Circular dependency detected: ${s.join(" -> ")}`, "dependency-resolver");
|
|
159
|
+
}
|
|
160
|
+
return n;
|
|
161
|
+
}
|
|
162
|
+
detectCycle() {
|
|
163
|
+
let r = /* @__PURE__ */ new Map();
|
|
164
|
+
for (let a of this.graph.keys()) r.set(a, 0);
|
|
165
|
+
let i = (a, s) => {
|
|
166
|
+
r.set(a, 1), s.push(a);
|
|
167
|
+
for (let o of this.graph.get(a)) if (this.graph.has(o)) {
|
|
168
|
+
if (r.get(o) === 1) {
|
|
169
|
+
let l = s.indexOf(o);
|
|
170
|
+
return [...s.slice(l), o];
|
|
171
|
+
}
|
|
172
|
+
if (r.get(o) === 0) {
|
|
173
|
+
let l = i(o, s);
|
|
174
|
+
if (l) return l;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
return s.pop(), r.set(a, 2), null;
|
|
178
|
+
};
|
|
179
|
+
for (let a of this.graph.keys()) if (r.get(a) === 0) {
|
|
180
|
+
let s = i(a, []);
|
|
181
|
+
if (s) return s;
|
|
182
|
+
}
|
|
183
|
+
return null;
|
|
184
|
+
}
|
|
185
|
+
getGraph() {
|
|
186
|
+
let e = {};
|
|
187
|
+
for (let [t, n] of this.graph) e[t] = [...n];
|
|
188
|
+
return e;
|
|
189
|
+
}
|
|
190
|
+
clear() {
|
|
191
|
+
this.graph.clear();
|
|
192
|
+
}
|
|
193
|
+
};
|
|
194
|
+
var v = class {
|
|
195
|
+
strategy;
|
|
196
|
+
globalHandler;
|
|
197
|
+
errors = [];
|
|
198
|
+
constructor(e, t) {
|
|
199
|
+
this.strategy = e, this.globalHandler = t;
|
|
200
|
+
}
|
|
201
|
+
getStrategy() {
|
|
202
|
+
return this.strategy;
|
|
203
|
+
}
|
|
204
|
+
async withBoundary(e, t, n) {
|
|
205
|
+
try {
|
|
206
|
+
return await n();
|
|
207
|
+
} catch (r) {
|
|
208
|
+
let i = r instanceof Error ? r : new Error(String(r));
|
|
209
|
+
throw await this.handleError(e, t, i), i;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
async handleError(e, t, n) {
|
|
213
|
+
if (t.onError) try {
|
|
214
|
+
t.onError(n);
|
|
215
|
+
} catch {
|
|
216
|
+
}
|
|
217
|
+
if (this.globalHandler) try {
|
|
218
|
+
this.globalHandler(n, e);
|
|
219
|
+
} catch {
|
|
220
|
+
}
|
|
221
|
+
this.strategy === "collect" && this.errors.push(n);
|
|
222
|
+
}
|
|
223
|
+
throwIfErrors() {
|
|
224
|
+
if (this.strategy === "collect" && this.errors.length > 0) {
|
|
225
|
+
let e = new AggregateError(this.errors, `${this.errors.length} plugin error(s) occurred`);
|
|
226
|
+
throw this.clearErrors(), e;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
clearErrors() {
|
|
230
|
+
this.errors = [];
|
|
231
|
+
}
|
|
232
|
+
getErrors() {
|
|
233
|
+
return Object.freeze([...this.errors]);
|
|
234
|
+
}
|
|
235
|
+
};
|
|
236
|
+
var m = class {
|
|
237
|
+
async initialize(e, t, n, r, i) {
|
|
238
|
+
let a = r.getStrategy();
|
|
239
|
+
for (let s of t) {
|
|
240
|
+
let o = e.get(s);
|
|
241
|
+
if (o && o.state !== "ready") {
|
|
242
|
+
o.state = "initializing";
|
|
243
|
+
try {
|
|
244
|
+
await r.withBoundary(s, o.plugin, async () => {
|
|
245
|
+
o.plugin.onInit && await o.plugin.onInit(n);
|
|
246
|
+
}), o.state = "ready", o.initializedAt = Date.now(), i?.(s);
|
|
247
|
+
} catch (l) {
|
|
248
|
+
if (a === "fail-fast") throw l;
|
|
249
|
+
o.state = "failed";
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
async destroy(e, t, n, r) {
|
|
255
|
+
let i = n.getStrategy(), a = [...t].reverse();
|
|
256
|
+
for (let s of a) {
|
|
257
|
+
let o = e.get(s);
|
|
258
|
+
if (o) {
|
|
259
|
+
o.state = "destroying";
|
|
260
|
+
try {
|
|
261
|
+
await n.withBoundary(s, o.plugin, async () => {
|
|
262
|
+
o.plugin.onDestroy && await o.plugin.onDestroy();
|
|
263
|
+
});
|
|
264
|
+
} catch (l) {
|
|
265
|
+
if (i === "fail-fast") throw l;
|
|
266
|
+
}
|
|
267
|
+
o.state = "destroyed", r?.(s);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
};
|
|
272
|
+
var c = class {
|
|
273
|
+
state = "created";
|
|
274
|
+
plugins = /* @__PURE__ */ new Map();
|
|
275
|
+
context;
|
|
276
|
+
events;
|
|
277
|
+
resolver;
|
|
278
|
+
errorBoundary;
|
|
279
|
+
lifecycle;
|
|
280
|
+
config;
|
|
281
|
+
pendingInits = /* @__PURE__ */ new Map();
|
|
282
|
+
emitInternal(e, t) {
|
|
283
|
+
this.events.emit(e, t);
|
|
284
|
+
}
|
|
285
|
+
constructor(e) {
|
|
286
|
+
this.config = e || {}, this.context = new h(this.config.context ?? {}), this.events = new u(), this.resolver = new f(), this.errorBoundary = new v(this.config.errorStrategy || "isolate", this.config.onError), this.lifecycle = new m();
|
|
287
|
+
}
|
|
288
|
+
use(e) {
|
|
289
|
+
if (this.state === "destroyed") throw new PluginError("Cannot add plugins to destroyed kernel", "kernel");
|
|
290
|
+
if (this.state === "initializing") throw new PluginError("Cannot add plugins while kernel is initializing. Register all plugins before calling init(), or wait until kernel is ready.", "kernel");
|
|
291
|
+
if (this.plugins.has(e.name)) throw new PluginError(`Plugin '${e.name}' is already registered`, "kernel");
|
|
292
|
+
let t = { plugin: e, state: "registered" };
|
|
293
|
+
this.plugins.set(e.name, t), this.resolver.addPlugin(e.name, [...e.dependencies || []]);
|
|
294
|
+
try {
|
|
295
|
+
if (this.state === "ready" && this.resolver.validateSinglePlugin(e.name), e.install(this), this.emitInternal("plugin:install", { name: e.name, version: e.version }), this.state === "ready") {
|
|
296
|
+
let n = this.initializeWithDependencies(e.name);
|
|
297
|
+
this.pendingInits.set(e.name, n), n.finally(() => {
|
|
298
|
+
this.pendingInits.delete(e.name);
|
|
299
|
+
});
|
|
300
|
+
}
|
|
301
|
+
} catch (n) {
|
|
302
|
+
throw this.pendingInits.delete(e.name), this.plugins.delete(e.name), this.resolver.removePlugin(e.name), n;
|
|
303
|
+
}
|
|
304
|
+
return this;
|
|
305
|
+
}
|
|
306
|
+
async waitForPlugin(e) {
|
|
307
|
+
let t = this.pendingInits.get(e);
|
|
308
|
+
t && await t;
|
|
309
|
+
}
|
|
310
|
+
async waitForAll() {
|
|
311
|
+
await Promise.all(this.pendingInits.values());
|
|
312
|
+
}
|
|
313
|
+
async initializeWithDependencies(e) {
|
|
314
|
+
let t = this.plugins.get(e);
|
|
315
|
+
if (!t) return;
|
|
316
|
+
let n = t.plugin.dependencies || [];
|
|
317
|
+
for (let r of n) {
|
|
318
|
+
let i = this.pendingInits.get(r);
|
|
319
|
+
i && await i;
|
|
320
|
+
}
|
|
321
|
+
for (let r of n) {
|
|
322
|
+
let i = this.plugins.get(r);
|
|
323
|
+
if (!i || i.state !== "ready") {
|
|
324
|
+
t.state = "failed";
|
|
325
|
+
let a = new PluginError(`Dependency '${r}' is not ready for plugin '${e}'`, e);
|
|
326
|
+
this.emitInternal("plugin:error", { name: e, error: a });
|
|
327
|
+
return;
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
await this.initializePlugin(e);
|
|
331
|
+
}
|
|
332
|
+
async initializePlugin(e) {
|
|
333
|
+
let t = this.plugins.get(e);
|
|
334
|
+
if (!(!t || t.state !== "registered")) try {
|
|
335
|
+
t.state = "initializing", await this.errorBoundary.withBoundary(e, t.plugin, async () => {
|
|
336
|
+
t.plugin.onInit && await t.plugin.onInit(this.context.get());
|
|
337
|
+
}), t.state = "ready", t.initializedAt = Date.now(), this.emitInternal("plugin:init", { name: e });
|
|
338
|
+
} catch (n) {
|
|
339
|
+
t.state = "failed", this.emitInternal("plugin:error", { name: e, error: n });
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
useAll(e) {
|
|
343
|
+
for (let t of e) this.use(t);
|
|
344
|
+
return this;
|
|
345
|
+
}
|
|
346
|
+
unregister(e) {
|
|
347
|
+
let t = this.plugins.get(e);
|
|
348
|
+
if (!t) return false;
|
|
349
|
+
let n = this.pendingInits.get(e);
|
|
350
|
+
return t.state === "initializing" && n ? (n.finally(() => {
|
|
351
|
+
if (t.plugin.onDestroy) return t.plugin.onDestroy();
|
|
352
|
+
}).then(() => this.emitInternal("plugin:destroy", { name: e })).catch(() => {
|
|
353
|
+
}), this.pendingInits.delete(e)) : t.state === "ready" && t.plugin.onDestroy && Promise.resolve().then(() => t.plugin.onDestroy()).then(() => this.emitInternal("plugin:destroy", { name: e })).catch(() => {
|
|
354
|
+
}), this.plugins.delete(e), this.resolver.removePlugin(e), true;
|
|
355
|
+
}
|
|
356
|
+
async unregisterAsync(e) {
|
|
357
|
+
let t = this.plugins.get(e);
|
|
358
|
+
if (!t) return false;
|
|
359
|
+
let n = this.pendingInits.get(e);
|
|
360
|
+
if (t.state === "initializing" && n) {
|
|
361
|
+
try {
|
|
362
|
+
await n;
|
|
363
|
+
} catch {
|
|
364
|
+
}
|
|
365
|
+
this.pendingInits.delete(e);
|
|
366
|
+
}
|
|
367
|
+
if ((t.state === "ready" || t.state === "failed") && t.plugin.onDestroy) try {
|
|
368
|
+
await t.plugin.onDestroy(), this.emitInternal("plugin:destroy", { name: e });
|
|
369
|
+
} catch {
|
|
370
|
+
}
|
|
371
|
+
return this.plugins.delete(e), this.resolver.removePlugin(e), true;
|
|
372
|
+
}
|
|
373
|
+
async replace(e) {
|
|
374
|
+
await this.unregisterAsync(e.name), this.use(e), this.state === "ready" && await this.waitForPlugin(e.name);
|
|
375
|
+
}
|
|
376
|
+
async reload(e) {
|
|
377
|
+
let t = this.plugins.get(e);
|
|
378
|
+
if (!t) return;
|
|
379
|
+
let n = this.errorBoundary.getStrategy();
|
|
380
|
+
if (t.state === "ready") {
|
|
381
|
+
try {
|
|
382
|
+
t.plugin.onDestroy && await this.errorBoundary.withBoundary(e, t.plugin, async () => t.plugin.onDestroy()), this.emitInternal("plugin:destroy", { name: e });
|
|
383
|
+
} catch (r) {
|
|
384
|
+
if (n === "fail-fast") throw r;
|
|
385
|
+
}
|
|
386
|
+
t.state = "registered";
|
|
387
|
+
try {
|
|
388
|
+
t.plugin.onInit && await this.errorBoundary.withBoundary(e, t.plugin, async () => t.plugin.onInit(this.context.get())), t.state = "ready", this.emitInternal("plugin:init", { name: e });
|
|
389
|
+
} catch (r) {
|
|
390
|
+
if (n === "fail-fast") throw r;
|
|
391
|
+
t.state = "failed";
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
async init() {
|
|
396
|
+
if (this.state !== "created") throw new PluginError("Kernel already initialized", "kernel");
|
|
397
|
+
this.state = "initializing";
|
|
398
|
+
try {
|
|
399
|
+
this.config.onBeforeInit && await this.config.onBeforeInit();
|
|
400
|
+
let e = this.resolver.resolve();
|
|
401
|
+
this.emitInternal("kernel:init", { timestamp: Date.now() }), await this.lifecycle.initialize(this.plugins, e, this.context.get(), this.errorBoundary, (t) => this.emitInternal("plugin:init", { name: t })), this.state = "ready", this.emitInternal("kernel:ready", { timestamp: Date.now(), plugins: Array.from(this.plugins.keys()) }), this.config.onAfterInit && await this.config.onAfterInit(), this.errorBoundary.throwIfErrors();
|
|
402
|
+
} catch (e) {
|
|
403
|
+
throw this.state = "created", e;
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
async destroy() {
|
|
407
|
+
if (this.state === "destroyed") return;
|
|
408
|
+
this.state = "destroying", this.config.onBeforeDestroy && await this.config.onBeforeDestroy(), this.emitInternal("kernel:destroy", { timestamp: Date.now() }), await Promise.allSettled(this.pendingInits.values());
|
|
409
|
+
let e;
|
|
410
|
+
try {
|
|
411
|
+
e = this.resolver.resolve();
|
|
412
|
+
} catch {
|
|
413
|
+
e = Array.from(this.plugins.keys());
|
|
414
|
+
}
|
|
415
|
+
await this.lifecycle.destroy(this.plugins, e, this.errorBoundary, (t) => this.emitInternal("plugin:destroy", { name: t })), this.state = "destroyed", this.emitInternal("kernel:destroyed", { timestamp: Date.now() }), this.events.clear(), this.config.onAfterDestroy && await this.config.onAfterDestroy();
|
|
416
|
+
}
|
|
417
|
+
isInitialized() {
|
|
418
|
+
return this.state === "ready";
|
|
419
|
+
}
|
|
420
|
+
isDestroyed() {
|
|
421
|
+
return this.state === "destroyed";
|
|
422
|
+
}
|
|
423
|
+
getPlugin(e) {
|
|
424
|
+
return this.plugins.get(e)?.plugin;
|
|
425
|
+
}
|
|
426
|
+
hasPlugin(e) {
|
|
427
|
+
return this.plugins.has(e);
|
|
428
|
+
}
|
|
429
|
+
listPlugins() {
|
|
430
|
+
return Array.from(this.plugins.values()).map((e) => e.plugin);
|
|
431
|
+
}
|
|
432
|
+
getPluginNames() {
|
|
433
|
+
return Array.from(this.plugins.keys());
|
|
434
|
+
}
|
|
435
|
+
getDependencyGraph() {
|
|
436
|
+
return this.resolver.getGraph();
|
|
437
|
+
}
|
|
438
|
+
on(e, t) {
|
|
439
|
+
return this.events.on(e, t);
|
|
440
|
+
}
|
|
441
|
+
once(e, t) {
|
|
442
|
+
return this.events.once(e, t);
|
|
443
|
+
}
|
|
444
|
+
off(e, t) {
|
|
445
|
+
this.events.off(e, t);
|
|
446
|
+
}
|
|
447
|
+
emit(e, t) {
|
|
448
|
+
this.events.emit(e, t);
|
|
449
|
+
}
|
|
450
|
+
onWildcard(e) {
|
|
451
|
+
return this.events.onWildcard(e);
|
|
452
|
+
}
|
|
453
|
+
onPattern(e, t) {
|
|
454
|
+
return this.events.onPattern(e, t);
|
|
455
|
+
}
|
|
456
|
+
getContext() {
|
|
457
|
+
return this.context.get();
|
|
458
|
+
}
|
|
459
|
+
updateContext(e) {
|
|
460
|
+
this.context.update(e);
|
|
461
|
+
}
|
|
462
|
+
deepUpdateContext(e) {
|
|
463
|
+
this.context.deepUpdate(e);
|
|
464
|
+
}
|
|
465
|
+
};
|
|
466
|
+
function W(d) {
|
|
467
|
+
return new c(d);
|
|
468
|
+
}
|
|
13
469
|
|
|
14
470
|
// src/constants.ts
|
|
15
471
|
var ANSI_MODIFIERS = {
|
|
@@ -331,17 +787,17 @@ function createProxyPigment(kernel, styles = []) {
|
|
|
331
787
|
if (prop === "supportsColor") {
|
|
332
788
|
return ctx.colorSupport;
|
|
333
789
|
}
|
|
334
|
-
if (MODIFIER_NAMES.
|
|
790
|
+
if (MODIFIER_NAMES.some((name) => name === prop)) {
|
|
335
791
|
const modifierStyle = generateModifierCode(prop);
|
|
336
792
|
return createProxyPigment(kernel, [...styles, modifierStyle]);
|
|
337
793
|
}
|
|
338
|
-
if (COLOR_NAMES.
|
|
794
|
+
if (COLOR_NAMES.some((name) => name === prop) || BACKGROUND_COLOR_NAMES.some((name) => name === prop)) {
|
|
339
795
|
const background = prop.startsWith("bg");
|
|
340
796
|
const colorName = background ? prop.slice(2) : prop;
|
|
341
797
|
const colorStyle = generateColorCode(colorName, background);
|
|
342
798
|
return createProxyPigment(kernel, [...styles, colorStyle]);
|
|
343
799
|
}
|
|
344
|
-
if (EXTENDED_COLOR_METHODS.
|
|
800
|
+
if (EXTENDED_COLOR_METHODS.some((name) => name === prop)) {
|
|
345
801
|
if (prop === "ansi256" || prop === "bgAnsi256") {
|
|
346
802
|
return (code) => {
|
|
347
803
|
const background = prop === "bgAnsi256";
|
|
@@ -369,13 +825,13 @@ function createProxyPigment(kernel, styles = []) {
|
|
|
369
825
|
};
|
|
370
826
|
}
|
|
371
827
|
if (prop === "hsl" || prop === "bgHsl") {
|
|
372
|
-
return (
|
|
828
|
+
return (h2, s, l) => {
|
|
373
829
|
const background = prop === "bgHsl";
|
|
374
830
|
const hslToRgb2 = ctx.utils?.hslToRgb;
|
|
375
831
|
if (!hslToRgb2) {
|
|
376
832
|
throw new Error("hslToRgb utility not available");
|
|
377
833
|
}
|
|
378
|
-
const { r, g, b } = hslToRgb2(
|
|
834
|
+
const { r, g, b } = hslToRgb2(h2, s, l);
|
|
379
835
|
const rgbStyle = generateRgbCode(r, g, b, background);
|
|
380
836
|
return createProxyPigment(kernel, [...styles, rgbStyle]);
|
|
381
837
|
};
|
|
@@ -383,9 +839,9 @@ function createProxyPigment(kernel, styles = []) {
|
|
|
383
839
|
}
|
|
384
840
|
if (prop === "visible") {
|
|
385
841
|
const colorSupport = ctx.colorSupport;
|
|
386
|
-
return (text) => colorSupport.level > 0 ? text : "";
|
|
842
|
+
return (text) => colorSupport.level > 0 ? applyStyles(String(text), styles) : "";
|
|
387
843
|
}
|
|
388
|
-
return target
|
|
844
|
+
return Reflect.get(target, prop);
|
|
389
845
|
},
|
|
390
846
|
apply(_target, _thisArg, argArray) {
|
|
391
847
|
const text = String(argArray[0] ?? "");
|
|
@@ -415,7 +871,7 @@ function applyStyles(text, styles) {
|
|
|
415
871
|
function hexToRgb(hex) {
|
|
416
872
|
const hexString = hex.trim().replace(/^#/, "");
|
|
417
873
|
if (hexString.length === 3) {
|
|
418
|
-
const expanded = hexString.split("").map((
|
|
874
|
+
const expanded = hexString.split("").map((c2) => c2 + c2).join("");
|
|
419
875
|
return parseExpandedHex(expanded);
|
|
420
876
|
}
|
|
421
877
|
if (hexString.length === 6) {
|
|
@@ -442,8 +898,8 @@ function validateHex(hex) {
|
|
|
442
898
|
}
|
|
443
899
|
|
|
444
900
|
// src/utils/hsl-to-rgb.ts
|
|
445
|
-
function hslToRgb(
|
|
446
|
-
const hNormalized =
|
|
901
|
+
function hslToRgb(h2, s, l) {
|
|
902
|
+
const hNormalized = h2 / 360;
|
|
447
903
|
const sNormalized = s / 100;
|
|
448
904
|
const lNormalized = l / 100;
|
|
449
905
|
let r;
|
|
@@ -686,13 +1142,13 @@ var BuilderPigment = class {
|
|
|
686
1142
|
}
|
|
687
1143
|
return this;
|
|
688
1144
|
}
|
|
689
|
-
hsl(
|
|
690
|
-
const { r, g, b } = hslToRgb(
|
|
1145
|
+
hsl(h2, s, l) {
|
|
1146
|
+
const { r, g, b } = hslToRgb(h2, s, l);
|
|
691
1147
|
this.styles.push(generateRgbCode(r, g, b, false));
|
|
692
1148
|
return this;
|
|
693
1149
|
}
|
|
694
|
-
bgHsl(
|
|
695
|
-
const { r, g, b } = hslToRgb(
|
|
1150
|
+
bgHsl(h2, s, l) {
|
|
1151
|
+
const { r, g, b } = hslToRgb(h2, s, l);
|
|
696
1152
|
this.styles.push(generateRgbCode(r, g, b, true));
|
|
697
1153
|
return this;
|
|
698
1154
|
}
|
|
@@ -776,69 +1232,31 @@ function detectColorSupport(overrideLevel) {
|
|
|
776
1232
|
};
|
|
777
1233
|
return cachedColorSupport;
|
|
778
1234
|
}
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
has256: false,
|
|
784
|
-
has16m: false
|
|
785
|
-
};
|
|
1235
|
+
const env = process.env;
|
|
1236
|
+
const forced = parseForceColor(env.FORCE_COLOR);
|
|
1237
|
+
if (forced !== void 0) {
|
|
1238
|
+
cachedColorSupport = toColorSupport(forced);
|
|
786
1239
|
return cachedColorSupport;
|
|
787
1240
|
}
|
|
788
|
-
if (
|
|
789
|
-
|
|
790
|
-
const clampedLevel = Math.max(0, Math.min(3, level));
|
|
791
|
-
cachedColorSupport = {
|
|
792
|
-
level: clampedLevel,
|
|
793
|
-
hasBasic: clampedLevel >= 1,
|
|
794
|
-
has256: clampedLevel >= 2,
|
|
795
|
-
has16m: clampedLevel >= 3
|
|
796
|
-
};
|
|
1241
|
+
if (env.NO_COLOR) {
|
|
1242
|
+
cachedColorSupport = toColorSupport(0);
|
|
797
1243
|
return cachedColorSupport;
|
|
798
1244
|
}
|
|
799
|
-
if (!hasTTY() && !
|
|
800
|
-
cachedColorSupport =
|
|
801
|
-
level: 0,
|
|
802
|
-
hasBasic: false,
|
|
803
|
-
has256: false,
|
|
804
|
-
has16m: false
|
|
805
|
-
};
|
|
1245
|
+
if (!hasTTY() && !env.CI || env.TERM === "dumb") {
|
|
1246
|
+
cachedColorSupport = toColorSupport(0);
|
|
806
1247
|
return cachedColorSupport;
|
|
807
1248
|
}
|
|
808
|
-
const term =
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
hasBasic: true,
|
|
813
|
-
has256: true,
|
|
814
|
-
has16m: true
|
|
815
|
-
};
|
|
1249
|
+
const term = env.TERM ?? "";
|
|
1250
|
+
const colorTerm = (env.COLORTERM ?? "").toLowerCase();
|
|
1251
|
+
if (colorTerm === "truecolor" || colorTerm === "24bit" || term.includes("truecolor") || term.includes("24bit")) {
|
|
1252
|
+
cachedColorSupport = toColorSupport(3);
|
|
816
1253
|
return cachedColorSupport;
|
|
817
1254
|
}
|
|
818
1255
|
if (term.includes("256color")) {
|
|
819
|
-
cachedColorSupport =
|
|
820
|
-
level: 2,
|
|
821
|
-
hasBasic: true,
|
|
822
|
-
has256: true,
|
|
823
|
-
has16m: false
|
|
824
|
-
};
|
|
1256
|
+
cachedColorSupport = toColorSupport(2);
|
|
825
1257
|
return cachedColorSupport;
|
|
826
1258
|
}
|
|
827
|
-
|
|
828
|
-
cachedColorSupport = {
|
|
829
|
-
level: 1,
|
|
830
|
-
hasBasic: true,
|
|
831
|
-
has256: false,
|
|
832
|
-
has16m: false
|
|
833
|
-
};
|
|
834
|
-
return cachedColorSupport;
|
|
835
|
-
}
|
|
836
|
-
cachedColorSupport = {
|
|
837
|
-
level: 1,
|
|
838
|
-
hasBasic: true,
|
|
839
|
-
has256: false,
|
|
840
|
-
has16m: false
|
|
841
|
-
};
|
|
1259
|
+
cachedColorSupport = toColorSupport(1);
|
|
842
1260
|
return cachedColorSupport;
|
|
843
1261
|
}
|
|
844
1262
|
function supportsColor() {
|
|
@@ -847,6 +1265,18 @@ function supportsColor() {
|
|
|
847
1265
|
function resetColorSupportCache() {
|
|
848
1266
|
cachedColorSupport = void 0;
|
|
849
1267
|
}
|
|
1268
|
+
function parseForceColor(value) {
|
|
1269
|
+
if (value === void 0) return void 0;
|
|
1270
|
+
const normalized = value.trim().toLowerCase();
|
|
1271
|
+
if (normalized === "" || normalized === "true") return 1;
|
|
1272
|
+
if (normalized === "false") return 0;
|
|
1273
|
+
const level = Number.parseInt(normalized, 10);
|
|
1274
|
+
if (Number.isNaN(level)) return 1;
|
|
1275
|
+
return Math.max(0, Math.min(3, level));
|
|
1276
|
+
}
|
|
1277
|
+
function toColorSupport(level) {
|
|
1278
|
+
return { level, hasBasic: level >= 1, has256: level >= 2, has16m: level >= 3 };
|
|
1279
|
+
}
|
|
850
1280
|
|
|
851
1281
|
// src/utils/rgb-to-ansi256.ts
|
|
852
1282
|
function rgbToAnsi256(r, g, b) {
|
|
@@ -974,11 +1404,11 @@ function trueColorPlugin() {
|
|
|
974
1404
|
k.emit("color:rgb", { ...rgb, background });
|
|
975
1405
|
});
|
|
976
1406
|
k.on("color:hsl", (data) => {
|
|
977
|
-
const { h, s, l, background } = data;
|
|
1407
|
+
const { h: h2, s, l, background } = data;
|
|
978
1408
|
if (!utils?.hslToRgb) {
|
|
979
1409
|
throw new Error("hslToRgb utility not available");
|
|
980
1410
|
}
|
|
981
|
-
const rgb = utils.hslToRgb(
|
|
1411
|
+
const rgb = utils.hslToRgb(h2, s, l);
|
|
982
1412
|
k.emit("color:rgb", { ...rgb, background });
|
|
983
1413
|
});
|
|
984
1414
|
}
|
|
@@ -1014,7 +1444,8 @@ function environmentPlugin() {
|
|
|
1014
1444
|
k.updateContext({
|
|
1015
1445
|
environment: {
|
|
1016
1446
|
isCI: ci,
|
|
1017
|
-
|
|
1447
|
+
// NO_COLOR only counts when non-empty (no-color.org)
|
|
1448
|
+
noColor: Boolean(process.env.NO_COLOR),
|
|
1018
1449
|
forceColor: process.env.FORCE_COLOR !== void 0,
|
|
1019
1450
|
term: process.env.TERM ?? ""
|
|
1020
1451
|
}
|
|
@@ -1023,13 +1454,13 @@ function environmentPlugin() {
|
|
|
1023
1454
|
k.on("environment:check", () => {
|
|
1024
1455
|
const ctx = k.getContext();
|
|
1025
1456
|
const env = ctx.environment;
|
|
1457
|
+
if (env?.forceColor) {
|
|
1458
|
+
const level = parseForceColor(process.env.FORCE_COLOR) ?? 1;
|
|
1459
|
+
return { level, enabled: level > 0 };
|
|
1460
|
+
}
|
|
1026
1461
|
if (env?.noColor) {
|
|
1027
1462
|
return { level: 0, enabled: false };
|
|
1028
1463
|
}
|
|
1029
|
-
if (env?.forceColor) {
|
|
1030
|
-
const level = parseInt(process.env.FORCE_COLOR ?? "0", 10);
|
|
1031
|
-
return { level: Math.max(0, Math.min(3, level)), enabled: true };
|
|
1032
|
-
}
|
|
1033
1464
|
if (ci) {
|
|
1034
1465
|
return { level: 1, enabled: colorSupport.hasBasic };
|
|
1035
1466
|
}
|
|
@@ -1056,7 +1487,7 @@ function createPigment(options) {
|
|
|
1056
1487
|
const noColor = options?.noColor ?? false;
|
|
1057
1488
|
const forceColor = options?.forceColor ?? false;
|
|
1058
1489
|
const level = noColor ? 0 : forceColor ? options?.level ?? 3 : colorSupport.level;
|
|
1059
|
-
const kernel =
|
|
1490
|
+
const kernel = W({
|
|
1060
1491
|
context: {
|
|
1061
1492
|
colorSupport: { ...colorSupport, level },
|
|
1062
1493
|
level,
|
|
@@ -1082,7 +1513,7 @@ function createPigment(options) {
|
|
|
1082
1513
|
kernel.use(plugin);
|
|
1083
1514
|
}
|
|
1084
1515
|
}
|
|
1085
|
-
kernel.init();
|
|
1516
|
+
void kernel.init();
|
|
1086
1517
|
const pigment2 = createProxyPigment(kernel);
|
|
1087
1518
|
const builder = new BuilderPigment(kernel);
|
|
1088
1519
|
Object.defineProperty(pigment2, "builder", { value: builder, enumerable: false });
|
|
@@ -1228,8 +1659,9 @@ var Chalk = class {
|
|
|
1228
1659
|
reset(text) {
|
|
1229
1660
|
return this.pigment.reset(text);
|
|
1230
1661
|
}
|
|
1662
|
+
/** Like chalk: returns the text only when colors are supported, otherwise ''. */
|
|
1231
1663
|
visible(text) {
|
|
1232
|
-
return this.supportsColor.level > 0 ? text :
|
|
1664
|
+
return this.supportsColor.level > 0 ? text : "";
|
|
1233
1665
|
}
|
|
1234
1666
|
rgb(r, g, b) {
|
|
1235
1667
|
const coloredPigment = this.pigment.rgb(r, g, b);
|
|
@@ -1321,9 +1753,6 @@ function compose(...stylers) {
|
|
|
1321
1753
|
};
|
|
1322
1754
|
}
|
|
1323
1755
|
|
|
1324
|
-
// src/index.ts
|
|
1325
|
-
import { createKernel as createKernel2 } from "@oxog/plugin";
|
|
1326
|
-
|
|
1327
1756
|
// src/plugins/index.ts
|
|
1328
1757
|
var plugins_exports = {};
|
|
1329
1758
|
__export(plugins_exports, {
|
|
@@ -1630,7 +2059,8 @@ function parseStyleExpression(expression, text, kernel) {
|
|
|
1630
2059
|
}
|
|
1631
2060
|
}
|
|
1632
2061
|
if (typeof current === "function") {
|
|
1633
|
-
|
|
2062
|
+
const style = current;
|
|
2063
|
+
return style(text);
|
|
1634
2064
|
}
|
|
1635
2065
|
return text;
|
|
1636
2066
|
}
|
|
@@ -1646,7 +2076,7 @@ function pigmentPlugin(options) {
|
|
|
1646
2076
|
const k = kernel;
|
|
1647
2077
|
const themeColors = THEME_PRESETS[theme] ?? THEME_PRESETS.monokai;
|
|
1648
2078
|
const pigment2 = createPigment({ level });
|
|
1649
|
-
k
|
|
2079
|
+
Object.assign(k, { pigment: pigment2 });
|
|
1650
2080
|
k.updateContext({
|
|
1651
2081
|
ecosystem: {
|
|
1652
2082
|
theme,
|
|
@@ -1770,7 +2200,7 @@ export {
|
|
|
1770
2200
|
blueBright,
|
|
1771
2201
|
bold,
|
|
1772
2202
|
compose,
|
|
1773
|
-
|
|
2203
|
+
W as createKernel,
|
|
1774
2204
|
createPigment,
|
|
1775
2205
|
cyan,
|
|
1776
2206
|
cyanBright,
|