@nu-art/commando 0.204.28 → 0.204.30
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.d.ts +16 -2
- package/core/cli.js +87 -18
- package/package.json +1 -1
package/core/cli.d.ts
CHANGED
|
@@ -19,6 +19,8 @@ type Options = {
|
|
|
19
19
|
indentation: number;
|
|
20
20
|
};
|
|
21
21
|
export declare class BaseCLI extends Logger {
|
|
22
|
+
protected stdoutProcessors: ((stdout: string) => void)[];
|
|
23
|
+
protected stderrProcessors: ((stdout: string) => void)[];
|
|
22
24
|
protected commands: string[];
|
|
23
25
|
private indentation;
|
|
24
26
|
protected _debug: boolean;
|
|
@@ -40,6 +42,10 @@ export declare class BaseCLI extends Logger {
|
|
|
40
42
|
*/
|
|
41
43
|
readonly append: (command: string) => this;
|
|
42
44
|
setUID(uuid: string): void;
|
|
45
|
+
addStdoutProcessor(processor: (stdout: string) => void): void;
|
|
46
|
+
addStderrProcessor(processor: (stderr: string) => void): void;
|
|
47
|
+
removeStdoutProcessor(processor: (stdout: string) => void): void;
|
|
48
|
+
removeStderrProcessor(processor: (stderr: string) => void): void;
|
|
43
49
|
}
|
|
44
50
|
export declare class CliInteractive extends BaseCLI {
|
|
45
51
|
private shell;
|
|
@@ -47,7 +53,7 @@ export declare class CliInteractive extends BaseCLI {
|
|
|
47
53
|
execute: () => Promise<void>;
|
|
48
54
|
endInteractive: () => void;
|
|
49
55
|
kill: (signal?: NodeJS.Signals | number) => boolean;
|
|
50
|
-
gracefullyKill: () => Promise<
|
|
56
|
+
gracefullyKill: (pid?: number) => Promise<unknown>;
|
|
51
57
|
}
|
|
52
58
|
export declare class Cli extends BaseCLI {
|
|
53
59
|
private cliOptions;
|
|
@@ -98,14 +104,22 @@ export declare class Commando {
|
|
|
98
104
|
stdout: string;
|
|
99
105
|
stderr: string;
|
|
100
106
|
}>;
|
|
107
|
+
addStdoutProcessor: (processor: (stdout: string) => void) => this;
|
|
108
|
+
addStderrProcessor: (processor: (stderr: string) => void) => this;
|
|
109
|
+
removeStdoutProcessor: (processor: (stdout: string) => void) => this;
|
|
110
|
+
removeStderrProcessor: (processor: (stderr: string) => void) => this;
|
|
101
111
|
private constructor();
|
|
102
112
|
}
|
|
103
113
|
export declare class CommandoInteractive {
|
|
104
114
|
cli: CliInteractive;
|
|
105
115
|
static create<T extends Constructor<CliWrapper>[]>(...plugins: T): CommandoInteractive & Commando & import("./class-merger").MergeTypes<T>;
|
|
116
|
+
addStdoutProcessor: (processor: (stdout: string) => void) => this;
|
|
117
|
+
addStderrProcessor: (processor: (stderr: string) => void) => this;
|
|
118
|
+
removeStdoutProcessor: (processor: (stdout: string) => void) => this;
|
|
119
|
+
removeStderrProcessor: (processor: (stderr: string) => void) => this;
|
|
106
120
|
setUID: (uid: string) => this;
|
|
107
121
|
close: () => this;
|
|
108
122
|
kill: (signal?: NodeJS.Signals | number) => boolean;
|
|
109
|
-
gracefullyKill: () => Promise<void>;
|
|
123
|
+
gracefullyKill: (pid?: number) => Promise<void>;
|
|
110
124
|
}
|
|
111
125
|
export {};
|
package/core/cli.js
CHANGED
|
@@ -20,6 +20,8 @@ class BaseCLI extends ts_common_1.Logger {
|
|
|
20
20
|
*/
|
|
21
21
|
constructor(options = defaultOptions) {
|
|
22
22
|
super();
|
|
23
|
+
this.stdoutProcessors = [];
|
|
24
|
+
this.stderrProcessors = [];
|
|
23
25
|
this.commands = [];
|
|
24
26
|
this.indentation = 0;
|
|
25
27
|
this._debug = false;
|
|
@@ -52,52 +54,75 @@ class BaseCLI extends ts_common_1.Logger {
|
|
|
52
54
|
setUID(uuid) {
|
|
53
55
|
this.setTag(uuid);
|
|
54
56
|
}
|
|
57
|
+
addStdoutProcessor(processor) {
|
|
58
|
+
this.stdoutProcessors.push(processor);
|
|
59
|
+
}
|
|
60
|
+
addStderrProcessor(processor) {
|
|
61
|
+
this.stderrProcessors.push(processor);
|
|
62
|
+
}
|
|
63
|
+
removeStdoutProcessor(processor) {
|
|
64
|
+
(0, ts_common_1.removeItemFromArray)(this.stdoutProcessors, processor);
|
|
65
|
+
}
|
|
66
|
+
removeStderrProcessor(processor) {
|
|
67
|
+
(0, ts_common_1.removeItemFromArray)(this.stderrProcessors, processor);
|
|
68
|
+
}
|
|
55
69
|
}
|
|
56
70
|
exports.BaseCLI = BaseCLI;
|
|
57
71
|
class CliInteractive extends BaseCLI {
|
|
58
72
|
constructor() {
|
|
73
|
+
var _a, _b;
|
|
59
74
|
super();
|
|
60
75
|
this.execute = async () => {
|
|
76
|
+
var _a;
|
|
61
77
|
const command = this.commands.join(this.option.newlineDelimiter);
|
|
62
78
|
if (this._debug)
|
|
63
79
|
this.logDebug(`executing: `, `"""\n${command}\n"""`);
|
|
64
|
-
this.shell.stdin.write(command + this.option.newlineDelimiter, 'utf-8', (err) => {
|
|
80
|
+
(_a = this.shell.stdin) === null || _a === void 0 ? void 0 : _a.write(command + this.option.newlineDelimiter, 'utf-8', (err) => {
|
|
65
81
|
if (err)
|
|
66
82
|
this.logError(`error`, err);
|
|
67
83
|
});
|
|
68
84
|
this.commands = [];
|
|
69
85
|
};
|
|
70
86
|
this.endInteractive = () => {
|
|
71
|
-
|
|
87
|
+
var _a;
|
|
88
|
+
(_a = this.shell.stdin) === null || _a === void 0 ? void 0 : _a.end();
|
|
72
89
|
};
|
|
73
90
|
this.kill = (signal) => {
|
|
74
91
|
return this.shell.kill(signal);
|
|
75
92
|
};
|
|
76
|
-
this.gracefullyKill = async () => {
|
|
93
|
+
this.gracefullyKill = async (pid) => {
|
|
77
94
|
return new Promise((resolve, reject) => {
|
|
78
95
|
console.log('Killing process');
|
|
79
|
-
this.shell.on('exit', (code, signal) => {
|
|
80
|
-
console.log(
|
|
96
|
+
this.shell.on('exit', async (code, signal) => {
|
|
97
|
+
console.log(`Process Killed ${signal}`);
|
|
98
|
+
await (0, ts_common_1.sleep)(5000);
|
|
81
99
|
resolve();
|
|
82
100
|
});
|
|
83
|
-
if (
|
|
101
|
+
if (pid) {
|
|
102
|
+
console.log(`KILLING PID: ${pid}`);
|
|
103
|
+
process.kill(pid, 'SIGINT');
|
|
104
|
+
}
|
|
105
|
+
else {
|
|
106
|
+
console.log(`KILLING SHELL WITH SIGINT`);
|
|
84
107
|
this.shell.kill('SIGINT');
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
});
|
|
108
|
+
}
|
|
109
|
+
}).then(() => (0, ts_common_1.sleep)(10000));
|
|
88
110
|
};
|
|
89
111
|
this.shell = (0, node_child_process_1.spawn)('/bin/bash', {
|
|
90
|
-
|
|
112
|
+
detached: true,
|
|
113
|
+
shell: true
|
|
91
114
|
});
|
|
92
115
|
// Handle shell output (stdout)
|
|
93
116
|
const printer = (data) => {
|
|
94
117
|
const message = data.toString().trim();
|
|
95
118
|
if (!message.length)
|
|
96
119
|
return;
|
|
120
|
+
this.stdoutProcessors.forEach(processor => processor(message));
|
|
121
|
+
this.stderrProcessors.forEach(processor => processor(message));
|
|
97
122
|
this.logInfo(`${message}`);
|
|
98
123
|
};
|
|
99
|
-
this.shell.stdout.on('data', printer);
|
|
100
|
-
this.shell.stderr.on('data', printer);
|
|
124
|
+
(_a = this.shell.stdout) === null || _a === void 0 ? void 0 : _a.on('data', printer);
|
|
125
|
+
(_b = this.shell.stderr) === null || _b === void 0 ? void 0 : _b.on('data', printer);
|
|
101
126
|
// Handle shell errors (stderr)
|
|
102
127
|
this.shell.on('data', printer);
|
|
103
128
|
// Handle shell exit
|
|
@@ -127,10 +152,14 @@ class Cli extends BaseCLI {
|
|
|
127
152
|
}
|
|
128
153
|
if (stderr)
|
|
129
154
|
reject(stderr);
|
|
130
|
-
if (stdout)
|
|
131
|
-
this.
|
|
132
|
-
|
|
155
|
+
if (stdout) {
|
|
156
|
+
this.stdoutProcessors.forEach(processor => processor(stdout));
|
|
157
|
+
this.logInfo(stdout);
|
|
158
|
+
}
|
|
159
|
+
if (stderr) {
|
|
160
|
+
this.stderrProcessors.forEach(processor => processor(stdout));
|
|
133
161
|
this.logError(stderr);
|
|
162
|
+
}
|
|
134
163
|
resolve({ stdout, stderr });
|
|
135
164
|
});
|
|
136
165
|
});
|
|
@@ -194,6 +223,22 @@ class Commando {
|
|
|
194
223
|
commando.cli.setUID(uid);
|
|
195
224
|
return commando;
|
|
196
225
|
};
|
|
226
|
+
commando.addStdoutProcessor = (processor) => {
|
|
227
|
+
cli.addStdoutProcessor(processor);
|
|
228
|
+
return commando;
|
|
229
|
+
};
|
|
230
|
+
commando.addStderrProcessor = (processor) => {
|
|
231
|
+
cli.addStderrProcessor(processor);
|
|
232
|
+
return commando;
|
|
233
|
+
};
|
|
234
|
+
commando.removeStdoutProcessor = (processor) => {
|
|
235
|
+
cli.removeStdoutProcessor(processor);
|
|
236
|
+
return commando;
|
|
237
|
+
};
|
|
238
|
+
commando.removeStderrProcessor = (processor) => {
|
|
239
|
+
cli.removeStderrProcessor(processor);
|
|
240
|
+
return commando;
|
|
241
|
+
};
|
|
197
242
|
return commando;
|
|
198
243
|
}
|
|
199
244
|
constructor() {
|
|
@@ -213,15 +258,23 @@ class Commando {
|
|
|
213
258
|
*/
|
|
214
259
|
this.executeFile = async (filePath, interpreter) => ({ stdout: '', stderr: '', });
|
|
215
260
|
this.executeRemoteFile = async (pathToFile, interpreter) => ({ stdout: '', stderr: '', });
|
|
261
|
+
this.addStdoutProcessor = (processor) => this;
|
|
262
|
+
this.addStderrProcessor = (processor) => this;
|
|
263
|
+
this.removeStdoutProcessor = (processor) => this;
|
|
264
|
+
this.removeStderrProcessor = (processor) => this;
|
|
216
265
|
}
|
|
217
266
|
}
|
|
218
267
|
exports.Commando = Commando;
|
|
219
268
|
class CommandoInteractive {
|
|
220
269
|
constructor() {
|
|
270
|
+
this.addStdoutProcessor = (processor) => this;
|
|
271
|
+
this.addStderrProcessor = (processor) => this;
|
|
272
|
+
this.removeStdoutProcessor = (processor) => this;
|
|
273
|
+
this.removeStderrProcessor = (processor) => this;
|
|
221
274
|
this.setUID = (uid) => this;
|
|
222
275
|
this.close = () => this;
|
|
223
276
|
this.kill = (signal) => true;
|
|
224
|
-
this.gracefullyKill = () => {
|
|
277
|
+
this.gracefullyKill = (pid) => {
|
|
225
278
|
return new Promise(resolve => resolve());
|
|
226
279
|
};
|
|
227
280
|
}
|
|
@@ -241,9 +294,25 @@ class CommandoInteractive {
|
|
|
241
294
|
commando.kill = (signal) => {
|
|
242
295
|
return commando.cli.kill(signal);
|
|
243
296
|
};
|
|
244
|
-
commando.gracefullyKill = () => {
|
|
297
|
+
commando.gracefullyKill = async (pid) => {
|
|
245
298
|
console.log('Commando Inter calling gracefullyKill');
|
|
246
|
-
|
|
299
|
+
await commando.cli.gracefullyKill(pid);
|
|
300
|
+
};
|
|
301
|
+
commando.addStdoutProcessor = (processor) => {
|
|
302
|
+
cli.addStdoutProcessor(processor);
|
|
303
|
+
return commando;
|
|
304
|
+
};
|
|
305
|
+
commando.addStderrProcessor = (processor) => {
|
|
306
|
+
cli.addStderrProcessor(processor);
|
|
307
|
+
return commando;
|
|
308
|
+
};
|
|
309
|
+
commando.removeStdoutProcessor = (processor) => {
|
|
310
|
+
cli.removeStdoutProcessor(processor);
|
|
311
|
+
return commando;
|
|
312
|
+
};
|
|
313
|
+
commando.removeStderrProcessor = (processor) => {
|
|
314
|
+
cli.removeStderrProcessor(processor);
|
|
315
|
+
return commando;
|
|
247
316
|
};
|
|
248
317
|
return commando;
|
|
249
318
|
}
|