【发布时间】:2022-10-20 20:43:31
【问题描述】:
我正在尝试创建一个 vscode 扩展,您可以在其中在状态栏中创建一个按钮,单击该按钮将在终端中运行一个脚本。这将是一个很好的附加组件,特别是对于需要本地服务器、测试运行器或其他服务器(如 Storybook 或 Sanity Studio)的前端项目。
我已经完成了大部分功能,但终端行为没有按预期工作。
用户流程如下;
- 用户打开命令面板并选择添加按钮的选项
- 用户输入脚本,例如
npm start表示 React.js 项目 - 用户输入状态栏中按钮显示的名称
- 按钮出现,用户单击它,它将打开终端并运行脚本
预期的行为是每次按下按钮都会打开一个新终端并使用 1 个脚本。
现在这适用于一个按钮,但我希望多个按钮工作,每个按钮打开一个新终端并运行不同的脚本。就像 npm 脚本侧边栏的工作方式一样。
现在发生的情况是,对于每个打开的新终端,它们都会收到相同的文本发送给它
在
utilities.ts我有;export function createButton(name: string, command: string) { const statusBar = window.createStatusBarItem(StatusBarAlignment.Left, 0) statusBar.text = name statusBar.command = `workbench.action.terminal.focus` statusBar.tooltip = command statusBar.show() window.onDidOpenTerminal((terminal) => terminal.sendText(command!)) return statusBar }在具有用户流的
addButtonScript中创建按钮时使用哪个export async function addButtonScript(context: ExtensionContext) { const command = await window.showInputBox({ prompt: 'Add in the script command you want to run in the terminal', }) if (!command) return window.showErrorMessage('No command provided') const name = await nameInput() if (!name) return await createButton(name!, command!) await addSingleObjectToState({ context, name, command }) return }这是
extension.ts中activate function中注册命令的一部分const addButton = commands.registerCommand('extension.addButton', () => addButtonScript(context) )无论如何,完整的回购都在这里 - https://github.com/puyanwei/quick-scripts-v2
我的猜测是,通过这种实现,
window.OnDidOpenTerminal没有正确监听每个单独的终端,而是监听所有终端,这导致脚本全部在新终端中运行,但都使用相同的脚本。我很难知道最好使用的命令,你们有什么建议吗?
【问题讨论】:
-
您想在每次点击 stausBar 按钮时打开一个新终端吗?因此,例如,每次单击第一个按钮时,它都会打开一个新终端并发送该文本?或者是其他东西?如果您打开了多个终端,应该关注哪个?
-
这是正确的,对于每个按钮单击一个新的终端应该打开并运行该文本。现在所有打开的终端都得到与
onDidOpenTerminal监听所有终端相同的文本
标签: typescript visual-studio-code terminal vscode-extensions