nativescript-childprocess
by Rahul Pillai | v1.0.0
A NativeScript plugin that allows access to creating and running Java Processes in Android using Node.js child process style.
npm i --save nativescript-childprocess

Child Process (NativeScript Plugin)

A NativeScript plugin for creation and execution of Android java.lang.Processes in Node.js child process style! As a webapp developer, you needn't worry about familiarizing yourself with the Java Process API, thanks to this plugin.

Installation

  • For NativeScript 7+:
ns plugin add nativescript-childprocess
  • For NativeScript versions < 7:
tns plugin add [email protected]

Usage

  • The simplest implementation looks like so:
import { ChildProcess } from 'nativescript-childprocess'
...
onClickRun() {
ChildProcess.run('pm list packages')
.then(output => { console.log('Success:', output) })
.catch(error => { console.log('Failure:', error) })
}
  • A slightly advanced implementation with an interactive shell would look like so:
import { ChildProcess } from 'nativescript-childprocess'
...
async onClickRun() {
let childProcess: ChildProcess = new ChildProcess('su')
childProcess.runInteractive('ls /system/')
childProcess.runInteractive('ls /system/bin/')
await childProcess.closeSafely('exit')
console.log(childProcess.getOutput()) // output of both `ls` commands
}

API

constructor(startCommand: string)

Creates an interactive shell session.

  • Parameters: startCommand — interactive shell command (su is an example of one).

getErrors(): any

Returns errors that have been collected from the onset of this process execution (i.e. right from creation of the interactive shell session) up until now.

clearErrors(): void

Clears errors collected from the onset of this process execution up until now. Helps if errors until this point in time are irrelevant.

getProcess(): java.lang.Process

Exposes the underlying Java process to not be confined by the bounds of the ChildProcess plugin. Helps in cases where outputs of commands running in an interactive shell have to collected and processed as and when available.

  • Returns: — native Java process.

getOutput(): string

  • Returns: — output collected in course of execution of this process up until now.

    (For errors, check out getErrors()).

static run(param0: string): Promise<any> + 4 overloads

Executes a single command on a local terminal / shell (pm list packages for instance).

  • Parameters: param0 — command to be executed.

  • Returns: — promise that resembles that of Node.js child process that resolves to an output or

    rejects with an error.

runInteractive(command: string): ChildProcess

Runs commands inside of an interactive shell.

  • Parameters: command — sub-command is run inside the interactive shell created while

    instantiating ChildProcess.

  • Returns:ChildProcess reference which allows for cascaded runInteractive() or

    used to display the output or any errors.

closeSafely(exitCommand: string): Promise<ChildProcess>

Safely closes an interactive shell by running an exit command passed as a parameter.

  • Parameters: exitCommandexit.

  • Returns: — promise that resolves to a ChildProcess and can be used to display the

    output or any errors.

closeAbruptly(): Promise<ChildProcess>

Abruptly closes an interactive shell (which, for instance, is unresponsive such as a failing network activity) by killing it.

  • Returns: — promise that resolves to a ChildProcess and can be used to display the

    output or any errors.

toString(): string

  • Returns: — output of the entire process and any errors.

Made with :heart: by theGeekyLad