Skip to content

Commit 357b375

Browse files
committed
updated docs, and examples
1 parent 012b8e9 commit 357b375

File tree

5 files changed

+102
-5
lines changed

5 files changed

+102
-5
lines changed

README.md

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,73 @@ This work is licenced via the [DBAD Public Licence](http://www.dbad-license.org/
6969
} else {
7070
console.log('error', err)
7171
}
72-
72+
7373
}
7474
);
7575

7676
```
77+
78+
## Accessing the CMD Process
79+
If you need PIDs, stdio,stdin, stdout, stderr, etc. access, for use in your code, or cleaning up, @freemany added in some functionality to get a reference to the child process as the returned value of the ` get ` and ` run ` calls.
80+
81+
82+
### Getting Process ID
83+
84+
```javascript
85+
86+
var cmd=require('../cmd.js');
87+
88+
var process=cmd.get('node');
89+
console.log(process.pid);
90+
91+
```
92+
93+
### Running a python shell from node
94+
95+
```javascript
96+
const cmd=require('../cmd.js');
97+
98+
const processRef=cmd.get('python -i');
99+
let data_line = '';
100+
101+
//listen to the python terminal output
102+
processRef.stdout.on(
103+
'data',
104+
function(data) {
105+
data_line += data;
106+
if (data_line[data_line.length-1] == '\n') {
107+
console.log(data_line);
108+
}
109+
}
110+
);
111+
112+
const pythonTerminalInput=`primes = [2, 3, 5, 7]
113+
for prime in primes:
114+
print(prime)
115+
116+
`;
117+
118+
//show what we are doing
119+
console.log(`>>>${pythonTerminalInput}`);
120+
121+
//send it to the open python terminal
122+
processRef.stdin.write(pythonTerminalInput);
123+
124+
```
125+
126+
Output :
127+
128+
```python
129+
130+
>>>primes = [2, 3, 5, 7]
131+
for prime in primes:
132+
print(prime)
133+
134+
135+
2
136+
3
137+
5
138+
7
139+
140+
141+
```

cmd.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ var commandline={
66
};
77

88
function runCommand(command){
9-
//return object with pid as one of keys, pid = cmd.run()['pid'];
9+
//return refrence to the child process
1010
return exec(
1111
command
1212
);
1313
}
1414

1515
function getString(command,callback){
16-
//return object with pid as one of keys, pid = cmd.get()['pid'];
16+
//return refrence to the child process
1717
return exec(
1818
command,
1919
(

example/getPID.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
var cmd=require('../cmd.js');
2+
3+
var processRef=cmd.get('node');
4+
console.log(processRef.pid);

example/nodePythonTerminal.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const cmd=require('../cmd.js');
2+
3+
const processRef=cmd.get('python -i');
4+
let data_line = '';
5+
6+
//listen to the python terminal output
7+
processRef.stdout.on(
8+
'data',
9+
function(data) {
10+
data_line += data;
11+
if (data_line[data_line.length-1] == '\n') {
12+
console.log(data_line);
13+
}
14+
}
15+
);
16+
17+
const pythonTerminalInput=`primes = [2, 3, 5, 7]
18+
for prime in primes:
19+
print(prime)
20+
21+
`;
22+
23+
//show what we are doing
24+
console.log(`>>>${pythonTerminalInput}`);
25+
26+
//send it to the open python terminal
27+
processRef.stdin.write(pythonTerminalInput);

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "node-cmd",
3-
"version": "1.3.0",
3+
"version": "2.0.0",
44
"description": "Simple commandline/terminal interface to allow you to run cli or bash style commands as if you were in the terminal.",
55
"main": "cmd.js",
66
"directories": {
@@ -19,7 +19,8 @@
1919
"cmd",
2020
"cli",
2121
"bash",
22-
"script"
22+
"script",
23+
"node"
2324
],
2425
"author": "Brandon Nozaki Miller",
2526
"license": "DBAD",

0 commit comments

Comments
 (0)