-
Notifications
You must be signed in to change notification settings - Fork 30
09 How do I use terminal
You only need the Shell and SpawnWrapper classes.
Create a Shell and pass the Shell.object or the Shell.get method call to SpawnWrapper.
> let shell = new Shell();
When shell is created, it automatically determines the OS and creates an object defining the parameters to pass to child_process.spawn in the background.
> let spawn = new(shell.object);
When spawn is created, it uses the shell.object to define the arguments that are passed to child_process.spawn when it is finally called.
Before we call spawn.tty, we need to define a special object required by the method call.
> let object = {
'log': true,
'cp': true,
'options': {},
'args': [
'python',
`${process.env.HOME}/Documents/Python/hello.py`
]
};-
logis a flag for togglingstdoutandstderrto atomsconsole.log. -
cpis a flag for using thecppython module to run scripts. -
optionsis the object to be passed tochild_process.spawnparameter. -
argsis an array that contains the arguments to pass to theshellor to thecpscript.
Then call spawn.tty and pass the object defining the parameters to be passed to child_process.spawn.
> spawn.tty(object);
spawn.tty returns a tty object called ChildProcess.
NOTE
If a relative path is required, you can pass the __dirname value to Shell when it's called.
NOTE
The path value passed to a new Shell instance should ALWAYS be a String and NOT an Object.
> let pathToCp = __dirname + '/../cp/main.py';
> let shell = new Shell(pathToCp);
> let spawn = new SpawnWrapper(shell.object);
// ...some more code...NOTE
If you want to modify the properties, you can do so after the SpawnWrapper instance has been created.
> let pathToCp = __dirname + '/../cp/main.py';
> let shell = new Shell(pathToCp);
> let spawn = new SpawnWrapper(shell.object);
> spawn.object.shell = 'terminator';
> spawn.object.option = ['-x'];
// ...some more code...'use strict';
const terminal = require('./terminal');
let object = {
'log': true,
'cp': true,
'options': {},
'args': [
'python',
`${process.env.HOME}/Documents/Python/hello.py`
]
}
let shell = new terminal.Shell();
let spawn = new terminal.SpawnWrapper(shell.object);
let process = spawn.tty(object);
process.unref();