-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcommand_line.js
More file actions
103 lines (86 loc) · 2.16 KB
/
command_line.js
File metadata and controls
103 lines (86 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright Titanium I.T. LLC.
import * as ensure from "util/ensure.js";
import { OutputListener } from "util/output_listener.js";
/** Command-line I/O (process arguments, stdout, and stderr). */
export class CommandLine {
/**
* Factory method. Wraps the current process arguments, stdout, and stderr.
* @returns {CommandLine} the wrapped process
*/
static create() {
ensure.signature(arguments, []);
return new CommandLine(process);
}
/**
* Factory method. Simulates process arguments and discards writes to stdout and stderr.
* @param [args] simulated process arguments
* @returns {CommandLine} the simulated process
*/
static createNull({ args = [] } = {}) {
ensure.signature(arguments, [ [ undefined, { args: Array } ] ]);
return new CommandLine(new StubbedProcess(args));
}
/** Only for use by tests. (Use a factory method instead.) */
constructor(proc) {
this._process = proc;
this._stdoutListener = new OutputListener();
this._stderrListener = new OutputListener();
}
/**
* @returns {string[]} arguments to the current process, not including 'node' or the name of the script
*/
args() {
ensure.signature(arguments, []);
return this._process.argv.slice(2);
}
/**
* Write to stdout.
* @param text the text to write
*/
writeStdout(text) {
ensure.signature(arguments, [ String ]);
this._process.stdout.write(text);
this._stdoutListener.emit(text);
}
/**
* Write to stderr.
* @param text the text to write
*/
writeStderr(text) {
ensure.signature(arguments, [ String ]);
this._process.stderr.write(text);
this._stderrListener.emit(text);
}
/**
* Track writes to stdout.
* @returns {OutputTracker} the output tracker
*/
trackStdout() {
return this._stdoutListener.trackOutput();
}
/**
* Track writes to stderr.
* @returns {OutputTracker} the output tracker
*/
trackStderr() {
return this._stderrListener.trackOutput();
}
}
class StubbedProcess {
constructor(args) {
this._args = args;
}
get argv() {
return [ "stubbed_process_node", "stubbed_process_script.js", ...this._args ];
}
get stdout() {
return {
write() {},
};
}
get stderr() {
return {
write() {},
};
}
}