How to execute local bash code from VSCode extension

孤街浪徒 提交于 2020-01-11 07:33:05

问题


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.

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


回答1:


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.
}


来源:https://stackoverflow.com/questions/55465789/how-to-execute-local-bash-code-from-vscode-extension

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!