@nu-art/commando 0.204.29 → 0.204.31
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 +19 -5
- package/core/cli.js +89 -20
- package/package.json +1 -1
package/core/cli.d.ts
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
/// <reference types="node" />
|
|
7
7
|
/// <reference types="node" />
|
|
8
8
|
import { ExecOptions } from 'child_process';
|
|
9
|
-
import { Constructor, Logger } from '@nu-art/ts-common';
|
|
9
|
+
import { AsyncVoidFunction, Constructor, Logger } from '@nu-art/ts-common';
|
|
10
10
|
export type CliBlock<Cli extends CliWrapper> = (cli: Cli) => void;
|
|
11
11
|
export type CliOptions = ExecOptions & {
|
|
12
12
|
encoding: BufferEncoding | string | null;
|
|
@@ -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,14 +42,18 @@ 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;
|
|
46
52
|
constructor();
|
|
47
53
|
execute: () => Promise<void>;
|
|
48
|
-
endInteractive: () => void;
|
|
54
|
+
endInteractive: (cb?: AsyncVoidFunction) => void;
|
|
49
55
|
kill: (signal?: NodeJS.Signals | number) => boolean;
|
|
50
|
-
gracefullyKill: () => Promise<void>;
|
|
56
|
+
gracefullyKill: (pid?: number) => Promise<void>;
|
|
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
|
-
close: () => this;
|
|
121
|
+
close: (cb?: AsyncVoidFunction) => 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,74 @@ 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
|
-
this.endInteractive = () => {
|
|
71
|
-
|
|
86
|
+
this.endInteractive = (cb) => {
|
|
87
|
+
var _a;
|
|
88
|
+
(_a = this.shell.stdin) === null || _a === void 0 ? void 0 : _a.end(cb);
|
|
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}`);
|
|
81
98
|
resolve();
|
|
82
99
|
});
|
|
83
|
-
if (
|
|
100
|
+
if (pid) {
|
|
101
|
+
console.log(`KILLING PID: ${pid}`);
|
|
102
|
+
process.kill(pid, 'SIGINT');
|
|
103
|
+
}
|
|
104
|
+
else {
|
|
105
|
+
console.log(`KILLING SHELL WITH SIGINT`);
|
|
84
106
|
this.shell.kill('SIGINT');
|
|
85
|
-
|
|
86
|
-
process.kill(-this.shell.pid, 'SIGINT');
|
|
107
|
+
}
|
|
87
108
|
});
|
|
88
109
|
};
|
|
89
110
|
this.shell = (0, node_child_process_1.spawn)('/bin/bash', {
|
|
90
|
-
|
|
111
|
+
detached: true,
|
|
112
|
+
shell: true
|
|
91
113
|
});
|
|
92
114
|
// Handle shell output (stdout)
|
|
93
115
|
const printer = (data) => {
|
|
94
116
|
const message = data.toString().trim();
|
|
95
117
|
if (!message.length)
|
|
96
118
|
return;
|
|
119
|
+
this.stdoutProcessors.forEach(processor => processor(message));
|
|
120
|
+
this.stderrProcessors.forEach(processor => processor(message));
|
|
97
121
|
this.logInfo(`${message}`);
|
|
98
122
|
};
|
|
99
|
-
this.shell.stdout.on('data', printer);
|
|
100
|
-
this.shell.stderr.on('data', printer);
|
|
123
|
+
(_a = this.shell.stdout) === null || _a === void 0 ? void 0 : _a.on('data', printer);
|
|
124
|
+
(_b = this.shell.stderr) === null || _b === void 0 ? void 0 : _b.on('data', printer);
|
|
101
125
|
// Handle shell errors (stderr)
|
|
102
126
|
this.shell.on('data', printer);
|
|
103
127
|
// Handle shell exit
|
|
@@ -127,10 +151,14 @@ class Cli extends BaseCLI {
|
|
|
127
151
|
}
|
|
128
152
|
if (stderr)
|
|
129
153
|
reject(stderr);
|
|
130
|
-
if (stdout)
|
|
131
|
-
this.
|
|
132
|
-
|
|
154
|
+
if (stdout) {
|
|
155
|
+
this.stdoutProcessors.forEach(processor => processor(stdout));
|
|
156
|
+
this.logInfo(stdout);
|
|
157
|
+
}
|
|
158
|
+
if (stderr) {
|
|
159
|
+
this.stderrProcessors.forEach(processor => processor(stdout));
|
|
133
160
|
this.logError(stderr);
|
|
161
|
+
}
|
|
134
162
|
resolve({ stdout, stderr });
|
|
135
163
|
});
|
|
136
164
|
});
|
|
@@ -194,6 +222,22 @@ class Commando {
|
|
|
194
222
|
commando.cli.setUID(uid);
|
|
195
223
|
return commando;
|
|
196
224
|
};
|
|
225
|
+
commando.addStdoutProcessor = (processor) => {
|
|
226
|
+
cli.addStdoutProcessor(processor);
|
|
227
|
+
return commando;
|
|
228
|
+
};
|
|
229
|
+
commando.addStderrProcessor = (processor) => {
|
|
230
|
+
cli.addStderrProcessor(processor);
|
|
231
|
+
return commando;
|
|
232
|
+
};
|
|
233
|
+
commando.removeStdoutProcessor = (processor) => {
|
|
234
|
+
cli.removeStdoutProcessor(processor);
|
|
235
|
+
return commando;
|
|
236
|
+
};
|
|
237
|
+
commando.removeStderrProcessor = (processor) => {
|
|
238
|
+
cli.removeStderrProcessor(processor);
|
|
239
|
+
return commando;
|
|
240
|
+
};
|
|
197
241
|
return commando;
|
|
198
242
|
}
|
|
199
243
|
constructor() {
|
|
@@ -213,15 +257,23 @@ class Commando {
|
|
|
213
257
|
*/
|
|
214
258
|
this.executeFile = async (filePath, interpreter) => ({ stdout: '', stderr: '', });
|
|
215
259
|
this.executeRemoteFile = async (pathToFile, interpreter) => ({ stdout: '', stderr: '', });
|
|
260
|
+
this.addStdoutProcessor = (processor) => this;
|
|
261
|
+
this.addStderrProcessor = (processor) => this;
|
|
262
|
+
this.removeStdoutProcessor = (processor) => this;
|
|
263
|
+
this.removeStderrProcessor = (processor) => this;
|
|
216
264
|
}
|
|
217
265
|
}
|
|
218
266
|
exports.Commando = Commando;
|
|
219
267
|
class CommandoInteractive {
|
|
220
268
|
constructor() {
|
|
269
|
+
this.addStdoutProcessor = (processor) => this;
|
|
270
|
+
this.addStderrProcessor = (processor) => this;
|
|
271
|
+
this.removeStdoutProcessor = (processor) => this;
|
|
272
|
+
this.removeStderrProcessor = (processor) => this;
|
|
221
273
|
this.setUID = (uid) => this;
|
|
222
|
-
this.close = () => this;
|
|
274
|
+
this.close = (cb) => this;
|
|
223
275
|
this.kill = (signal) => true;
|
|
224
|
-
this.gracefullyKill = () => {
|
|
276
|
+
this.gracefullyKill = (pid) => {
|
|
225
277
|
return new Promise(resolve => resolve());
|
|
226
278
|
};
|
|
227
279
|
}
|
|
@@ -235,15 +287,32 @@ class CommandoInteractive {
|
|
|
235
287
|
commando.cli.setUID(uid);
|
|
236
288
|
return commando;
|
|
237
289
|
};
|
|
238
|
-
commando.close = () => {
|
|
290
|
+
commando.close = (cb) => {
|
|
291
|
+
commando.cli.endInteractive(cb);
|
|
239
292
|
return commando;
|
|
240
293
|
};
|
|
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
|
}
|