接口: Shell


接口: Shell

shell.Shell

Shell类,通过createShell创建新实例。

继承关系

  • unknown

    Shell

目录

方法

Events

方法

exec

exec(cmd): Promise<ExecutionResult>

执行shell命令,并等待执行结果。

示例

"nodejs";
const { createShell } = require('shell');
async function main() {
    const shell = createShell();
    console.log(await shell.exec("touch test.txt"));
    console.log(await shell.exec("ls -l test.txt"));
    await shell.exit();
}
main();

参数

名称 类型 描述
cmd string 要执行的命令

返回值

Promise<ExecutionResult>

执行结果的Promise


exit

exit(forcedly?): Promise<ExitResult>

退出shell进程。若forcedly为true,则会直接杀死进程,此时返回值为字符串,表示杀死进程的signal;如果forcedly为false,则会使用exit命令退出shell进程,并返回退出码(exit code)。

参数

名称 类型 描述
forcedly? boolean 是否强制退出

返回值

Promise<ExitResult>


submit

submit(input): void

往shell的标准输入中提交文本内容,若文本内容末尾未包含换行符,则会自动添加换行符。

参数

名称 类型
input string

返回值

void

Events

on

on(event, listener): Shell

shell的标准输出或标准输出结果有新数据的事件。type参数用于区分是标准输出还是标准错误输出。

data

参数

名称 类型
event "data"
listener (chunk: Buffer, type: StandardOutputType) => void

返回值

Shell

on(event, listener): Shell

shell的标准输出或标准输出结果有新的一行数据的事件。type参数用于区分是标准输出还是标准错误输出。

line

参数

名称 类型
event "line"
listener (line: string, type: StandardOutputType) => void

返回值

Shell