Skip to content

09 How do I use terminal

Austin edited this page Dec 13, 2017 · 2 revisions

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`
    ]
};
  • log is a flag for toggling stdout and stderr to atoms console.log.
  • cp is a flag for using the cp python module to run scripts.
  • options is the object to be passed to child_process.spawn parameter.
  • args is an array that contains the arguments to pass to the shell or to the cp script.

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...

Example

'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();
Clone this wiki locally