Perry
Perry

Reputation: 1217

get spawned process environment

In my node application I would like to spawn the vcvars32.bat then use the environment set by it to spawn the cl.exe.

There is a way to get the new "Environment key-value pairs" from ended child process?

Many thanks.

Upvotes: 0

Views: 87

Answers (1)

Perry
Perry

Reputation: 1217

After some trials, I developed this solution that looks a trick to me:

const cp = require("child_process");
const process = require("process")
const fs = require("fs")

fs.writeFileSync("testCL.bat",
    "call \"c:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Community\\VC\\Auxiliary\\Build\\vcvars32.bat\"\r\nset\r\n")


var env1 = {};
function onData(str) {
    //console.log(str.toString())
    /** @type{String[]} */
    var str = str.toString().split(/[\r\n]{1,2}/);
    for(let i=0;i<str.length-1;++i) {
        var m = str[i].match(/([^=]+)=(.*)$/);
        if(m) {
            env1[ m[1].toUpperCase() ] = m[2];
        }
    }
}

var p1 = cp.spawn( "testCL.bat",{env:process.env})
p1.stdout.on('data', onData);
p1.on("exit", (c) => {
    fs.unlink("testCL.bat", ()=>{});
    var p2 = cp.spawn( "cl.exe",[], { "env":env1 })
    p2.stdout.on('data', d=>console.log(d.toString()));
});

So i create a batch file with the call to vcvars and the command set, then parse the set's output to create the environment variable.

Upvotes: 1

Related Questions