shell


CloudControl Pro 9 Docs / shell

shell

Shell is Unix shell, which provides a set of commands for interacting with the operating system. Many programs can use shell to execute shell commands, such as terminal emulator.

CloudControl Pro 9 also includes a terminal emulator for executing npm, node commands. This module creates a Shell object or executes a shell command using exec function, which supports node and npm commands unless you modify the PATH environment variable.

This module provides functions createShell to create a new Shell object and exec function to execute a shell command once.

Table of contents

Interfaces

Type Aliases

Functions

Type Aliases

ExitResult

Ƭ ExitResult: string | number


PrivilegeType

Ƭ PrivilegeType: "root" | "adb"


StandardOutputType

Ƭ StandardOutputType: "stderr" | "stdout"

Functions

InputText

InputText(text): Promise<void>

Use default shell instance to execute input text command, simulate inputting text.

Default shell instance will be created automatically when using Tap, Swipe, SendKey, InputText etc. If you want to use adb permission or specific shell options, you should use setDefaultShellOptions before using these functions.

These functions start with uppercase letter, which means they require root or adb permission, otherwise they only work in this app.

Example

"nodejs";
const { InputText } = require("shell");
async function main() {
    await InputText("Hello, World");
}
main();

Parameters

Name Type Description
text string Text to input, only support ASCII characters

Returns

Promise<void>


SendKey

SendKey(key): Promise<void>

Use default shell instance to execute input keyevent key command, simulate sending key.

Default shell instance will be created automatically when using Tap, Swipe, SendKey, InputText etc. If you want to use adb permission or specific shell options, you should use setDefaultShellOptions before using these functions.

These functions start with uppercase letter, which means they require root or adb permission, otherwise they only work in this app.

Example

"nodejs";
const { SendKey, setDefaultShellOptions } = require("shell");

async function main() {
    setDefaultShellOptions({
       adb: true,
    });
    await SendKey("HOME");
}
main();

Parameters

Name Type Description
key string | number Key name or key code, like "HOME" or 3, see KeyEventopen in new window

Returns

Promise<void>


Swipe

Swipe(x1, y1, x2, y2, duration?): Promise<void>

Use default shell instance to execute input swipe x1 y1 x2 y2 duration command, simulate sliding from position (x1, y1) to position (x2, y2).

Default shell instance will be created automatically when using Tap, Swipe, SendKey, InputText etc. If you want to use adb permission or specific shell options, you should use setDefaultShellOptions before using these functions.

These functions start with uppercase letter, which means they require root or adb permission, otherwise they only work in this app.

Example

"nodejs";
const { Swipe } = require("shell");
async function main() {
    await Swipe(800, 100, 800, 1000);
}
main();

Parameters

Name Type Description
x1 number -
y1 number -
x2 number -
y2 number -
duration? number Swipe duration, unit is millisecond

Returns

Promise<void>


Tap

Tap(x, y): Promise<void>

Use default shell instance to execute input tab command, simulate clicking position (x, y).

Default shell instance will be created automatically when using Tap, Swipe, SendKey, InputText etc. If you want to use adb permission or specific shell options, you should use setDefaultShellOptions before using these functions.

These functions start with uppercase letter, which means they require root or adb permission, otherwise they only work in this app.

Example

"nodejs";
const { Tap } = require("shell");
async function main() {
    await Tap(100, 100);
}
main();

Parameters

Name Type
x number
y number

Returns

Promise<void>


checkAccess

checkAccess(type): Promise<boolean>

Check if there is a specific privilege, such as whether there is root permission.

Example

"nodejs";

const { checkAccess } = require("shell");
async function main() {
   const hasRoot = await checkAccess("root");
   const hasAdb = await checkAccess("adb");
   console.log(`hasRoot: ${hasRoot}, hasAdb: ${hasAdb}`);
}
main();

Parameters

Name Type Description
type PrivilegeType Privilege type, such as "root" or "adb"

Returns

Promise<boolean>

Whether there is a specific privilege


createShell

createShell(options?): Shell

Create a Shell instance.

We usually use exec function to execute a single command and get the result, but if there are multiple commands to execute, the efficiency of Shell object is higher. This is because we don't need to create a new shell process every time we execute.

We can also listen to Shell's output by using Shell object.

Example

"nodejs";
const { createShell } = require("shell");
const shell = createShell();
shell.on("line", (line) => {
    console.log(line);
});
shell.exec("ls");

const id = $autojs.keepRunning();
shell.exit().then(() => $autojs.cancelKeepRunning(id));

Parameters

Name Type Description
options? ShellOptions Shell options, will override default options

Returns

Shell

New shell instance


exec

exec(cmd, options?): Promise<ExecutionResult>

Create a new shell process, and execute a command, return the result asynchronously.

Example

"nodejs";
const { exec, isRootAvailable } = require("shell");
async function main() {
    console.log(await exec("npm"));
    if (await isRootAvailable()) {
       console.log(await exec("ls /data", { root: true }));
    }
}
main();

Parameters

Name Type Description
cmd string Command to execute
options? ShellOptions Shell options, will override default options

Returns

Promise<ExecutionResult>

Promise of execution result


getDefaultShellOptions

getDefaultShellOptions(): ShellOptions

Get default options for Shell.

See

setDefaultShellOptions

Returns

ShellOptions


isRootAvailable

isRootAvailable(): Promise<boolean>

Check if device is rooted. Note that device is rooted does not mean this app has root permission.

Example

"nodejs";

const { isRootAvailable } = require("shell");
async function main() {
   const rootAvailable = await isRootAvailable();
   console.log(`rootAvailable: ${rootAvailable}`);
}
main();

Returns

Promise<boolean>


setDefaultShellOptions

setDefaultShellOptions(options): void

Set default options for Shell. These options include whether to use Root permission, adb permission, environment variables, etc. They will be used when creating new Shell or RootAutomator instance.

Parameters

Name Type Description
options ShellOptions Shell options

Returns

void