How to execute local bash code from VSCode extension

前端 未结 1 787
青春惊慌失措
青春惊慌失措 2021-01-14 14:25

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

相关标签:
1条回答
  • 2021-01-14 15:22

    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.
    }
    
    0 讨论(0)
提交回复
热议问题