@gesslar/toolkit 1.6.0 → 1.8.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/package.json +4 -4
- package/src/index.js +1 -0
- package/src/lib/DirectoryObject.js +25 -0
- package/src/lib/Notify.js +87 -0
- package/src/types/index.d.ts +1 -0
- package/src/types/lib/DirectoryObject.d.ts +14 -0
- package/src/types/lib/DirectoryObject.d.ts.map +1 -1
- package/src/types/lib/Notify.d.ts +55 -0
- package/src/types/lib/Notify.d.ts.map +1 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gesslar/toolkit",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.8.0",
|
|
4
4
|
"description": "Get in, bitches, we're going toolkitting.",
|
|
5
5
|
"main": "./src/index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -66,14 +66,14 @@
|
|
|
66
66
|
"ajv": "^8.17.1",
|
|
67
67
|
"globby": "^16.0.0",
|
|
68
68
|
"json5": "^2.2.3",
|
|
69
|
-
"yaml": "^2.8.
|
|
69
|
+
"yaml": "^2.8.2"
|
|
70
70
|
},
|
|
71
71
|
"devDependencies": {
|
|
72
72
|
"@gesslar/uglier": "^0.0.5",
|
|
73
73
|
"@stylistic/eslint-plugin": "^5.6.1",
|
|
74
74
|
"@types/node": "^24.10.1",
|
|
75
|
-
"@typescript-eslint/eslint-plugin": "^8.48.
|
|
76
|
-
"@typescript-eslint/parser": "^8.48.
|
|
75
|
+
"@typescript-eslint/eslint-plugin": "^8.48.1",
|
|
76
|
+
"@typescript-eslint/parser": "^8.48.1",
|
|
77
77
|
"eslint": "^9.39.1",
|
|
78
78
|
"eslint-plugin-jsdoc": "^61.4.1",
|
|
79
79
|
"globals": "^16.5.0"
|
package/src/index.js
CHANGED
|
@@ -18,6 +18,7 @@ export {default as DirectoryObject} from "./lib/DirectoryObject.js"
|
|
|
18
18
|
export {default as FileObject} from "./lib/FileObject.js"
|
|
19
19
|
export {default as FS} from "./lib/FS.js"
|
|
20
20
|
export {default as Glog} from "./lib/Glog.js"
|
|
21
|
+
export {default as Notify} from "./lib/Notify.js"
|
|
21
22
|
export {default as Schemer} from "./lib/Schemer.js"
|
|
22
23
|
export {default as Term} from "./lib/Term.js"
|
|
23
24
|
export {default as Terms} from "./lib/Terms.js"
|
|
@@ -355,4 +355,29 @@ export default class DirectoryObject extends FS {
|
|
|
355
355
|
|
|
356
356
|
return await fs.rmdir(this.path)
|
|
357
357
|
}
|
|
358
|
+
|
|
359
|
+
/**
|
|
360
|
+
* Checks if a file exists within this directory.
|
|
361
|
+
*
|
|
362
|
+
* @param {string} filename - The filename to check for
|
|
363
|
+
* @returns {Promise<boolean>} True if the file exists, false otherwise
|
|
364
|
+
*/
|
|
365
|
+
async hasFile(filename) {
|
|
366
|
+
const file = new FileObject(filename, this)
|
|
367
|
+
|
|
368
|
+
return await file.exists
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
/**
|
|
372
|
+
* Checks if a subdirectory exists within this directory.
|
|
373
|
+
*
|
|
374
|
+
* @param {string} dirname - The directory name to check for
|
|
375
|
+
* @returns {Promise<boolean>} True if the directory exists, false otherwise
|
|
376
|
+
*/
|
|
377
|
+
async hasDirectory(dirname) {
|
|
378
|
+
const resolved = FS.resolvePath(this.path, dirname)
|
|
379
|
+
const directory = new DirectoryObject(resolved)
|
|
380
|
+
|
|
381
|
+
return await directory.exists
|
|
382
|
+
}
|
|
358
383
|
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file Notify.js
|
|
3
|
+
* @description Node.js event notification system using EventEmitter.
|
|
4
|
+
* Provides a centralized API for event emission and handling.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import {EventEmitter} from "node:events"
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* @typedef {object} NotifyEventOptions
|
|
11
|
+
* @property {boolean} [once] - Whether the listener should be invoked only once.
|
|
12
|
+
* @property {AbortSignal} [signal] - An AbortSignal to remove the listener.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Notify class provides a thin wrapper around EventEmitter for centralized
|
|
17
|
+
* event handling in Node.js applications. Mirrors the browser Notify API.
|
|
18
|
+
*/
|
|
19
|
+
export default new class Notify {
|
|
20
|
+
/** @type {string} Display name for debugging. */
|
|
21
|
+
name = "Notify"
|
|
22
|
+
|
|
23
|
+
/** @type {EventEmitter} Internal event emitter */
|
|
24
|
+
#emitter = new EventEmitter()
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Emits an event without expecting a return value.
|
|
28
|
+
*
|
|
29
|
+
* @param {string} type - Event name to dispatch.
|
|
30
|
+
* @param {unknown} [payload] - Data to send with the event.
|
|
31
|
+
* @returns {void}
|
|
32
|
+
*/
|
|
33
|
+
emit(type, payload=undefined) {
|
|
34
|
+
this.#emitter.emit(type, payload)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Emits an event and returns the payload for simple request/response flows.
|
|
39
|
+
* Listeners can mutate the payload object to provide responses.
|
|
40
|
+
*
|
|
41
|
+
* @param {string} type - Event name to dispatch.
|
|
42
|
+
* @param {unknown} [payload] - Data to send with the event (will be returned).
|
|
43
|
+
* @returns {unknown} The payload after listeners have processed it.
|
|
44
|
+
*/
|
|
45
|
+
request(type, payload={}) {
|
|
46
|
+
this.#emitter.emit(type, payload)
|
|
47
|
+
|
|
48
|
+
return payload
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Registers a listener for the given event type.
|
|
53
|
+
*
|
|
54
|
+
* @param {string} type - Event name to listen for.
|
|
55
|
+
* @param {(payload: unknown) => void} handler - Listener callback.
|
|
56
|
+
* @param {EventEmitter} [emitter] - The EventEmitter to attach to. Default is internal emitter.
|
|
57
|
+
* @param {NotifyEventOptions} [options] - Options for the listener.
|
|
58
|
+
* @returns {() => void} Dispose function to unregister the handler.
|
|
59
|
+
*/
|
|
60
|
+
on(type, handler, emitter=this.#emitter, options=undefined) {
|
|
61
|
+
if(!(typeof type === "string" && type))
|
|
62
|
+
throw new Error("No event 'type' specified to listen for.")
|
|
63
|
+
|
|
64
|
+
if(typeof handler !== "function")
|
|
65
|
+
throw new Error("No handler function specified.")
|
|
66
|
+
|
|
67
|
+
if(options?.once) {
|
|
68
|
+
emitter.once(type, handler, options)
|
|
69
|
+
} else {
|
|
70
|
+
emitter.on(type, handler, options)
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return () => this.off(type, handler, emitter)
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Removes a previously registered listener for the given event type.
|
|
78
|
+
*
|
|
79
|
+
* @param {string} type - Event name to remove.
|
|
80
|
+
* @param {(payload: unknown) => void} handler - Listener callback to detach.
|
|
81
|
+
* @param {EventEmitter} [emitter] - The EventEmitter from which to remove. Default is internal emitter.
|
|
82
|
+
* @returns {void}
|
|
83
|
+
*/
|
|
84
|
+
off(type, handler, emitter=this.#emitter) {
|
|
85
|
+
emitter.off(type, handler)
|
|
86
|
+
}
|
|
87
|
+
}
|
package/src/types/index.d.ts
CHANGED
|
@@ -11,6 +11,7 @@ export { default as DirectoryObject } from "./lib/DirectoryObject.js";
|
|
|
11
11
|
export { default as FileObject } from "./lib/FileObject.js";
|
|
12
12
|
export { default as FS } from "./lib/FS.js";
|
|
13
13
|
export { default as Glog } from "./lib/Glog.js";
|
|
14
|
+
export { default as Notify } from "./lib/Notify.js";
|
|
14
15
|
export { default as Schemer } from "./lib/Schemer.js";
|
|
15
16
|
export { default as Term } from "./lib/Term.js";
|
|
16
17
|
export { default as Terms } from "./lib/Terms.js";
|
|
@@ -150,6 +150,20 @@ export default class DirectoryObject extends FS {
|
|
|
150
150
|
* await dir.delete() // Only works if directory is empty
|
|
151
151
|
*/
|
|
152
152
|
delete(): Promise<void>;
|
|
153
|
+
/**
|
|
154
|
+
* Checks if a file exists within this directory.
|
|
155
|
+
*
|
|
156
|
+
* @param {string} filename - The filename to check for
|
|
157
|
+
* @returns {Promise<boolean>} True if the file exists, false otherwise
|
|
158
|
+
*/
|
|
159
|
+
hasFile(filename: string): Promise<boolean>;
|
|
160
|
+
/**
|
|
161
|
+
* Checks if a subdirectory exists within this directory.
|
|
162
|
+
*
|
|
163
|
+
* @param {string} dirname - The directory name to check for
|
|
164
|
+
* @returns {Promise<boolean>} True if the directory exists, false otherwise
|
|
165
|
+
*/
|
|
166
|
+
hasDirectory(dirname: string): Promise<boolean>;
|
|
153
167
|
/**
|
|
154
168
|
* Custom inspect method for Node.js console.
|
|
155
169
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"DirectoryObject.d.ts","sourceRoot":"","sources":["../../lib/DirectoryObject.js"],"names":[],"mappings":"AAeA;;;;;;;;;;;;;GAaG;AACH;IA0BE;;;;OAIG;IACH,uBAFW,MAAM,EAsBhB;IAWD;;;;OAIG;IACH,UAFa,MAAM,CAalB;IAWD;;;;OAIG;IACH,cAFa,OAAO,CAAC,OAAO,CAAC,CAI5B;IAED;;;;OAIG;IACH,gBAFa,MAAM,CAIlB;IAED;;;;OAIG;IACH,YAFa,MAAM,CAIlB;IAED;;;;OAIG;IACH,WAFa,GAAG,CAIf;IAED;;;;OAIG;IACH,YAFa,MAAM,CAIlB;IAED;;;;OAIG;IACH,cAFa,MAAM,CAIlB;IAED;;;;OAIG;IACH,iBAFa,MAAM,CAIlB;IAED;;;;OAIG;IACH,WAFa,MAAM,CAIlB;IAED;;;;;;;OAOG;IACH,aALa,KAAK,CAAC,MAAM,CAAC,CAOzB;IAED;;;;OAIG;IACH,cAFa,OAAO,CAInB;IAED;;;;OAIG;IACH,mBAFa,OAAO,CAInB;IAiBD;;;;OAIG;IACH,QAFa,OAAO,CAAC;QAAC,KAAK,EAAE,KAAK,CAAC,UAAU,CAAC,CAAC;QAAC,WAAW,EAAE,KAAK,CAAC,eAAe,CAAC,CAAA;KAAC,CAAC,CAcpF;IAED;;;;;;;;;;;;OAYG;IACH,uBARW,MAAM,GACJ,OAAO,CAAC,IAAI,CAAC,CAqBzB;IA8BD;;;;;;;;;;;;;;;OAeG;IACH,cAZa,MAAM,CAclB;IAED;;;;;;;;;;;;;;OAcG;IACH,UARa,OAAO,CAAC,IAAI,CAAC,CAkBzB;
|
|
1
|
+
{"version":3,"file":"DirectoryObject.d.ts","sourceRoot":"","sources":["../../lib/DirectoryObject.js"],"names":[],"mappings":"AAeA;;;;;;;;;;;;;GAaG;AACH;IA0BE;;;;OAIG;IACH,uBAFW,MAAM,EAsBhB;IAWD;;;;OAIG;IACH,UAFa,MAAM,CAalB;IAWD;;;;OAIG;IACH,cAFa,OAAO,CAAC,OAAO,CAAC,CAI5B;IAED;;;;OAIG;IACH,gBAFa,MAAM,CAIlB;IAED;;;;OAIG;IACH,YAFa,MAAM,CAIlB;IAED;;;;OAIG;IACH,WAFa,GAAG,CAIf;IAED;;;;OAIG;IACH,YAFa,MAAM,CAIlB;IAED;;;;OAIG;IACH,cAFa,MAAM,CAIlB;IAED;;;;OAIG;IACH,iBAFa,MAAM,CAIlB;IAED;;;;OAIG;IACH,WAFa,MAAM,CAIlB;IAED;;;;;;;OAOG;IACH,aALa,KAAK,CAAC,MAAM,CAAC,CAOzB;IAED;;;;OAIG;IACH,cAFa,OAAO,CAInB;IAED;;;;OAIG;IACH,mBAFa,OAAO,CAInB;IAiBD;;;;OAIG;IACH,QAFa,OAAO,CAAC;QAAC,KAAK,EAAE,KAAK,CAAC,UAAU,CAAC,CAAC;QAAC,WAAW,EAAE,KAAK,CAAC,eAAe,CAAC,CAAA;KAAC,CAAC,CAcpF;IAED;;;;;;;;;;;;OAYG;IACH,uBARW,MAAM,GACJ,OAAO,CAAC,IAAI,CAAC,CAqBzB;IA8BD;;;;;;;;;;;;;;;OAeG;IACH,cAZa,MAAM,CAclB;IAED;;;;;;;;;;;;;;OAcG;IACH,UARa,OAAO,CAAC,IAAI,CAAC,CAkBzB;IAED;;;;;OAKG;IACH,kBAHW,MAAM,GACJ,OAAO,CAAC,OAAO,CAAC,CAM5B;IAED;;;;;OAKG;IACH,sBAHW,MAAM,GACJ,OAAO,CAAC,OAAO,CAAC,CAO5B;IAhRD;;;;OAIG;IACH,yBAFa,MAAM,CAIlB;;CA0QF;eAnXc,SAAS;oBAFN,UAAU;uBAGL,iBAAiB;iBAJvB,WAAW"}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
declare const _default: {
|
|
2
|
+
/** @type {string} Display name for debugging. */
|
|
3
|
+
name: string;
|
|
4
|
+
/** @type {EventEmitter} Internal event emitter */
|
|
5
|
+
"__#private@#emitter": EventEmitter;
|
|
6
|
+
/**
|
|
7
|
+
* Emits an event without expecting a return value.
|
|
8
|
+
*
|
|
9
|
+
* @param {string} type - Event name to dispatch.
|
|
10
|
+
* @param {unknown} [payload] - Data to send with the event.
|
|
11
|
+
* @returns {void}
|
|
12
|
+
*/
|
|
13
|
+
emit(type: string, payload?: unknown): void;
|
|
14
|
+
/**
|
|
15
|
+
* Emits an event and returns the payload for simple request/response flows.
|
|
16
|
+
* Listeners can mutate the payload object to provide responses.
|
|
17
|
+
*
|
|
18
|
+
* @param {string} type - Event name to dispatch.
|
|
19
|
+
* @param {unknown} [payload] - Data to send with the event (will be returned).
|
|
20
|
+
* @returns {unknown} The payload after listeners have processed it.
|
|
21
|
+
*/
|
|
22
|
+
request(type: string, payload?: unknown): unknown;
|
|
23
|
+
/**
|
|
24
|
+
* Registers a listener for the given event type.
|
|
25
|
+
*
|
|
26
|
+
* @param {string} type - Event name to listen for.
|
|
27
|
+
* @param {(payload: unknown) => void} handler - Listener callback.
|
|
28
|
+
* @param {EventEmitter} [emitter] - The EventEmitter to attach to. Default is internal emitter.
|
|
29
|
+
* @param {NotifyEventOptions} [options] - Options for the listener.
|
|
30
|
+
* @returns {() => void} Dispose function to unregister the handler.
|
|
31
|
+
*/
|
|
32
|
+
on(type: string, handler: (payload: unknown) => void, emitter?: EventEmitter, options?: NotifyEventOptions): () => void;
|
|
33
|
+
/**
|
|
34
|
+
* Removes a previously registered listener for the given event type.
|
|
35
|
+
*
|
|
36
|
+
* @param {string} type - Event name to remove.
|
|
37
|
+
* @param {(payload: unknown) => void} handler - Listener callback to detach.
|
|
38
|
+
* @param {EventEmitter} [emitter] - The EventEmitter from which to remove. Default is internal emitter.
|
|
39
|
+
* @returns {void}
|
|
40
|
+
*/
|
|
41
|
+
off(type: string, handler: (payload: unknown) => void, emitter?: EventEmitter): void;
|
|
42
|
+
};
|
|
43
|
+
export default _default;
|
|
44
|
+
export type NotifyEventOptions = {
|
|
45
|
+
/**
|
|
46
|
+
* - Whether the listener should be invoked only once.
|
|
47
|
+
*/
|
|
48
|
+
once?: boolean;
|
|
49
|
+
/**
|
|
50
|
+
* - An AbortSignal to remove the listener.
|
|
51
|
+
*/
|
|
52
|
+
signal?: AbortSignal;
|
|
53
|
+
};
|
|
54
|
+
import { EventEmitter } from "node:events";
|
|
55
|
+
//# sourceMappingURL=Notify.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Notify.d.ts","sourceRoot":"","sources":["../../lib/Notify.js"],"names":[],"mappings":";IAmBE,iDAAiD;UAAtC,MAAM;IAGjB,kDAAkD;2BAAvC,YAAY;IAGvB;;;;;;OAMG;eAHQ,MAAM,YACN,OAAO,GACL,IAAI;IAMjB;;;;;;;OAOG;kBAHQ,MAAM,YACN,OAAO,GACL,OAAO;IAQpB;;;;;;;;OAQG;aALQ,MAAM,WACN,CAAC,OAAO,EAAE,OAAO,KAAK,IAAI,YAC1B,YAAY,YACZ,kBAAkB,GAChB,MAAM,IAAI;IAkBvB;;;;;;;OAOG;cAJQ,MAAM,WACN,CAAC,OAAO,EAAE,OAAO,KAAK,IAAI,YAC1B,YAAY,GACV,IAAI;;;;;;;WAvEL,OAAO;;;;aACP,WAAW;;6BALE,aAAa"}
|