shell - Shell commands
On Unix-like systems, a shell is the command layer that talks to the OS. Many tools can run shell commands (terminal emulators, IDEs, etc.).
Auto.js Pro 9 ships a terminal for node, npm, and more. Commands run through this module’s Shell instances or exec support node and npm in non-packaged builds unless PATH was changed.
The module exposes createShell for interactive sessions (many commands, streamed output) and exec for one-shot commands with a single result object.
Table of contents
Interfaces
Types
Functions
- InputText
- SendKey
- Swipe
- Tap
- checkAccess
- createShell
- exec
- getDefaultShellOptions
- isRootAvailable
- setDefaultShellOptions
Types
ExitResult
ExitResult: string | number
PrivilegeType
PrivilegeType: "root" | "adb"
StandardOutputType
StandardOutputType: "stderr" | "stdout"
Functions
InputText
InputText(text: string): Promise<void>
Runs input text via the default shell to type ASCII text (not Chinese characters).
The default shell is created on first use of Tap, Swipe, SendKey, or InputText using getDefaultShellOptions. Call setDefaultShellOptions before these helpers if you need adb or custom options.
Names start with a capital letter because they usually need root or adb; otherwise they only affect this app.
Example
"nodejs";
const { InputText } = require("shell");
async function main() {
await InputText("Hello, World");
}
main();Parameters
text: ASCII text to inject.
Returns
Promise<void>
SendKey
SendKey(key: string | number): Promise<void>
Runs input keyevent … via the default shell to synthesize a key (for example "HOME" or keycode 3). See Android KeyEvent.
Same default-shell / setDefaultShellOptions behavior as InputText.
Example
"nodejs";
const { SendKey, setDefaultShellOptions } = require("shell");
async function main() {
setDefaultShellOptions({
adb: true,
});
await SendKey("HOME");
}
main();Parameters
key: Key name or numeric keycode.
Returns
Promise<void>
Swipe
Swipe(x1: number, y1: number, x2: number, y2: number, duration?: number): Promise<void>
Runs input swipe x1 y1 x2 y2 [duration] via the default shell.
Same default-shell / privilege notes as above.
Example
"nodejs";
const { Swipe } = require("shell");
async function main() {
await Swipe(800, 100, 800, 1000);
}
main();Parameters
x1,y1: Start coordinates.x2,y2: End coordinates.duration: Duration in milliseconds (optional).
Returns
Promise<void>
Tap
Tap(x: number, y: number): Promise<void>
Runs input tap x y via the default shell to tap a point.
Same default-shell / privilege notes as above.
Example
"nodejs";
const { Tap } = require("shell");
async function main() {
await Tap(100, 100);
}
main();Parameters
x,y: Tap coordinates.
Returns
Promise<void>
checkAccess
checkAccess(type: PrivilegeType ): Promise<boolean>
Checks whether a privilege (for example root or adb) is available.
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
type:"root"or"adb".
Returns
Promise<boolean>
createShell
createShell(options?: ShellOptions ): Shell`
Creates a reusable Shell session—ideal for many commands because it avoids spawning a new process each time. You can listen for output lines on the returned 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
options: Overrides merged with the default shell options.
Returns
exec
exec(cmd: string, options?: ShellOptions ): Promise< ExecutionResult >
Spawns a process, runs cmd, and resolves with structured output.
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
cmd: Command line to execute.options: Optional shell overrides.
Returns
Promise< ExecutionResult >
getDefaultShellOptions
getDefaultShellOptions(): ShellOptions`
Returns the default options used when creating shells or the capital-letter helpers.
See also
Returns
isRootAvailable
isRootAvailable(): Promise<boolean>
true if the device appears rooted. Rooted hardware does not guarantee this app was granted root.
Example
"nodejs";
const { isRootAvailable } = require("shell");
async function main() {
const rootAvailable = await isRootAvailable();
console.log(`rootAvailable: ${rootAvailable}`);
}
main();Returns
Promise<boolean>
setDefaultShellOptions
setDefaultShellOptions(options: ShellOptions ): void
Updates defaults (root/adb flags, env vars, etc.) used whenever a new shell or root automator is created.
Parameters
options: New default shell configuration.
Returns
void
