@ix-xs/node-comfort 1.0.57 → 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/src/cli.js DELETED
@@ -1,86 +0,0 @@
1
- const { execSync, spawn } = require("node:child_process");
2
- const EventEmitter = require("node:events");
3
-
4
- module.exports = {
5
- exec(command) {
6
- return execSync(command, {
7
- shell: true,
8
- stdio: "pipe",
9
- });
10
- },
11
- stream: class {
12
- #processus;
13
- #events = new EventEmitter();
14
-
15
- constructor(command, args) {
16
- this.#processus = spawn(command, args, {
17
- shell: true,
18
- stdio: ["ignore", "pipe", "pipe"],
19
- });
20
-
21
- this.#processus.stdout.on("data", (data) => {
22
- this.#events.emit("data", data.toString());
23
- });
24
- this.#processus.stderr.on("data", (data) => {
25
- this.#events.emit("data", data.toString());
26
- });
27
- this.#processus.on("error", (error) => {
28
- this.#events.emit("error", error);
29
- });
30
- this.#processus.on("exit", async (code, signal) => {
31
- await this.stop();
32
- });
33
- this.#processus.on("close", (code, signal) => {
34
- this.#events.emit("stopped", code, signal);
35
- });
36
- }
37
-
38
- /**
39
- * @typedef {"error"|"data"|"stopped"} EventName
40
- * @typedef {{
41
- * error: (error: Error) => void|Promise<void>,
42
- * data: (chunk: string) => void|Promise<void>,
43
- * stopped: (code: number|null, signal: keyof import("node:child_process").ChildProcessEventMap) => void|Promise<void>,
44
- * }} EventHandler
45
- */
46
- /**
47
- * @template {EventName} E
48
- * @param {E} event
49
- * @param {EventHandler[E]} handler
50
- */
51
- on(event, handler) {
52
- this.#events.on(event, handler);
53
- return this;
54
- }
55
-
56
- async stop() {
57
- return new Promise((resolve) => {
58
- if (!this.#processus || this.#processus.killed) {
59
- this.#processus = null;
60
- return resolve();
61
- }
62
-
63
- let resolved = false;
64
-
65
- const done = () => {
66
- if (!resolved) {
67
- resolved = true;
68
- this.#processus = null;
69
- resolve();
70
- }
71
- };
72
-
73
- this.#processus.once("exit", done);
74
-
75
- this.#processus.kill("SIGTERM");
76
-
77
- setTimeout(() => {
78
- if (this.#processus && !this.#processus.killed && !resolved) {
79
- this.#processus.kill("SIGKILL");
80
- }
81
- done();
82
- }, 5000);
83
- });
84
- }
85
- },
86
- };