使用自定义节点脚本创建 Visual Studio 代码集成终端作为 shell

Creating a Visual Studio Code integrated terminal with a custom Node script as the shell

我正在开发一个 Visual Studio 代码扩展,我希望在其中创建一个可以访问自定义 shell 的终端。我有一个实现此 shell 的 Node.js 脚本(.js 文件)。 现在,我正在尝试使用我的扩展程序中代码的 createTerminal 方法来启动一个终端,该终端使用我的 Node.js 脚本作为其 shell.

我不能直接将shellPath设置为我的js文件,因为我不能保证用户已经安装了Node.js,系统会运行 带有 Node.js 的此类文件,也没有安装什么版本的 Node。我需要能够指出可以处理该文件的普遍理解的二进制文件。粗略地说,我想这样做:

let terminal = vscode.window.createTerminal({
    name: "My terminal",
    shellPath: 'node', // Use the Node.js executable as the target
    shellArgs: [path.join(__dirname, 'my-shell-wrapper.js')] // Tell Node to run my shell
});
terminal.show();

我怎样才能做到这一点?是否有代码附带的可执行文件,我可以指向 运行s Node 脚本?还是我缺少另一种机制?

VSCode 现在(从去年左右开始)除了底层 shell 可执行文件之外,还可以选择为终端指定自定义行为。 createTerminal has an overload which takes ExtensionTerminalOptions; this has a pty property which allows you to specify custom handlers for reading from and writing to the terminal. From their Pseudoterminal documentation:

const writeEmitter = new vscode.EventEmitter<string>();
const pty: vscode.Pseudoterminal = {
  onDidWrite: writeEmitter.event,
  open: () => {},
  close: () => {},
  handleInput: data => writeEmitter.fire(data === '\r' ? '\r\n' : data)
};
vscode.window.createTerminal({ name: 'Local echo', pty });

这可以用来实现任意终端交互。