请问如何在终端执行命令并等待回调? #2792
Answered
by
bytemain
fankangsong
asked this question in
Q&A
请问如何在终端执行命令并等待回调?
#2792
-
|
Beta Was this translation helpful? Give feedback.
Answered by
bytemain
Jun 9, 2023
Replies: 3 comments
-
扒了源码,大致如下: import { Autowired } from "@opensumi/di";
import { ITerminalController } from "@opensumi/ide-terminal-next"; // 头部引入 `ITerminalController`
// 使用 `@Autowired` 自动加载
@Autowired(ITerminalController)
protected readonly terminalController: ITerminalController;
// 调用函数:
excuteCmd(cmd: string) {
try {
// 创建一个叫 NPM RUN 的 terminal 实例
const terminalClient = await this.terminalController.createTerminal({ config: { name: 'NPM RUN' }})
// 打卡底部的终端面板
await this.terminalController.showTerminalPanel();
// 等待实例化完成
await terminalClient.attached.promise;
// 执行命令,并回车
await terminalClient.sendText('node -v\r');
// 等待完成执行命令,返回状态
return Promise.resolve()
} catch (error) {
console.error(error)
return Promise.reject()
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
-
感谢提问,这部分的使用稍后会加入官方文档中。 |
Beta Was this translation helpful? Give feedback.
0 replies
-
文档已经更新:https://opensumi.com/zh/docs/integrate/module-usage/terminal-basic-usage |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
bytemain
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
文档已经更新:https://opensumi.com/zh/docs/integrate/module-usage/terminal-basic-usage