如何从VSCode扩展名执行本地bash代码 [英] How to execute local bash code from VSCode extension

查看:285
本文介绍了如何从VSCode扩展名执行本地bash代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为简单的git命令创建扩展,当用户在命令面板中输入命令时,例如Init,我想在其当前目录中调用git init.

I am creating an extension for simple git commands, and when a user enters a command in the Command Palette, like Init, I want to call git init on their current directory.

不幸的是,没有关于使用VSCode扩展API在本地执行代码的文档.有什么办法吗?

Unfortunately, there is no documentation on executing code locally with the VSCode extensions API. Is there any way to do this?

推荐答案

是的,可以通过使用运行一个Java jar .执行的核心如下所示:

Yes, this is possible, by using child_process.spawn. I have used it in my extension to run a Java jar. The core of the execution is shown here:

let spawnOptions = { cwd: options.baseDir ? options.baseDir : undefined };
let java = child_process.spawn("java", parameters, spawnOptions);

let buffer = "";
java.stderr.on("data", (data) => {
    let text = data.toString();
    if (text.startsWith("Picked up _JAVA_OPTIONS:")) {
        let endOfInfo = text.indexOf("\n");
        if (endOfInfo == -1) {
            text = "";
        } else {
            text = text.substr(endOfInfo + 1, text.length);
        }
    }

    if (text.length > 0) {
        buffer += "\n" + text;
    }
});

java.on("close", (code) => {
    // Handle the result + errors (i.e. the text in "buffer") here.
}

这篇关于如何从VSCode扩展名执行本地bash代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆