Given a JS script running a D3js dynamic dataviz

I currently print out a single view into a output.svg file using :
var jsdom = require('jsdom');
jsdom.env(
"<html><body></body></html>", // CREATE DOM HOOK:
[ 'http://d3js.org/d3.v3.min.js', // JS DEPENDENCIES online ...
'js/d3.v3.min.js' ], // ... & offline
// D3JS MAP DRAWING CODE * * * * * * * * * * * * * * * * * * *
function (err, window) {
var svg = window.d3.select("body")
.append("svg") // append svg on body
... // more D3js code
// END svg design
//PRINTING OUT SELECTION
console.log(window.d3.select("body").html());
}
// END (D3JS) * * * * * * * * * * * * * * * * * * * * * * * *
);
Terminal NodeJS command :
node svgcreator.node.js > output.svg
The way it works is that each console.log() message from svgcreator.node.js script is printed into the file output.svg file.
Since this D3js dataviz turn around the globe, It should be possible to print out one console.log() message for each view into a proper svg file named correctly. I thing I should get a variable such var country=...; from the JS script up to the terminal. Then, I could do (?) something such :
node svgcreator.node.js > $(JsVar.country).svg # nb: this var syntax if fictional
So whenever a console.log() message come, the message in printed into an SVG file with the correct name.
Also, how to pass JS variable's value from script to terminal ?
See also: Node.js : how to pass parameter's value from terminal to JS script
The easiest thing to do is to just have node write to the file directly instead of trying to use > somefile.svg.
The code to do that is pretty simple. Let's say you have a variable in your script called country that contains the name of the country that you want to create an .svg for. Instead of using console.log to output your .svg file, write it out to country + '.svg' instead using fs.writeFileSync.
So, this:
console.log(window.d3.select("body").html());
Would change to this:
fs.writeFileSync(country + '.svg', window.d3.select("body").html());
Note: Don't forget to var fs = require('fs'); to get access to the fs module.