@ix-xs/node-comfort 1.0.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/core/CLI.js ADDED
@@ -0,0 +1,95 @@
1
+ const { spawn, execSync } = require("node:child_process");
2
+ const EventEmitter = require("node:events");
3
+
4
+ class cli_stream {
5
+ #processus;
6
+ #events = new EventEmitter();
7
+
8
+ constructor(command, args) {
9
+ this.#processus = spawn(command, args, {
10
+ shell: true,
11
+ stdio: ["ignore", "pipe", "pipe"],
12
+ });
13
+
14
+ this.#processus.stdout.on("data", (data) => {
15
+ this.#events.emit("data", data.toString());
16
+ });
17
+ this.#processus.stderr.on("data", (data) => {
18
+ this.#events.emit("data", data.toString());
19
+ });
20
+ this.#processus.on("error", (error) => {
21
+ this.#events.emit("error", error);
22
+ });
23
+ this.#processus.on("exit", async (code, signal) => {
24
+ await this.stop();
25
+ });
26
+ this.#processus.on("close", (code, signal) => {
27
+ this.#events.emit("stopped", code, signal);
28
+ });
29
+ }
30
+
31
+ /**
32
+ * @typedef {"error"|"data"|"stopped"} EventName
33
+ * @typedef {{
34
+ * error: (error: Error) => void|Promise<void>,
35
+ * data: (message: string) => void|Promise<void>,
36
+ * stopped: (code: number, signal: keyof import("node:child_process").ChildProcessEventMap) => void|Promise<void>,
37
+ * }} EventHandler
38
+ */
39
+ /**
40
+ * @template {EventName} E
41
+ * @param {E} event
42
+ * @param {EventHandler[E]} handler
43
+ */
44
+ on(event, handler) {
45
+ this.#events.on(event, handler);
46
+ return this;
47
+ }
48
+
49
+ async stop() {
50
+ return new Promise((resolve) => {
51
+ if (!this.#processus || this.#processus.killed) {
52
+ this.#processus = null;
53
+ return resolve();
54
+ }
55
+
56
+ let resolved = false;
57
+
58
+ const done = () => {
59
+ if (!resolved) {
60
+ resolved = true;
61
+ this.#processus = null;
62
+ resolve();
63
+ }
64
+ };
65
+
66
+ this.#processus.once("exit", done);
67
+
68
+ this.#processus.kill("SIGTERM");
69
+
70
+ setTimeout(() => {
71
+ if (this.#processus && !this.#processus.killed && !resolved) {
72
+ this.#processus.kill("SIGKILL");
73
+ }
74
+ done();
75
+ }, 5000);
76
+ });
77
+ }
78
+ }
79
+
80
+ /**
81
+ * @module CLI
82
+ * @description
83
+ */
84
+ module.exports = {
85
+ /**
86
+ * @param {string} command
87
+ */
88
+ cli_run: (command) => {
89
+ return execSync(command, {
90
+ shell: true,
91
+ stdio: "pipe",
92
+ });
93
+ },
94
+ cli_stream,
95
+ };
@@ -0,0 +1,127 @@
1
+ const { types } = require("node:util");
2
+
3
+ /**
4
+ * @module Checker
5
+ * @description
6
+ * Provides a collection of utility functions for checking JavaScript value types.
7
+ *
8
+ * These helpers wrap common JavaScript type checks and selected `node:util.types`
9
+ * methods for specific built-in objects such as Map, Set, Date, and Error.
10
+ *
11
+ * @example
12
+ * const nodeComfort = require("@ix-xs/node-comfort");
13
+ *
14
+ * console.log(nodeComfort.isArray([1, 2, 3])); // true
15
+ * console.log(nodeComfort.isNumber(42)); // true
16
+ * console.log(nodeComfort.isDate(new Date())); // true
17
+ */
18
+
19
+ module.exports = {
20
+ /**
21
+ * Checks if the value is an array.
22
+ * @param {*} value - The value to check.
23
+ * @returns {boolean} Returns `true` if the value is an array, otherwise `false`.
24
+ */
25
+ isArray(value) {
26
+ return Array.isArray(value);
27
+ },
28
+
29
+ /**
30
+ * Checks if the value is a valid number (not NaN).
31
+ * @param {*} value - The value to check.
32
+ * @returns {boolean} Returns `true` if the value is a valid number, otherwise `false`.
33
+ */
34
+ isNumber(value) {
35
+ return typeof value === "number" && !Number.isNaN(value);
36
+ },
37
+
38
+ /**
39
+ * Checks if the value is a function.
40
+ * @param {*} value - The value to check.
41
+ * @returns {boolean} Returns `true` if the value is a function, otherwise `false`.
42
+ */
43
+ isFunction(value) {
44
+ return typeof value === "function";
45
+ },
46
+
47
+ /**
48
+ * Checks if the value is an object (and not `null`).
49
+ * @param {*} value - The value to check.
50
+ * @returns {boolean} Returns `true` if the value is a non-null object, otherwise `false`.
51
+ */
52
+ isObject(value) {
53
+ return typeof value === "object" && value !== null;
54
+ },
55
+
56
+ /**
57
+ * Checks if the value is a boolean.
58
+ * @param {*} value - The value to check.
59
+ * @returns {boolean} Returns `true` if the value is a boolean, otherwise `false`.
60
+ */
61
+ isBoolean(value) {
62
+ return typeof value === "boolean";
63
+ },
64
+
65
+ /**
66
+ * Checks if the value is a string.
67
+ * @param {*} value - The value to check.
68
+ * @returns {boolean} Returns `true` if the value is a string, otherwise `false`.
69
+ */
70
+ isString(value) {
71
+ return typeof value === "string";
72
+ },
73
+
74
+ /**
75
+ * Checks if the value is `undefined`.
76
+ * @param {*} value - The value to check.
77
+ * @returns {boolean} Returns `true` if the value is `undefined`, otherwise `false`.
78
+ */
79
+ isUndefined(value) {
80
+ return typeof value === "undefined";
81
+ },
82
+
83
+ /**
84
+ * Checks if the value is `null`.
85
+ * @param {*} value - The value to check.
86
+ * @returns {boolean} Returns `true` if the value is `null`, otherwise `false`.
87
+ */
88
+ isNull(value) {
89
+ return value === null;
90
+ },
91
+
92
+ /**
93
+ * Checks if the value is a valid `Date` object.
94
+ * @param {*} value - The value to check.
95
+ * @returns {boolean} Returns `true` if the value is an instance of `Date`, otherwise `false`.
96
+ */
97
+ isDate(value) {
98
+ return types.isDate(value);
99
+ },
100
+
101
+ /**
102
+ * Checks if the value is a `Map` instance.
103
+ * @param {*} value - The value to check.
104
+ * @returns {boolean} Returns `true` if the value is a Map, otherwise `false`.
105
+ */
106
+ isMap(value) {
107
+ return types.isMap(value);
108
+ },
109
+
110
+ /**
111
+ * Checks if the value is a `Set` instance.
112
+ * @param {*} value - The value to check.
113
+ * @returns {boolean} Returns `true` if the value is a Set, otherwise `false`.
114
+ */
115
+ isSet(value) {
116
+ return types.isSet(value);
117
+ },
118
+
119
+ /**
120
+ * Checks if the value is a native JavaScript error (`Error`, `TypeError`, etc.).
121
+ * @param {*} value - The value to check.
122
+ * @returns {boolean} Returns `true` if the value is a native error, otherwise `false`.
123
+ */
124
+ isError(value) {
125
+ return types.isNativeError(value);
126
+ },
127
+ };