Skip to content

Commit

Permalink
updated docs, and examples
Browse files Browse the repository at this point in the history
  • Loading branch information
RIAEvangelist committed Feb 18, 2017
1 parent 012b8e9 commit 357b375
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 5 deletions.
67 changes: 66 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,73 @@ This work is licenced via the [DBAD Public Licence](http://www.dbad-license.org/
} else {
console.log('error', err)
}

}
);

```

## Accessing the CMD Process
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.


### Getting Process ID

```javascript

var cmd=require('../cmd.js');

var process=cmd.get('node');
console.log(process.pid);

```

### Running a python shell from node

```javascript
const cmd=require('../cmd.js');

const processRef=cmd.get('python -i');
let data_line = '';

//listen to the python terminal output
processRef.stdout.on(
'data',
function(data) {
data_line += data;
if (data_line[data_line.length-1] == '\n') {
console.log(data_line);
}
}
);

const pythonTerminalInput=`primes = [2, 3, 5, 7]
for prime in primes:
print(prime)
`;

//show what we are doing
console.log(`>>>${pythonTerminalInput}`);

//send it to the open python terminal
processRef.stdin.write(pythonTerminalInput);

```

Output :

```python

>>>primes = [2, 3, 5, 7]
for prime in primes:
print(prime)


2
3
5
7


```
4 changes: 2 additions & 2 deletions cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ var commandline={
};

function runCommand(command){
//return object with pid as one of keys, pid = cmd.run()['pid'];
//return refrence to the child process
return exec(
command
);
}

function getString(command,callback){
//return object with pid as one of keys, pid = cmd.get()['pid'];
//return refrence to the child process
return exec(
command,
(
Expand Down
4 changes: 4 additions & 0 deletions example/getPID.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
var cmd=require('../cmd.js');

var processRef=cmd.get('node');
console.log(processRef.pid);
27 changes: 27 additions & 0 deletions example/nodePythonTerminal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const cmd=require('../cmd.js');

const processRef=cmd.get('python -i');
let data_line = '';

//listen to the python terminal output
processRef.stdout.on(
'data',
function(data) {
data_line += data;
if (data_line[data_line.length-1] == '\n') {
console.log(data_line);
}
}
);

const pythonTerminalInput=`primes = [2, 3, 5, 7]
for prime in primes:
print(prime)
`;

//show what we are doing
console.log(`>>>${pythonTerminalInput}`);

//send it to the open python terminal
processRef.stdin.write(pythonTerminalInput);
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-cmd",
"version": "1.3.0",
"version": "2.0.0",
"description": "Simple commandline/terminal interface to allow you to run cli or bash style commands as if you were in the terminal.",
"main": "cmd.js",
"directories": {
Expand All @@ -19,7 +19,8 @@
"cmd",
"cli",
"bash",
"script"
"script",
"node"
],
"author": "Brandon Nozaki Miller",
"license": "DBAD",
Expand Down

0 comments on commit 357b375

Please sign in to comment.